我正在尝试使用 python2 和 GTKBuilder 学习 GUI 编程,但是在运行代码时出现分段错误。这是我的文件,在 Glade 中作为 GTKBuilder 文件创建:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkWindow" id="mainWindow">
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_left">146</property>
<property name="margin_right">276</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entryName">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_bottom">4</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="invisible_char">●</property>
<property name="placeholder_text">Please enter your name here...</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="buttonWriteNameToFile">
<property name="label" translatable="yes">button</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<signal name="clicked" handler="buttonWriteNameToFile_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</interface>
我的python代码,基于这个问题,是这样的:
#!/usr/bin/env python
import gtk
class NameApp:
def __init__(self):
filename = "project.glade"
builder = gtk.Builder()
builder.add_from_file(filename)
builder.connect_signals(self)
builder.get_object("mainWindow").show_all()
def buttonWriteNameToFile_clicked(self, widget):
print("File write code...")
if __name__ == "__main__":
app = NameApp()
gtk.main()
运行该文件会python2
产生此错误:
name.py:9: Warning: cannot create instance of abstract (non-instantiatable) type `GtkBox'
builder.add_from_file(filename)
./geany_run_script.sh: line 5: 14897 Segmentation fault python2 "name.py"
我认为我尽可能地遵循该示例,并且在 GTKBuilder 文件之外我没有看到任何差异。但是,链接问题中的示例在我的机器上成功运行。我不知道这是否相关,但我正在运行 Arch Linux x86_64。