Python Tkinter用面向对象编程关闭程序并在代码中打开

Python Tkinter用面向对象编程关闭程序并在代码中打开,python,tkinter,python-3.4,Python,Tkinter,Python 3.4,几天前,我开始用tkinter开发我的System Manager GUI程序,我被选项卡住了。我想从程序内部重新定义应用颜色选项时的所有内容,但我做不到,所以我想到了一个想法,即当前程序窗口关闭,相同窗口打开,但应用了新颜色。所以我设法再次打开那个项目,但现在我不知道如何关闭最后一个。 以下是代码的一部分: class Menu(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args

几天前,我开始用tkinter开发我的System Manager GUI程序,我被选项卡住了。我想从程序内部重新定义应用颜色选项时的所有内容,但我做不到,所以我想到了一个想法,即当前程序窗口关闭,相同窗口打开,但应用了新颜色。所以我设法再次打开那个项目,但现在我不知道如何关闭最后一个。 以下是代码的一部分:

class Menu(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        for F in (StartMenu, Settings, Colour):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
            frame.configure(background="white")
        frame.grid(row=0, column=0, sticky="nsew")
        tk.Tk.wm_title(self, "System Manager")
        tk.Tk.geometry(self, "1500x800+200+100")
        self.show_frame(StartMenu)
    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()        
class StartMenu(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        SMLabel = tk.Label(self, text="System", font=("Aharoni Bold", 80), fg=Color[0], bg="white").place(x=325)
        SMLabel2 = tk.Label(self, text="Manager", font=("Aharoni Bold", 80), fg="grey", bg="white").place(x=750)


        Button1 = tk.Button(self, text="Settings",font=("Arial", 80), bg=BB[0], activebackground=BB[0], width=7, fg=Color[0],
                             command=lambda: controller.show_frame(Settings))
        Button1.place(x=250, y=500)


        Button2 = tk.Button(self, text="Quit",font=("Arial", 80), bg=BB[0], activebackground=BB[0], width=7, fg=Color[0],
                             command=lambda: exit1)
        Button2.place(x=800, y=500)


        Button3 = tk.Button(self, text="Find", font=("Arial", 80), bg=BB[0], activebackground=BB[0], width=7, fg=Color[0])

        Button3.place(x=250, y=200)


        Button4 = tk.Button(self, text="System", font=("Arial", 80), bg=BB[0], activebackground=BB[0], width=7, fg=Color[0])

        Button4.place(x=800, y=200)
    def exit1():
        fw = "Color:" + Color[0] + " ButtonColor:" + BB[0] + " Language:" + language
        file2 = open("SystemManager.txt", "w+")
        file2.write(fw)
        file2.close()
        exit()
class Settings(tk.Frame):
    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        SLabel = tk.Label(self, text="System", font=("Aharoni Bold", 80), fg=Color[0], bg="white").place(x=325)
        SLabel2 = tk.Label(self, text="Manager", font=("Aharoni Bold", 80), fg="grey", bg="white").place(x=750)
        SLabel3 = tk.Label(self, text="Settings", font=("Aharoni Bold", 60), fg=Color[0], bg="white").place(x=600, y=150)

        SButton = tk.Button(self, text="Colour",bg=BB[0], activebackground=BB[0], fg=Color[0], font=("Arial", 50),
                        command = lambda: controller.show_frame(Colour))
        SButton.place(x=300, y=300)

        SButton2 = tk.Button(self, text="Language", font=("Arial", 50), activebackground=BB[0], fg=Color[0], bg=BB[0])
        SButton2.place(x=650, y=300)

        SReturn = tk.Button(self, text="Back", font=("Arial", 50),bg=BB[0], activebackground=BB[0], fg=Color[0],
                        command=lambda: controller.show_frame(StartMenu))
        SReturn.place(x=100, y=575)



class Colour(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        SystemLabel = tk.Label(self, text="System", font=("Aharoni Bold", 80), fg=Color[0], bg="white").place(x=325)
        ManagerLabel = tk.Label(self, text="Manager", font=("Aharoni Bold", 80), fg="grey", bg="white").place(x=750)
        CLabel = tk.Label(self, text="Colour Settings", font=("Aharoni Bold", 60), fg=Color[0], bg="white").place(x=500, y=150)
        SReturn1 = tk.Button(self, text="Back", font=("Arial", 50),bg=BB[0], activebackground=BB[0], fg=Color[0],
                        command=lambda: controller.show_frame(Settings))
        SReturn1.place(x=100, y=575)
        CheckVar1 = tk.IntVar()
        CheckVar2 = tk.IntVar()
        C1 = tk.Checkbutton(self, text = "Apply colour of text  ", activebackground="white", font=("Arial", 20), bg="white", variable = CheckVar1, height=5, width = 20)
        C1.place(x=395, y=300)
        C2 = tk.Checkbutton(self, text = "Apply color of buttons", activebackground="white", font=("Arial", 20), bg="white", variable = CheckVar2, height=5, width = 20)
        C2.place(x=400, y=400)
        ChangeCol = tk.Button(self, text="Change\nColor", fg=Color[0], bg=BB[0], activebackground=BB[0], font=("Arial", 50),
                              command=lambda: [col.remove(col[0]),
                                           col.append(colorchooser.askcolor()[1]),
                                           (Color.remove(Color[0]), Color.remove(Color[0]), Color.append(col[0])) if CheckVar1.get() == 1 else None,  (BB.remove(BB[0]), BB.remove(BB[0]), BB.append(col[0])) if CheckVar2.get() == 1 else None,
                                           self.exit2()])
        ChangeCol.place(x=800, y=350)
        self.parent = parent
    #Here is problem
    def exit2(self):
        fw = "Color:" + Color[0] + " ButtonColor:" + BB[0] + " Language:" + language
        file2 = open("SystemManager.txt", "w+")
        file2.write(fw)
        file2.close()
        os.chdir("C:\\Python34")
        with open("SM.py") as f:
            code = compile(f.read(), "SM.py", 'exec')
            exec(code)
        #Now I opened that project but I don't know how to close window from this project
        exit()
SM = Menu()
SM.mainloop()

不要为错误的缩进感到抱歉,修复它。一般来说,在Tkinter中打开其他窗口是通过。由于示例代码太长且不完整,因此很难进一步帮助您。你可能会在这个页面上发现有趣的东西