Tkinter:按下按钮时如何使窗口出现

时间:2013-03-22 21:49:02

标签: python window tkinter

我想用Tkinter创建一个窗口。这个窗口应该有一个按钮。 按下按钮时,我想要出现第二个窗口(没有第一个窗口消失)。

代码缩短了:

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

嗯,显示第二个窗口,但在按下按钮之前。我可以改变吗

1 个答案:

答案 0 :(得分:6)

command属性将引用带到函数中。当您执行command=self.display()时,您调用该函数并将结果传递给command属性。

修复是省略括号:

self.b4 = Button(..., command=self.display, ...)