我刚开始用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"><separator></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()