Tkinter - 从顶层隐藏和显示主屏幕

时间:2016-04-12 16:58:43

标签: python user-interface tkinter

我正在尝试创建一个仅在登录成功时显示的主屏幕,但我无法弄清楚如何使主屏幕可见/不可见

from tkinter import *

class Login_screen(Frame):
    def __init__(self,master):
        self.open_login()

    def open_login(self):
        self.root2 = Toplevel()
        self.root2.geometry("400x200")
        self.grid_forget()
        self.app2 = Main_screen(self.root2)

class Main_screen(Frame):
    def __init__(self,master):
        Frame.__init__(self,master)
        self.master.grid()
        self.button = Button(master = self,text = "Close",command = lambda: self.close_windows())
        self.button.grid()

    def close_windows(self):
        self.master.destroy()
        Login_screen.master.grid()

root = Tk()
root.geometry("800x600")
app = Login_screen(root)
root.mainloop()

1 个答案:

答案 0 :(得分:0)

我弄清楚它是如何运作的:

from tkinter import *

class Main_screen(Frame):
    def __init__(self,master):
        Frame.__init__(self, master)
        self.grid()
        self.text = Label(text="Janela")
        self.text.grid()
        root.withdraw()
        self.create_login()

    def create_login(self):
        self.root2 = Toplevel()
        self.app2 = Login_screen(self.root2)

class Login_screen(Frame):
    def __init__(self,master):
        Frame.__init__(self,master)
        self.grid()
        self.botao1 = Button(self,text="Appear",command = lambda: self.show_main())
        self.botao1.grid()

    def show_main(self):
        self.master.destroy()
        root.deiconify()


root = Tk()
app = Main_screen(root)
root.mainloop()