Ricardo Altamirano
Ricardo Altamirano

Reputation: 15198

Segmentation fault when running a python script/GTKBuilder app?

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.

Upvotes: 2

Views: 1767

Answers (2)

Szil&#225;rd Pfeiffer
Szil&#225;rd Pfeiffer

Reputation: 1646

If you prefer 3.x version (as it can be read in your Glade file) you have to use GTK from GObject Introspection module. The modified code of yours is the following.

#!/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()

In the 2.x versions of GTK the widget GtkBox was abstract, so you cannot instantiate it. You can use GtkHBox or GtkVBox.

Upvotes: 4

ptomato
ptomato

Reputation: 57880

You built your Glade file using GTK 3.0, in which Box is a regular widget. In GTK 2, it's an abstract class and you have to use either VBox or HBox. Your Python script is using GTK 2, so it can't load the Glade file.

Go to the project preferences in Glade and select a GTK version lower than 3.0.

Upvotes: 0

Related Questions