tkinter类继承不显示任何内容

时间:2018-12-04 08:19:33

标签: python class inheritance tkinter

我可能不太了解继承,或者我可能错过了一些有关继承的细节。我正在阅读https://stackoverflow.com/a/40130619/5989438,并认为我需要这样做,但是它不起作用。

我在Application()类中有一个完整的程序,该程序位于选项卡“ Week_1”中。对于OOP,由于我的知识有限,我正在考虑为每个选项卡(每个星期)创建一个类,因此我编写了这样的程序。如果有更好的设置,我很高兴学习。感谢您的协助。这是程序的简化版本。

import tkinter as tk
from tkinter import ttk

#upper tabs
upper_tabs = ["Final", "Requests", "Control"]
lower_tabs = ["Week 1", "Week 2"]
tabs2 = {} #upper
tabs = {} #lower

class Application(tk.Frame): #inherent from frame.
    def __init__(self, parent):
        tk.Frame.__init__(self, parent, bg="tan")
        self.parent = parent
        self.pack(fill=tk.BOTH, expand=1)
        self.Days= ["Monday", "Tuesday", "Wednesday", "Thursday",    
                    "Friday", "Saturday", "Sunday"]
        self.GUI()

    def GUI(self): #the function that runs all the GUI functions.
        self.create_lower_tabs()

####---------create grid_GUI---------------------####

    def create_lower_tabs(self):
        style1 = ttk.Style()
        style1.configure("down.TNotebook", tabposition = "sw")
        self.tabControl_lower = ttk.Notebook(self, width=1200,   
                                            height=550, padding = 0,  
                                            style = "down.TNotebook" )
        for name in lower_tabs:
            self.tab1=tk.Frame(self.tabControl_lower, bg='old lace')
            self.tabControl_lower.add(self.tab1, text=name, )
            tabs[name] = self.tab1 #add the widets to the dict.
            self.tabControl_lower.pack(fill=tk.BOTH, expand=1)

class Week_1(Application):
    def __init__(self, parent):
        Application.__init__(self, parent)
        week_1 = Week_1(Application)
        self.pack(fill=tk.BOTH, expand=1)
        self.GUI()

    def GUI(self): #the function that runs all the GUI functions.
        self.buttons("Week 1")

    def buttons(self, name):
        self.button = tk.Button(tabs[name], text="test button",          
                                bg="salmon",)
        self.button.pack()



def main():
    root = tk.Tk()
    root.title("class basic window")
    root.config(background="LightBlue4")
    app = Application(root)
    root.mainloop()

if __name__ == '__main__':
    main()

0 个答案:

没有答案