在 Tkinter (Python) 中禁用按钮 [英] Disable button in Tkinter (Python)

查看:74
本文介绍了在 Tkinter (Python) 中禁用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题要问

我只想在启动程序时禁用该按钮

I just want to disable the button when i start my program

在附加的图片中,按钮似乎已经被禁用,但它对我的点击事件或键盘事件的响应

in attached image, it looks like the button is already disabled ,but its response to my click event or keyboard event

我该怎么办?

谢谢大家的回答

from Tkinter import *


def printSomething(event):
    print("Print")

#Start GUI
gui = Tk()
gui.geometry("800x500")
gui.title("Button Test")

mButton = Button(text="[a] Print",fg="#000",state="disabled")

mButton.place(x=5,y=10)

mButton.bind('<Button-1>',printSomething)
gui.bind('a',printSomething)

gui.mainloop()

推荐答案

您需要解除绑定该事件.state="disabled"/state=DISABLED 使按钮 disabled 但它不会 unbind 事件.你需要unbind对应的events来实现这个目标.如果您想再次启用该按钮,则需要再次bind 事件.喜欢:

You need to unbind the event. state="disabled"/state=DISABLED makes button disabled but it doesn't unbind the event. You need to unbind the corresponding events to achieve this goal. If you want to enable the button again then you need to bind the event again. Like:

from Tkinter import *

def printSomething(event):
    print("Print")

#Start GUI
gui = Tk()
gui.geometry("800x500")
gui.title("Button Test")

mButton = Button(text="[a] Print",fg="#000",state="disabled")

mButton.place(x=5,y=10)

mButton.bind('<Button-1>',printSomething)
mButton.unbind("<Button-1>") #new line added
gui.bind('a',printSomething)

gui.mainloop()

这篇关于在 Tkinter (Python) 中禁用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆