0

How can I manage to automatic cancel this 1st window when I click the next window button?

sample code:

from tkinter import *
from tkinter import messagebox

root = Tk()
root.title("GUI practice")

def open():
    top = Toplevel()  # new window
    top.title("Kokomi")
    labels = Label(top, text="This one automatically close when i click the next window").pack()
    button2 = Button(top,text="Close window", command=top.destroy).pack()
    button3 = Button(top,text="Next window", command=open2).pack()

def open2():
    top = Toplevel()  # new window
    top.title("Guide")
    labels = Label(top, text="end").pack()
    button2 = Button(top, text="Close window", command=top.destroy).pack()  # destroy to quit things

button = Button(root, text="Open(No need to close this)", command=open).pack()


root.mainloop()

[Click open][1]
[Click Next window and after that this windows should disappear and continue to the 3rd picture][2]
[The 2nd picture goes disappear when i click the next window][3]

  [1]: https://i.stack.imgur.com/plS1T.png
  [2]: https://i.stack.imgur.com/EFW76.png
  [3]: https://i.stack.imgur.com/xSZCp.png
ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • Do you need two `open` functions? – Delrius Euphoria Nov 20 '21 at 10:19
  • yes sir... im trying to make the other one disappear when i click the next window.. but two command doesn't work –  Nov 20 '21 at 10:26
  • 1
    I feel like there is some more issues here, can you make your objectives more clear. Why are you opening two windows – Delrius Euphoria Nov 20 '21 at 10:32
  • I am trying to make a map locator GUI, a project of mine in college.. I need multiple windows like around 15.. –  Nov 20 '21 at 10:35
  • So are you going to make 15 functions? I can give you an answer for this 2 functions, but if the number of functions are going to increase, then you might need a better solution, which is why I asked you to explain your case better. Are these 15 windows going to be similar? or some pattern in these windows – Delrius Euphoria Nov 20 '21 at 10:45
  • The only similar im going to make is the next window and the close window... I'm sorry i'm quite new to this.. i can't really explain it well... I don't know if sending pictures here is okay.. I can explain it well by pictures –  Nov 20 '21 at 10:51
  • Yes you can use pictures – Delrius Euphoria Nov 20 '21 at 11:01
  • [Click open][1] [Click Next window and after that this windows should disappear and continue to the 3rd picture][2] [The 2nd picture goes disappear when i click the next window][3] [1]: https://i.stack.imgur.com/plS1T.png [2]: https://i.stack.imgur.com/EFW76.png [3]: https://i.stack.imgur.com/xSZCp.png –  Nov 20 '21 at 11:15
  • @KurtLee dont use names like `open` that are already reserved for built-in operations. You will overwrite these names and might lose functionality that you want to use. – Thingamabobs Nov 20 '21 at 12:11

1 Answers1

2

For this specific case of using two functions and two windows, you can just simply rename the Toplevel widgets to different names and then globalize one and then access it from another function and destroy it before the new windows is shown.

def open():
    global top
    top = Toplevel()  # new window
    top.title("Kokomi")

    Label(top, text="This one automatically close when i click the next window").pack()
    Button(top, text="Close window", command=top.destroy).pack()
    Button(top, text="Next window", command=open2).pack()

def open2():
    top.destroy() # Destroy previously open window
    top1 = Toplevel()  # new window
    top1.title("Guide")

    Label(top1, text="end").pack()
    Button(top1, text="Close window", command=top1.destroy).pack()  # destroy to quit things

If you noticed, I removed the variable names of buttons and labels because its useless to have those as their value is None, read Tkinter: AttributeError: NoneType object has no attribute <attribute name>.

When you wish to use more functions and windows, you have to manually follow this procedure for all the functions. Unless ofcourse there is a better and cleaner way of designing your app using classes and frames.

Alternatively you can also invoke two functions from a single button, this method will get rid of globalization and renaming and might be a bit better than the above mentioned solution:

def open():
    top = Toplevel()  # new window
    top.title("Kokomi")

    Label(top, text="This one automatically close when i click the next window").pack()
    Button(top, text="Close window", command=top.destroy).pack()
    Button(top, text="Next window", command=lambda: [top.destroy(),open2()]).pack()

def open2():
    top = Toplevel()  # new window
    top.title("Guide")

    Label(top, text="end").pack()
    Button(top, text="Close window", command=top.destroy).pack()  # destroy to quit things
Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46