GTK,Glade和Python使用connect_signals

时间:2018-08-21 16:51:46

标签: python gtk glade

我希望你们都过得愉快!

我一生不知道如何使用 connect_signals(obj_or_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()

1 个答案:

答案 0 :(得分:1)

为此,您需要将对象的信号分别与处理程序而不是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()