Tkinter如何使用按钮隐藏当前窗口并打开新窗口

2024-06-26 10:42:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用一个tkinter按钮,当点击打开另一个窗口,并隐藏当前一个按钮里面

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()

Tags: gamefalsetitletkinterdefrootwindow按钮
1条回答
网友
1楼 · 发布于 2024-06-26 10:42:02

您可以尝试以下方法:

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()

相关问题 更多 >