本文介绍了Tkinter:如何在按下按钮时显示窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
限时送ChatGPT账号..我想用 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, ...)
这篇关于Tkinter:如何在按下按钮时显示窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论