获取按钮小部件的文本 [英] Get the text of a button widget
本文介绍了获取按钮小部件的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从按钮获取文本,以使用if语句进行比较。
I want to get the text from a button to compare it using an if-statement.
说我有这个按钮:
my_button = Button(self, text = 'hi')
my_button.grid(row = 0, column = 0, sticky = W)
并想要执行以下操作:
my_text = my_button.text
因此,以下if语句评估为True:
So that the following if-statement evaluates as True:
if my_text == 'hi':
# do something
如何以一种简单的方式做到这一点?
How can I do this in an easy way?
推荐答案
您可以轻松地做到:
my_text = my_button['text']
Tkinter允许您以这种方式访问小部件的任何选项( height
,宽度
,文本
等)
Tkinter allows you to access any option of a widget this way (height
, width
, text
, etc.)
如果需要将此作为方法调用,则可以使用 .cget
:
If you need this as a method call, you can use .cget
:
my_text = my_button.cget('text')
请注意所有标准的Tkinter窗口小部件都可以使用此方法。
Note that this method is available on all standard Tkinter widgets.
这篇关于获取按钮小部件的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文