gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:01:23 24 4
gpt4 key购买 nike

我正在尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com