Get button widget text

I want to get text using a button to compare it using an if statement.

Say I have this button:

my_button = Button(self, text = 'hi') my_button.grid(row = 0, column = 0, sticky = W) 

And I want to do something like this:

 my_text = my_button.text 

So that the following if-statement evaluates to True:

 if my_text == 'hi': # do something 

How can I do this in a simple way?

+5
source share
1 answer

You can simply do:

 my_text = my_button['text'] 

Tkinter allows you to access any widget option this way ( height , width , text , etc.)


If you need this as a method call, you can use .cget :

 my_text = my_button.cget('text') 

Please note that this method is available for all standard Tkinter widgets.

+9
source

Source: https://habr.com/ru/post/1206279/


All Articles