首页 > 解决方案 > 继承父窗口并在 tkinter 中添加一个附加按钮?

问题描述

我正在尝试创建两个单独的窗口,其中一个应该继承其他界面,并网格化一些额外的按钮。我怎样才能做到这一点?下面是一段示例代码:

f = ("Helvetica", 18)
bg = 'white'
g = '1400x800'

class MainUser(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        Frame.configure(self, background='white')

        self.logo = PhotoImage(file="logo.gif")
        Label(self, image=self.logo).pack()

        Button(self, text='test', bg=bg, font=f).pack()

class MainAdmin(MainUser):
    pass # What now?

标签: python-3.xclasstkinter

解决方案


您只需要创建一个__init__在超类中调用相同函数的属性。然后,像在超类中那样添加小部件。

例子:

class MainAdmin(MainUser):
    def __init__(self, master):
        super().__init__(master)

        another_label = Label(self, text="Hello from MainAdmin")
        another_label.pack(side="top", fill="x")

推荐阅读