[英]Using self.pack and self.grid works without classes, but fails with them
所以我嘗試在 Grid 中使用 Tkinter 按鈕 Alignment來制作工具欄,但不同之處在於我使用類。 因此,當他們使用 frame.pack 和 button.grid 時,我得到了同時使用 pack 和 grid 的錯誤。 這是我的代碼:
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.title('Hi!')
self.master.configure(bg='dark grey')
self.pack(fill=X, side=TOP)
self.create_toolbar()
def create_toolbar(self):
Home = Button(text='Home', bd=1)
About = Button(text='About', bd=1)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
Home.grid(row=0, column=0, sticky=W+E)
About.grid(row=0, column=1, sticky=W+E)
在我看來,這里的問題是小部件沒有主集,因此它們默認為類的主集,您已經使用包進行幾何管理。
這里的解決方案是在聲明小部件時設置master=self
,然后當您 go 將小部件放置在 tk.Frame object 中時,您可以使用網格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.