Tkinter:如何在按下按钮时显示窗口

编程入门 行业动态 更新时间:2024-10-18 18:22:30
本文介绍了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:关键词]

本文发布于:2023-04-30 07:03:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按下   按钮   窗口   如何在   Tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!