Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check the type of tkinter-widget-objects

I wanna create a simple GUI for a research project.
For this I have plenty of widgets and I want to check what their type is.
Lets take the entry-widget as an example. I have entries, which look like this:

entry_modelling_script_folder = tk.Entry(self, width=40)
entry_modelling_script_folder.grid(row=6,column=0)

Now I want to check wether a certain widget is an entry:

widget_type = type(current_widget)

which returns:
<class ‘tkinter.Entry’>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Now – how do I write the if-condition?
I only came up with stuff, that is not working:

if widget_type == '<class \'tkinter.Entry\'>':
if widget_type == 'tkinter.Entry':
...

I would really appreciate any help =)

(I know, there is a method .winfo_class() too, but this didnt work in the first step, so I chose type(…))

>Solution :

Use isinstance:

if isinstance(current_widget, tk.Entry):
    ...

Or

if widget_type == 'tkinter.Entry':
    ...

I prefer the first solution.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading