Python 我如何关闭tkinter?

Python 我如何关闭tkinter?,python,tkinter,Python,Tkinter,不用担心这个变量,我只是想知道为什么我不能关闭tkinter来创建一个新的 def trainup(): global newbw global mg global pmg mg=mg+1 newbw.destroy() pmg=str(mg) print("you have leveled up your money gain your new money gain will be:" + pmg) train() def tr

不用担心这个变量,我只是想知道为什么我不能关闭tkinter来创建一个新的

def trainup():
    global newbw
    global mg
    global pmg
    mg=mg+1
    newbw.destroy()
    pmg=str(mg)
    print("you have leveled up your money gain your new money gain will be:" + pmg)
    train()

def train():
    import tkinter
    print("welcome to the training area")
    print("click the button to train")
    newbw=tkinter.Tk()
    b=tkinter.Button(newbw, text="train", command=trainup)
    b.pack()
    newbw.mainloop()

代码中有几个问题需要解决

  • 导入tkinter
    不应在函数中。所有导入都应该位于代码的顶部。现在,每次调用
    train()
    函数时,您都要导入tkinter

  • 您的主要问题是,为什么不能销毁根窗口。您的
    newbw
    tk实例是在
    train()
    中本地定义的,因此该函数之外的任何内容都看不到该变量。您至少需要将
    global newbw
    添加到
    train()
    函数中。尽管最好的做法是将根窗口移动到全局命名空间中,或者将其作为类编写

  • mg
    在您的代码中不存在,因此在本例中这将导致错误


  • 您确实不应该删除根窗口并重新创建它。相反,您应该在根窗口或第二个顶级窗口中管理一个框架

  • 使此代码正常工作所需的最小更改:

    import tkinter
    
    
    def trainup():
        global newbw, mg, pmg
        # mg=mg+1
        newbw.destroy()
        # pmg = str(mg)
        print("you have leveled up your money gain your new money gain will be:")  # + pmg)
        train()
    
    
    def train():
        global newbw
        print("welcome to the training area")
        print("click the button to train")
        newbw = tkinter.Tk()
        b = tkinter.Button(newbw, text="train", command=trainup)
        b.pack()
        newbw.mainloop()
    
    train()
    
    更好的解决方案可能是更新标签,而不是每次都尝试重新创建窗口

    大概是这样的:

    import tkinter as tk
    
    
    def trainup():
        global mg, pmg
        mg += 1
        pmg = str(mg)
        status_label.config(text="you have leveled up your money gain your new money gain will be:" + pmg)
    
    
    newbw = tk.Tk()
    mg = 0
    pmg = 0
    tk.Label(newbw, text="welcome to the training area\nclick the button to train").pack()
    status_label = tk.Label(newbw, text='')
    status_label.pack()
    tk.Button(newbw, text="train", command=trainup).pack()
    newbw.mainloop()
    
    下面是一个例子,我们更新一个包含小部件的框架。在这里,这并不是一个更好的选择,但是这个例子在今后应该是有用的

    import tkinter as tk
    
    
    def trainup():
        global mg, pmg
        mg += 1
        pmg = str(mg)
        status_label.config(text="you have leveled up your money gain your new money gain will be:" + pmg)
    
    
    def update_frame():
        global frame, status_label
        if frame is not None:
            frame.destroy()
        frame = tk.Frame(newbw)
        frame.pack()
        status_label = tk.Label(frame, text='')
        status_label.pack()
    
    
    newbw = tk.Tk()
    mg = 0
    pmg = 0
    frame = None
    update_frame()
    
    tk.Label(newbw, text="welcome to the training area\nclick the button to train").pack()
    tk.Button(newbw, text="train", command=trainup).pack()
    newbw.mainloop()
    

    newbw
    train()
    中定义,因此
    trainup()
    无法查看
    newbw
    。您需要
    train()
    中的
    global newbw
    ,此代码才能工作。也就是说,这个设置有一些问题,你应该改变。谢谢你,它工作得很好。现在我要做的就是发布。另外,一般认为最好避免使用这样的全局变量。最好将它们作为参数传递给函数。这样做有助于避免此类问题。当你写更多的代码时,这些类型的问题会以更复杂的方式出现。我只是惊讶于你写这篇文章的速度让我觉得有点愚蠢,因为我才刚刚了解tkinter所以谢谢you@arandomdosfile别觉得自己愚蠢。在过去两年的大部分时间里,我都在工作场所学习和专业地使用tkinter。我恰好有时间在一个简单的界面上快速做出判断和纠正。如果你坚持使用它,你也可以达到这一点。你真的不应该删除根窗口并重新创建它。相反,您应该在根窗口或第二个顶层窗口中管理一个框架。为此,我不明白您的意思,我看到您使用了一些类似于b=tk.button(newbw,text=5)的东西,并在代码b=tk.button(newbw,text=4)中简单地更改了它