python Tkinter:包的锚选项不起作用

vbopmzt1  于 2022-11-21  发布在  Python
关注(0)|答案(2)|浏览(189)

我的应用程序有两个文件,在第二个文件page_one.py中,我不能正确使用锚方法。标签“left”和“right”总是位于屏幕的中间,而不是侧面
第一个
如何使anchor选项起作用?

eqoofvh9

eqoofvh91#

这是因为你的PageOne框架没有填满Main。把fill="both"添加到它的pack方法中:

  1. import tkinter as tk
  2. class PageOne(tk.Frame):
  3. def __init__(self, parent, *args, **kwargs):
  4. super().__init__(parent, *args, **kwargs)
  5. self.one_label = tk.Label(self, text='LEFT')
  6. self.one_label.pack(padx=(20,0), side='left', anchor='w')
  7. self.two_label = tk.Label(self, text='RIGHT')
  8. self.two_label.pack(padx=(0,20), side='right', anchor='e')
  9. class Main(tk.Frame):
  10. def __init__(self, parent, *args, **kwargs):
  11. super().__init__(parent, *args, **kwargs)
  12. self.page_one = PageOne(self)
  13. self.page_one.pack(expand='True', fill="both")
  14. if __name__ == "__main__":
  15. root = tk.Tk()
  16. main = Main(root)
  17. #root.attributes("-fullscreen", True)
  18. root.geometry("1280x720")
  19. main.pack(side="top", fill="both", expand=True)
  20. root.mainloop()

请注意,您可以在init函数中使用super()(没有self作为参数)

展开查看全部
34gzjxbg

34gzjxbg2#

在压缩PageOne框架时,您必须指定fill="both",以使其在x和y轴上完全展开。请相应地更新您的main.py

  1. # main.py
  2. import tkinter as tk
  3. from page_one import PageOne
  4. class Main(tk.Frame):
  5. def __init__(self, parent, *args, **kwargs):
  6. tk.Frame.__init__(self, parent, *args, **kwargs)
  7. self.page_one = PageOne(self)
  8. self.page_one.pack(fill="both", expand='True')
  9. if __name__ == "__main__":
  10. root = tk.Tk()
  11. main = Main(root)
  12. root.attributes("-fullscreen", True)
  13. main.pack(side="top", fill="both", expand=True)
  14. root.mainloop()
展开查看全部

相关问题