python - 运行 python 脚本/GTKBuilder 应用程序时出现段错误?

标签 python segmentation-fault pygtk

我正在尝试使用 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代码,基于this question ,是这样的:

#!/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。

最佳答案

如果您更喜欢 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。

关于python - 运行 python 脚本/GTKBuilder 应用程序时出现段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11060467/

相关文章:

c++ - 设置跳转/长跳转 : Why is this throwing a segfault?

c - 更改大页分配的大小时由于initialize()函数导致的段错误

c++ - 为什么虚函数会出现段错误

gtk - 使用 GTK+ 或 PyGTK 转储 X 剪贴板数据

pygtk - Twisted/PyGTK 的简单非网络示例

python - 无法在 Jupyter Notebook 中导入 nltk 模块?

Python:通过引用和切片赋值传递

带有 CSS 的 python gtk webkit,宽度问题

python - 无法匹配 Django 中 URL.conf 中的 URL?

Python:如何合并显示为句子和显示行号