I am currently working on a program which handles a number of different possible entry widgets in a Python program.
I need some code to be able to determine what type of widget a particular object is, for example Entry
or Checkbutton
.
I have tried using the type(var)
method to no avail (I get the error missing required variable self
) as well as the var.__class__
and I am making no progress.
for d in dataTypes:
if isinstance(d, Entry):
print("Found Entry!")
elif type(d).__name__ == 'Checkbutton':
print("Found Checkbox!")
Does anyone have any idea of how I can solve this?
If you need the name as a string, you can use the
.winfo_class()
method:Or, you could access the
__name__
attribute:That said, using
isinstance
is a more common/pythonic approach:Also, your current code is failing because
type(d).__name__
does not return what you think it does:Notice that it returns the name of the type object returned by
type
, not the name ofCheckbutton
.