Find type of GTK widgets

北慕城南 提交于 2021-02-18 19:40:14

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!