简体   繁体   中英

Tkinter how to use a button to hide current window and open new one

Im trying to use a tkinter button that when clicked opens another window and hides the current one with the button inside.

def game():
    window = tk.Toplevel()
    window.geometry("1280x720")

root = tk.Tk()
root.title('testgame')
root.resizable(False,False)
root.geometry("500x500")
pbutton = tk.Button(root, text='Play', width=25, command=game and root.withdraw).place(relx = 0.5,rely = 0.5, anchor = 'center')

root.mainloop()

You can try something like this:

import tkinter as tk

root = tk.Tk()

#In order to hide main window
root.withdraw()

tk.Label(root, text="Main Window").pack()

aWindow = tk.Toplevel(root)

def change_window():
    #remove the other window entirely
    aWindow.destroy()

    #make root visible again
    root.iconify()
    root.deiconify()

tk.Button(aWindow, text="This is aWindow", command=change_window).pack()

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM