【问题标题】:I need example code for a simple python-gui script with gtk+我需要带有 gtk+ 的简单 python-gui 脚本的示例代码
【发布时间】:2019-05-06 18:10:40
【问题描述】:

我是 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>

【问题讨论】:

  • python gtk 有一些合理的教程,您可以查看这些教程以组合您需要的内容。 python-gtk-3-tutorial.readthedocs.io/en/latest/…
  • 您所展示的代码究竟是什么“示例代码”不够?
  • 谢谢 jonyfries -- 我马上去看看
  • mkrieger1:上面的代码没有引用来自 gui 按钮的点击信号,我无法弄清楚它应该去哪里......更根本的是为什么它应该去它应该去的地方去...我的大脑并没有真正“点击”进入 OOP 的思维方式,我猜。
  • 感谢您发布问题。我有同样的问题。上面的示例代码没有显示如何连接信号。我花了几个小时才找到这些信息。

标签: python-3.x gnome


【解决方案1】:

我假设您正在使用 Gnome builder 来构建您的应用程序。为了将处理程序连接到按钮,您应该向按钮添加 &lt;signal name="clicked" handler="handler_name" /&gt;

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>

【讨论】:

    【解决方案2】:

    您可以在此处找到示例代码:

    https://pygobject.readthedocs.io/en/latest/guide/gtk_template.html

    使用Gtk.Template 将信号与处理程序连接起来比@joshua-bell 在another answer 中所说的更容易。你不再需要.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!')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 2011-05-25
      相关资源
      最近更新 更多