Tkinter:如何在按下按钮时显示窗口
问题描述:
我想用 Tkinter 创建一个窗口.这个窗口应该有一个按钮.按下按钮时,我希望出现第二个窗口(第一个窗口不会消失).
I want to create a window with Tkinter. This window should have a button. When the button is pressed, I want a second window to appear (without the disappearance of the first).
代码,缩短:
from Tkinter import *
from modules.startingKit.highscore import Highscore
class OptionWindow:
def __init__(self):
self.master = Tk()
self.b4 = Button(self.master, text = "display Highscores", command = self.display()).grid(row=0, sticky = W)
mainloop()
def display(self):
myWin = Toplevel()
好吧,显示了第二个窗口,但在我按下按钮之前.我可以改变这个吗
Well, the second window IS displayed, but before I press the button. Can I change this
答
command
属性接受一个函数的引用.当您执行 command=self.display()
时,您正在调用该函数并将结果传递给 command
属性.
The command
attribute takes a reference to a function. When you do command=self.display()
, you are calling the function and passing the result to the command
attribute.
解决方法是省略括号:
self.b4 = Button(..., command=self.display, ...)