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

时间:2015-05-01 11:13:20

标签: python tkinter python-3.4

几天前,我开始使用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()

0 个答案:

没有答案