查找GTK小部件的类型 [英] Find type of GTK widgets

查看:56
本文介绍了查找GTK小部件的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下命令创建CheckButton或Button后,我正在使用python开发GTK:

I'am working on GTK in python after creating a CheckButton or Button with:

x = Gtk.CheckButton()
y = Gtk.Button("Config")

我要检查 x y 的类型使用type()时返回

I want to check type of x or y When use type() it returns

<class 'gi.repository.Gtk.CheckButton'>

<class 'gi.overrides.Gtk.Button'>

是否可以通过其他方式检查其类型?

Is it possible to check It's type in another way ?

推荐答案

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

这篇关于查找GTK小部件的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆