使用self.pack和self.grid可以在没有类的情况下工作,但在有类的情况下却失败了。

问题描述 投票:0回答:1

所以我试着用 网格中的Tkinter按钮对齐 来制作一个工具栏,但不同的是我使用的是类,所以当他们使用frame.pack和button.grid时,我得到了同时使用pack和grid的错误。所以当他们使用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)
python python-3.x button tkinter tk
1个回答
1
投票

在我看来,这里的问题是Widgets没有设置主站,所以它们默认使用类的主站,而你已经使用pack来管理几何体。

这里的解决方案是设置 master=self 当你声明你的widgets时,当你把widgets放置在tk.Frame对象中时,你可以使用grid.Frame对象。

© www.soinside.com 2019 - 2024. All rights reserved.