我的应用程序有两个文件,在第二个文件page_one.py中,我不能正确使用锚方法。标签“left”和“right”总是位于屏幕的中间,而不是侧面第一个如何使anchor选项起作用?
page_one.py
anchor
eqoofvh91#
这是因为你的PageOne框架没有填满Main。把fill="both"添加到它的pack方法中:
PageOne
Main
fill="both"
pack
import tkinter as tkclass PageOne(tk.Frame): def __init__(self, parent, *args, **kwargs): super().__init__(parent, *args, **kwargs) self.one_label = tk.Label(self, text='LEFT') self.one_label.pack(padx=(20,0), side='left', anchor='w') self.two_label = tk.Label(self, text='RIGHT') self.two_label.pack(padx=(0,20), side='right', anchor='e') class Main(tk.Frame): def __init__(self, parent, *args, **kwargs): super().__init__(parent, *args, **kwargs) self.page_one = PageOne(self) self.page_one.pack(expand='True', fill="both")if __name__ == "__main__": root = tk.Tk() main = Main(root) #root.attributes("-fullscreen", True) root.geometry("1280x720") main.pack(side="top", fill="both", expand=True) root.mainloop()
import tkinter as tk
class PageOne(tk.Frame):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.one_label = tk.Label(self, text='LEFT')
self.one_label.pack(padx=(20,0), side='left', anchor='w')
self.two_label = tk.Label(self, text='RIGHT')
self.two_label.pack(padx=(0,20), side='right', anchor='e')
class Main(tk.Frame):
self.page_one = PageOne(self)
self.page_one.pack(expand='True', fill="both")
if __name__ == "__main__":
root = tk.Tk()
main = Main(root)
#root.attributes("-fullscreen", True)
root.geometry("1280x720")
main.pack(side="top", fill="both", expand=True)
root.mainloop()
请注意,您可以在init函数中使用super()(没有self作为参数)
init
super()
self
34gzjxbg2#
在压缩PageOne框架时,您必须指定fill="both",以使其在x和y轴上完全展开。请相应地更新您的main.py。
main.py
# main.pyimport tkinter as tkfrom page_one import PageOneclass Main(tk.Frame): def __init__(self, parent, *args, **kwargs): tk.Frame.__init__(self, parent, *args, **kwargs) self.page_one = PageOne(self) self.page_one.pack(fill="both", expand='True')if __name__ == "__main__": root = tk.Tk() main = Main(root) root.attributes("-fullscreen", True) main.pack(side="top", fill="both", expand=True) root.mainloop()
# main.py
from page_one import PageOne
tk.Frame.__init__(self, parent, *args, **kwargs)
self.page_one.pack(fill="both", expand='True')
root.attributes("-fullscreen", True)
2条答案
按热度按时间eqoofvh91#
这是因为你的
PageOne
框架没有填满Main
。把fill="both"
添加到它的pack
方法中:请注意,您可以在
init
函数中使用super()
(没有self
作为参数)34gzjxbg2#
在压缩
PageOne
框架时,您必须指定fill="both"
,以使其在x和y轴上完全展开。请相应地更新您的main.py
。