Python Tkinter-如何通过单击按钮打开窗口?

Python Tkinter-如何通过单击按钮打开窗口?,python,tkinter,Python,Tkinter,在我尝试使用EasyGUI制作一个游戏后,我发现它不会做一些对游戏很重要的事情,所以我开始使用Tkinter。然而,我遇到了另一个问题,我不知道如何解决。代码如下: money = Button(box2, text = "Get Money", highlightbackground = "yellow", command = close_parentg) def moneywindow(): parent.destroy() # The button is inside the p

在我尝试使用EasyGUI制作一个游戏后,我发现它不会做一些对游戏很重要的事情,所以我开始使用Tkinter。然而,我遇到了另一个问题,我不知道如何解决。代码如下:

money = Button(box2, text = "Get Money", highlightbackground = "yellow", command = close_parentg)

def moneywindow():
    parent.destroy() # The button is inside the parent.
    Money.mainloop() # This is the window I want to open.
destroy()命令工作正常,因为当我按下按钮时,第一个窗口关闭,但是如果我运行程序,第二个窗口会弹出,即使我没有告诉它(或者至少我认为我没有)

如何阻止第二个窗口在开始时弹出,并且仅在单击按钮时显示?

mainloop()
不是创建窗口的原因。创建窗口的唯一两种方式是:在程序开始时创建
Tk
的实例,以及在程序运行时创建
Toplevel
的实例

您的GUI应该只调用
mainloop()
一次,并且应该在应用程序的生命周期中保持运行。它将在您销毁根窗口时退出,tkinter的设计使您销毁根窗口时,程序退出

一旦您这样做,除非您使用
Toplevel
显式创建窗口,否则不会弹出任何窗口

下面的示例允许您创建多个窗口,并使用
lambda
为每个窗口提供一个破坏自身的按钮

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        new_win_button = tk.Button(self, text="Create new window", 
                                   command=self.new_window)
        new_win_button.pack(side="top", padx=20, pady=20)

    def new_window(self):
        top = tk.Toplevel(self)
        label = tk.Label(top, text="Hello, world")
        b = tk.Button(top, text="Destroy me", 
                      command=lambda win=top: win.destroy())
        label.pack(side="top", fill="both", expand=True, padx=20, pady=20)
        b.pack(side="bottom")

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

您需要显示更多的代码,您能提供一个复制您的问题的代码吗?如果我运行该程序,您的意思是什么?程序不是已经在运行了吗?