0

I'm trying to use two windows with Tkinter: One welcome window appears, and when a button is clicked, another window opens (and the welcome window closes). However, currently the first window doesn't show up so the program can't verify the condition and move on to the second one. (It shows up if the condition isn't there, but in which case both windows appear at the same time)

welWindow=New_Toplevel_1(Tk())
#wait until welWindow.getGo()==1
while welWindow.getGo()!=1:
    time.sleep(1)
#my variables get values from welWindow's variables
welWindow.destroy()
labWindow=(Tk())
labWindow.mainloop()

The destroy function in my New_Toplevel_1 class:

def destroy(self):
    self.top.destroy()

(top is the Tk() used in the constructor)

As you have probably guessed I am very new to this, all tips/recommendations are welcome :)

Jessica Chambers
  • 1,246
  • 5
  • 28
  • 56
  • 1
    When running the code without the `New_Toplevel_1` (is this your own class?) and assigning Tk() to welWindow, this code only displays one window. So maybe the New_Toplevel_1 is not implementing the destroy properly? – Roars Mar 01 '18 at 11:19
  • @Roars I added my destroy function.... It's homemade so maybe that's not what it should look like? – Jessica Chambers Mar 01 '18 at 11:29
  • 1
    If you are simply overriding the destroy you could put in `super(New_Toplevel_1, self).destroy()` which should use the parents destroy method (You could run your own code before and after this call). I would also suggest looking at @progmatico 's answer as they are right about having one instance of Tk. – Roars Mar 01 '18 at 15:39
  • 1
    @JessicaChambers I edited my answer, take a look at the thread I mention in the end. – progmatico Mar 02 '18 at 15:09

1 Answers1

2

While I am not telling that your code has to resemble the code below, because the same behaviour can be written in some different ways and styles, it will do what you want.

I see two or three fundamental errors in your code.

You are passing the main root window as a parameter by calling Tk(). That is wrong because there should be only one Tk instance, made with a TK() call in your tkinter program. Give it a name such as root and use that reference from them on.

Secondly, you don't see anything because you sleep forever without ever calling mainloop(), which you should, otherwise your program will not update the UI, nor it will respond to events.

mainloop is the tkinter's eventloop for the Tk instance. So setup your complete UI with all the widgets and make sure the code reaches and ending statement calling root.mainloop().

Also you usually don't need to call sleep() and that is a blocking function. Any blocking function would also block the mainloop, inhibiting updates and event reception, until it returns.

Now follows some code

from tkinter import ttk, Tk, Toplevel

root = Tk()
welcome_window = Toplevel(root)
welcome_window.title('Welcome')

lab_window = Toplevel(root)
lab_window.title('Lab')

root.withdraw() # hide root window
lab_window.withdraw() # hide lab window

def goto_lab():
    welcome_window.destroy()
    lab_window.deiconify() # show lab window

button1 = ttk.Button(welcome_window, text='Close',\
                     command=goto_lab)
button1.pack(padx=100, pady=50)

button2 = ttk.Button(lab_window, text='Close',\
                     command=quit)
button2.pack(padx=100, pady=50)

root.mainloop()

About giving some structure to your GUI code, as you are starting to learn tkinter, take a look at this thread

progmatico
  • 4,714
  • 1
  • 16
  • 27