pythonif-statementtkintertkinter-entry

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'>

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.