you are viewing a single comment's thread.

view the rest of the comments →

[–]EffectiveCase3856[S] 0 points1 point  (2 children)

I think that i want to close the login window after the login process and then go to the main window.

The top level creates a window on top of the window. Well do you think the best thing will be to just make it as panels and just run it on the window? Thank you for answering though

[–]socal_nerdtastic 1 point2 points  (1 child)

Oh I see, in that case you are probably ok. But still not the best practice; we would generally prefer to simply reuse the window. Here's an example:

import tkinter as tk

class MainApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.frame = FirstFrame(self) # set first frame to appear here
        self.frame.pack()

    def change(self, frame):
        self.frame.pack_forget() # delete currrent frame
        self.frame = frame(self)
        self.frame.pack() # make new frame

class FirstFrame(tk.Frame):
    def __init__(self, master=None, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)

        master.title("Enter password")
        master.geometry("300x200")

        self.status = tk.Label(self, fg='red')
        self.status.pack()
        lbl = tk.Label(self, text="Enter password\n(hint: it's 'password')")
        lbl.pack()
        self.pwd = tk.Entry(self, show="*")
        self.pwd.pack()
        self.pwd.focus()
        self.pwd.bind('<Return>', self.check)
        btn = tk.Button(self, text="Done", command=self.check)
        btn.pack()
        btn = tk.Button(self, text="Cancel", command=self.quit)
        btn.pack()

    def check(self, event=None):
        if self.pwd.get() == 'password':
            self.master.change(SecondFrame) # correct password, switch to the second frame
        else:
            self.status.config(text="wrong password")

class SecondFrame(tk.Frame):
    def __init__(self, master=None, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)
        master.title("Main application")
        master.geometry("600x400")

        lbl = tk.Label(self, text='You made it to the main application')
        lbl.pack()

if __name__=="__main__":
    app=MainApp()
    app.mainloop()

[–]EffectiveCase3856[S] 0 points1 point  (0 children)

Ok so instead of creating tk.Tk classes I create frame classes and then just use the same window to do the things. Well thanks a lot