如何在 PyGtk 中的两个类/窗口之间传递变量? [英] How can I pass variables between two classes/windows in PyGtk?

查看:65
本文介绍了如何在 PyGtk 中的两个类/窗口之间传递变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用 PyGtk,但在理解 Windows 的交互方面有困难.

I am starting out with PyGtk and am having trouble understanding the interaction of windows.

我非常简单的问题如下.
假设我有一个类,它只是创建一个带有文本输入字段的窗口.单击该窗口中的确定"按钮时,我想将输入字段中的文本传递到另一个窗口,该窗口由另一个类创建,并带有 gtk 菜单,并使用文本字段的内容创建一个新条目.

My very simple question is the following.
Suppose I have a class that simply creates a window with a text-entry field. When clicking the "ok" button in that window, I want to pass the text in the entry field to another window, created by another class, with a gtk menu and create a new entry with the content of the text field.

我该如何实施?

推荐答案

让我们称 A 为菜单,而 B 为带有文本输入字段的窗口.如果我理解正确,A 调用 B 并且当 B 中按下 Ok 按钮时,A 需要更新其菜单.

Let's call A the Menu, and B the window with the text-entry field. If I understood correctly A calls B and when Ok button is pressed in B, A needs to update its menu.

在这种情况下,您可以在 A 中创建一个回调函数,用于在按下 B 的确定按钮时调用.当你创建 B 时你可以传递这个回调,这是一个例子:

In this scenario you could create a callback function in A, meant to be called when B's ok button is pressed. When you create B you can pass this callback, here's an example:

class B(gtk.Window):
   def __init__(self, callback):
      gtk.Window.__init__(self)

      self.callback = callback
      # Create components:
      # self.entry, self.ok_button ...

      self.ok_button.connect("clicked", self.clicked)

   def clicked(self, button):
      self.callback(self.entry.get_text())

class A(gtk.Window):
   def create_popup(self):
      popup = B(self.popup_callback)
      popup.show()

   def popup_callback(self, text):
       # Update menu with new text
       # ...

这篇关于如何在 PyGtk 中的两个类/窗口之间传递变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆