问题描述
我正在使用3.5.2
版本将Python GTK应用程序从python2移植到python3。在此过程中,我将切换到更新的GTK
API调用。我需要调用gtk.TextBuffer
构造函数,在[here] [1]中进行了描述。根据该文档,我写道:
tb = gtk.TextBuffer(table=text_tag_table)
这会出现以下错误:
*** TypeError: gobject `GtkTextBuffer' doesn't support property 'table'
我尝试删除通话的table=
部分:
gtk.TextBuffer(table=text_tag_table)
这给出了:
*** TypeError: GObject.__init__() takes exactly 0 arguments (1 given)
我可以调用不带参数的构造函数,例如tb = gtk.TextBuffer()
。我尝试手动设置标签表,如下所示:
tb.set_property("tag-table",text_tag_table)
这会发出警告:
__main__:1: Warning: g_object_set_property: construct property "tag-table" for object 'GtkTextBuffer' can't be set after construction
似乎带有table=
arg的原始构造函数应该可以工作。谁能帮我弄清楚为什么会抛出TypeError
吗?我确实使用pydb
确认text_tag_table
是正确类型的对象:
(Pdb) p text_tag_table
<Gtk.TextTagTable object at 0x7fb723d6b288 (GtkTextTagTable at 0x2b2e8e0)>
非常感谢!
解决方法
此问题的解决方法是使用new
方法:
tb = gtk.TextBuffer.new(table=text_tag_table)
我对此感到惊讶,但看起来还不错!