GTK、Glade 和 Python 连接来自多个 类 的处理程序与 connect_signals

GTK, Glade and Python connecting Handlers from Multiple Classes with the connect_signals

希望你们今天过得愉快!

我一辈子都想不通如何 link 使用 connect_signals( obj_or_class)。我想将 classes OverviewNavigation 都注入到 window 中。考虑添加每个 window 然后搜索每个 class 以将其生成到字典中是可行的......但是有更简单的方法吗?!

我对 python 还可以,但不是最好的。因此,任何指导、解释和帮助都将是一笔财富!提前谢谢你。

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

class Application():
    def __init__(self):
        self.new_window = Window('overview')
        self.run()

    def run(self):
        Gtk.main()
        print('program terminated')

class Window():
    def __init__(self, window):
        self.gladefile = os.path.dirname((os.path.dirname(os.path.abspath(__file__))) 
            + '/overview/overview.glade')
        self.builder = Gtk.Builder()
        self.builder.add_from_file(self.gladefile)
        self.builder.connect_signals(Overview())
        self.window_name =  'overview'
        self.window = self.builder.get_object(self.window_name)
        self.window.show()

class Overview():
    def onDestroy(self, *args):
        Gtk.main_quit()

    def click(self, button):
        print("clicked")

class Navigation():
    def Overview(self, button):
        # Open the Overview Window
        print("clicked")

    def Settings(self, button):
        # Open the Settings Window
        print("clicked")

    def PID Settings(self, button):
        # Open the PID Settings Window
        print("clicked")

    def Reporting(self, button):
        # Open the Reporting Window
        print("clicked")

# Run Server
if __name__ == '__main__':
main = Application()

为此,您需要将对象的信号与处理程序分开连接,而不是 Gtk.Builder.connect_signals()

看看下面的例子。

example.glade

  <?xml version="1.0" encoding="UTF-8"?>
  <interface>
    <!-- interface-requires gtk+ 3.0 -->
    <object class="GtkWindow" id="window1">
      <property name="can_focus">False</property>
      <signal name="destroy" handler="onDestroy" swapped="no"/>
      <child>
        <object class="GtkButton" id="button1">
          <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="pressed" handler="onButtonPressed" swapped="no"/>
        </object>
      </child>
    </object>
  </interface>

example.py

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

class Handler:
    @staticmethod
    def onDestroy(*args):
        Gtk.main_quit()

class Handler1:
    @staticmethod
    def onButtonPressed(button):
        print("Hello World!")

builder = Gtk.Builder()
builder.add_from_file("example.glade")
window = builder.get_object("window1")
window.connect("destroy",Handler.onDestroy)
button = builder.get_object("button1")
button.connect("pressed",Handler1.onButtonPressed)
window.show_all()

Gtk.main()