我正在尝试使用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>
基于this question的我的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。
答案 0 :(得分:4)
如果您更喜欢3.x版本(因为它可以在您的Glade文件中读取),您必须使用GObject Introspection模块中的GTK。您的修改后的代码如下。
#!/usr/bin/env python
#import gtk
from gi.repository import Gtk
class NameApp:
def __init__(self):
filename = "test.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()
在GTK的2.x版本中,窗口小部件GtkBox是抽象的,因此您无法实例化它。您可以使用GtkHBox或GtkVBox。
答案 1 :(得分:0)
您使用GTK 3.0构建了Glade文件,其中Box
是常规窗口小部件。在GTK 2中,它是一个抽象类,您必须使用VBox
或HBox
。您的Python脚本使用的是GTK 2,因此无法加载Glade文件。
转到Glade中的项目首选项并选择低于3.0的GTK版本。