尽管没有出现任何错误,但我的 Tkinter GUI 无法运行.为什么



我正在尝试创建一个 GUI,在按下按钮后打开一个新窗口,同时销毁最后一个窗口。我没有收到任何错误,但是当我运行程序时没有任何结果。

from Tkinter import *
def team():
    def table():
        table = Toplevel(contributers)
        contributers.destroy()
    def contributers():
        contributers = Toplevel(table)
        table.destroy()
    def firstpage():
        firstpage = Toplevel(letsbegin)
        letsbegin.destroy()
    def secondpage():
        secondpage = Toplevel(firstpage)
        firstpage.destroy()
    def leave():
        exit()
    root = Tk()
    root.title("Team Blue")
    label1 = label(menu, text="Team Blue", bg = "Yellow", fg="Black")
    button1 = button(menu, text="ENTER", width=15, bg="yellow", fg="Black", command =contributers)
    button2 = button(menu, text="Exit", bg="red", fg="white", command=leave)
    root.mainloop()

我只想运行这段代码

你在评论中提到了很多错误。


如果要关闭一个窗口并打开新窗口,请销毁第一个窗口 - root.destroy() - 然后再次使用Tk()创建新窗口并再次使用mainloop()

我将新窗口分配给全局变量root这样我就可以使用几乎相同的代码关闭第二个窗口并打开第三个窗口。

我使用global root所以变量root不是局部变量,但它是全局的,我可以在其他函数中访问(分配给root的窗口(。

from Tkinter import *
# --- functions ---
def open_first_window():
    global root
    root = Tk()
    label1 = Label(root, text="Team Brake 'Em")
    label1.pack()
    button1 = Button(root, text="Open Second Window", command=open_second_window)
    button1.pack()
    button2 = Button(root, text="Exit", command=root.destroy)
    button2.pack()
    root.mainloop()
def open_second_window():
    global root
    root.destroy()
    root = Tk()
    label1 = Label(root, text="Second Window")
    label1.pack()
    button1 = Button(root, text="Open Third Window", command=open_third_window)
    button1.pack()
    button2 = Button(root, text="Exit", command=root.destroy)
    button2.pack()
    root.mainloop()
def open_third_window():
    global root
    root.destroy()
    root = Tk()
    label1 = Label(root, text="Third Window")
    label1.pack()
    button2 = Button(root, text="Exit", command=root.destroy)
    button2.pack()
    root.mainloop()
# --- main ---
open_first_window() 

还有其他流行的方法 - 不要设计窗口,而是删除所有小部件并放置新的小部件。小部件Frame可能很有用,因为您可以将所有小部件放在Frame中,Frame放入窗口中,稍后您只需删除Frame并使用新小部件放置新Frame

from Tkinter import *
# --- function ---
def create_first_frame():
    global root
    global frame
    #frame.destroy()
    frame = Frame()
    frame.pack()
    label1 = Label(frame, text="Team Brake 'Em")
    label1.pack()
    button1 = Button(frame, text="Open Second Window", command=create_second_frame)
    button1.pack()
    button2 = Button(frame, text="Exit", command=root.destroy)
    button2.pack()
def create_second_frame():
    global root
    global frame
    frame.destroy()
    frame = Frame()
    frame.pack()
    label1 = Label(frame, text="Second Window")
    label1.pack()
    button1 = Button(frame, text="Open Third Window", command=create_third_frame)
    button1.pack()
    button2 = Button(frame, text="Exit", command=root.destroy)
    button2.pack()
def create_third_frame():
    global root
    global frame
    frame.destroy()
    frame = Frame()
    frame.pack()
    label1 = Label(frame, text="Third Window")
    label1.pack()
    button2 = Button(frame, text="Exit", command=root.destroy)
    button2.pack()
# --- main ---
root = Tk()
create_first_frame()
root.mainloop()

这是因为当您将整个代码包装在功能名称 team(( 中时。

因此,您必须在适当的位置调用该方法才能运行该程序。

请确保标签功能的字母大小写写为 Label((,按钮(( 也是 Button((。

而且您必须使用root代替菜单,然后希望您看到窗口。

根据内容打包。

最新更新