Jor*_*ink 3 python bind canvas tkinter button
在tkinter GUI上,我想根据悬停的按钮状态在画布上打印不同的消息。如果按钮本身是禁用的,则我想在画布上显示比按钮为“正常”时的其他消息。我有这个(剥离)相关代码:
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()
Run Code Online (Sandbox Code Playgroud)
这总是在画布上绘制“该文本”,而不是“此文本”
我也尝试了以下方法:
self.button['state']
== 'disabled'
== 'DISABLED'
Run Code Online (Sandbox Code Playgroud)
如果我打印:
print(self.button["state"] == DISABLED)
Run Code Online (Sandbox Code Playgroud)
它给了我:
False
Run Code Online (Sandbox Code Playgroud)
使用以下方法更改状态:
self.button["state"] = NORMAL
Run Code Online (Sandbox Code Playgroud)
如我所料。
我在这里已经阅读了几个主题,但是似乎没有一个问题可以回答为什么if语句不起作用的问题。
经过更多研究后,我终于找到了解决方案。
print(self.button['state'])
Run Code Online (Sandbox Code Playgroud)
印刷品:
disabled
Run Code Online (Sandbox Code Playgroud)
所以我可以使用:
state = str(self.button['state'])
if state == 'disabled':
#print the correct text!
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7800 次 |
| 最近记录: |