在tkinter GUI上我想在画布上打印不同的消息,具体取决于我悬停在按钮上的状态。如果按钮本身是DISABLED,我想在画布上显示另一条消息,而不是按钮是NORMAL。 我有这个(剥离的)相关代码:
from tkinter import *
class app:
def __init__(self):
self.window = Tk()
self.button = Button(self.window,text="Button",command=self.someCommand,state=DISABLED)
self.button.bind("<Enter>", self.showText)
self.button.bind("<Leave>", self.hideText)
self.window.mainloop()
def showText(self):
if self.button["state"] == DISABLED:
#print this text on a canvas
else:
#print that text on a canvas
def hideText(self):
#remove text
def main()
instance = app()
main()
这总是在画布上绘制“那个文本”,而不是“此文本”
我也尝试了以下内容:
self.button['state']
== 'disabled'
== 'DISABLED'
如果我打印:
print(self.button["state"] == DISABLED)
它给了我:
False
使用以下方式更改状态:
self.button["state"] = NORMAL
按照我的预期工作。
我在这里已经阅读了一些主题,但似乎没有人回答为什么if语句不起作用的问题。
答案 0 :(得分:5)
print(self.button['state'])
打印:
disabled
所以我可以使用:
state = str(self.button['state'])
if state == 'disabled':
#print the correct text!