问题
I'am working on GTK in python after creating a CheckButton or Button with:
x = Gtk.CheckButton()
y = Gtk.Button("Config")
I want to check type of x
or y
When use type() it returns
<class 'gi.repository.Gtk.CheckButton'>
or
<class 'gi.overrides.Gtk.Button'>
Is it possible to check It's type in another way ?
回答1:
The idiomatic way to check if a variable is an instance of a specific type is using isinstance()
, e.g.:
>>> import gi
>>> gi.require_version('Gtk', '3.0')
>>> from gi.repository import Gtk
>>> cb = Gtk.CheckButton()
>>> if isinstance(cb, Gtk.Button):
... print('Yes')
...
Yes
>>> if isinstance(cb, Gtk.CheckButton):
... print('Yes')
...
Yes
>>> if isinstance(cb, Gtk.RadioButton):
... print('Yes')
... else:
... print('No')
...
No
>>>
来源:https://stackoverflow.com/questions/60112777/find-type-of-gtk-widgets