我是Python和OOP的新手,需要一个示例脚本来理解gtk.builder对象和窗口对象之间的关系。我正在使用gnome-builder来开始。
我想要的是从xml中加载由builder (或Glade)生成的gui定义:实际上很简单:
窗口有一个按钮和一个标签。单击按钮时,标签将切换为显示或隐藏。但是,标签(当显示时)应该是一个连续变化的随机字母。
下面的代码来自Gnome builder hello world,其中的gui根据我的需要进行了更改。
main.py:
import sys
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio
from .window import TestWindow
class Application(Gtk.Application):
def __init__(self):
super().__init__(application_id='test',
flags=Gio.ApplicationFlags.FLAGS_NONE)
def do_activate(self):
win = self.props.active_window
if not win:
win = TestWindow(application=self)
win.present()
def main(version):
app = Application()
return app.run(sys.argv)window.py:
from gi.repository import Gtk
@Gtk.Template(resource_path='/test/window.ui')
class TestWindow(Gtk.ApplicationWindow):
__gtype_name__ = 'TestWindow'
label = Gtk.Template.Child()
def __init__(self, **kwargs):
super().__init__(**kwargs)window.ui:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<template class="TestWindow" parent="GtkApplicationWindow">
<property name="can_focus">False</property>
<property name="default_width">600</property>
<property name="default_height">300</property>
<child type="titlebar">
<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="GtkLabel" id="label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button">
<property name="label" translatable="yes">button</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>
</object>
</child>
</template>
</interface>发布于 2019-05-14 01:31:11
我假设您正在使用Gnome builder来构建您的应用程序。为了将处理程序连接到按钮,您应该向按钮添加一个<signal name="clicked" handler="handler_name" />。
http://www.learningpython.com/2006/05/07/creating-a-gui-using-pygtk-and-glade/
这是将pygtk与glade结合使用的教程的链接,该教程主要适用于您正在使用的框架。
这是我的window.py文件中的一段代码,它将VisitFaq按钮连接到它的处理程序
@Gtk.Template(resource_path='/edu/umich/help/window.ui')
class HelpWindow(Gtk.ApplicationWindow):
__gtype_name__ = 'HelpWindow'
VisitFaq = Gtk.Template.Child()
ChatButton = Gtk.Template.Child()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.ChatButton.connect('clicked', self.start_chat)
def start_chat(self):下面是VisitFaq在ui文件中的外观
<object class="GtkButton" id="VisitFaq">
<property name="label" translatable="yes">Visit the FAQ</property>
<property name="name">FaqButton</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="visit_faq" />
</object>发布于 2021-11-19 18:21:01
你可以在这里找到示例代码:
https://pygobject.readthedocs.io/en/latest/guide/gtk_template.html
@joshua-bell在another answer中说,使用Gtk.Template将信号与处理程序连接起来甚至更容易。您不再需要.connect()函数。
您所需要做的就是创建一个带有如下信号的GUI对象:
<object class="GtkButton">
<property name="label">Hello world button</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<signal name="clicked" handler="button_click" swapped="no"/>
</object>并使用@Gtk.Template.Callback装饰器来定义此信号的处理程序:
@Gtk.Template(resource_path='/path/to/window.ui')
class AppWindow(Gtk.ApplicationWindow):
__gtype_name__ = 'AppWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
@Gtk.Template.Callback('button_click')
def button_click(self, *args):
print('Hello world!')https://stackoverflow.com/questions/56010352
复制相似问题