gpt4 book ai didi

python - GTK、Glade 和 Python 使用 connect_signals 连接来自多个类的处理程序

转载 作者:行者123 更新时间:2023-12-01 15:20:29 25 4
gpt4 key购买 nike

希望你们今天过得愉快!

我终究无法弄清楚如何使用connect_signals(obj_or_class) 将多个类处理程序链接到一个窗口对象。我想将 OverviewNavigation 这两个类都注入(inject)到窗口中。考虑添加每个窗口然后搜索每个类以将其生成到字典中是可行的......但是有更简单的方法吗?!

我对 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()

看看下面的例子。

例子.林间空地

  <?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>

例子.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()

关于python - GTK、Glade 和 Python 使用 connect_signals 连接来自多个类的处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51953389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com