Segmentation fault when running a python script/GTKBuilder app?
- by pythonscript
I'm trying to learn GUI programming using python2 and GTKBuilder, but I get a segmentation fault when I run the code. This is my file, created in Glade as a GTKBuilder file:
<?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>
My python code, based on this question, is this:
#!/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()
Running the file with python2 yields this error:
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"
I thought I followed that example as closely as possible, and I don't see any differences outside of the GTKBuilder file. However, the example in the linked question runs successfully on my machine. I don't know if it's relevant, but I'm running Arch Linux x86_64.