gpt4 book ai didi

python - 如何使用python代码修改用glade创建的GUI

转载 作者:太空宇宙 更新时间:2023-11-03 21:11:57 26 4
gpt4 key购买 nike

我刚刚开始用python学习Glade,我创建了一个简单的glade文件,我添加了几个按钮,我还添加了一个添加按钮,我希望添加按钮向“mainBox”对象添加一个新按钮,即“window1”的子级,我正在使用下面给出的 python 代码,但它不起作用,因为它只打印完成但不向 GUI 添加任何按钮,我做错了什么?谢谢

  <?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="default_width">800</property>
<property name="default_height">800</property>
<signal name="destroy" handler="on_destroy_clicked" swapped="no"/>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkToolbar" id="toolbar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuToolButton">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="is_important">True</property>
<property name="label" translatable="yes">Back</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-go-back</property>
<signal name="show-menu" handler="on_back_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkMenuToolButton">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="is_important">True</property>
<property name="label" translatable="yes">Forward</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-sort-descending</property>
<signal name="show-menu" handler="on_forward_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkSeparatorToolItem">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">&lt;separator&gt;</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">Refresh</property>
<property name="is_important">True</property>
<property name="label" translatable="yes">Refresh</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-convert</property>
<signal name="clicked" handler="on_refresh_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">Home</property>
<property name="is_important">True</property>
<property name="label" translatable="yes">Home</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-disconnect</property>
<signal name="clicked" handler="on_home_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="is_important">True</property>
<property name="label" translatable="yes">Add</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-add</property>
<signal name="clicked" handler="on_add_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkSeparatorToolItem">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">Delete</property>
<property name="is_important">True</property>
<property name="label" translatable="yes">Delete</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-dialog-error</property>
<signal name="clicked" handler="on_delete_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="is_important">True</property>
<property name="label" translatable="yes">Information</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-justify-fill</property>
<signal name="clicked" handler="on_information_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</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="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_bottom">1</property>
<property name="spacing">2</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Current Location</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_right">1</property>
<property name="input_purpose">url</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-execute</property>
<property name="icon_size">3</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkBox" id="boxMain">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="homogeneous">True</property>
<child>
<object class="GtkButton">
<property name="label" translatable="yes">button1</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton">
<property name="label" translatable="yes">button2</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</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>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

所以,我想要使用添加按钮做的事情是在 boxMain 对象中创建另一个按钮,所以我尝试了这段代码

import gi
gi.require_version('Gtk','3.0')
from gi.repository import Gtk

builder = Gtk.Builder()
builder.add_from_file("test.glade")
box = builder.get_object("boxMain")


class Handler:
def on_back_clicked(self, button):
pass
def on_destroy_clicked(self, button):
Gtk.main_quit()
def on_forward_clicked(self, button):
pass
def on_refresh_clicked(self, button):
pass
def on_home_clicked(self, button):
pass
def on_information_clicked(self, button):
pass
def on_delete_clicked(self, button):
pass
def on_add_clicked(self, button):
button1 = Gtk.Button()
button1.set_label("Button")
box.pack_start(button1,True,True,0)
print("done")


builder.connect_signals(Handler())
window = builder.get_object("window1")

window.show_all()

Gtk.main()

但是这段代码只是打印完成,没有向 GUI 添加任何按钮,有人可以帮忙吗?谢谢

最佳答案

您没有使用 button1.show() 显示按钮。

顺便说一句,编写代码的更好方法是:

import gi
gi.require_version('Gtk','3.0')
from gi.repository import Gtk




class GUI:
def __init__ (self):

self.builder = Gtk.Builder()
self.builder.add_from_file("test.glade")
self.builder.connect_signals(self)
window = self.builder.get_object("window1")
window.show_all()

def on_back_clicked(self, button):
pass
def on_destroy_clicked(self, button):
Gtk.main_quit()
def on_forward_clicked(self, button):
pass
def on_refresh_clicked(self, button):
pass
def on_home_clicked(self, button):
pass
def on_information_clicked(self, button):
pass
def on_delete_clicked(self, button):
pass
def on_add_clicked(self, button):
box = self.builder.get_object("boxMain")
button1 = Gtk.Button()
button1.set_label("Button")
box.pack_start(button1,True,True,0)
button1.show()
print("done")


GUI()
Gtk.main()

关于python - 如何使用python代码修改用glade创建的GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55005463/

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