How can I pass variables between two classes/windows in PyGtk?

ε祈祈猫儿з 提交于 2019-12-11 17:53:08

问题


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

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.

How do I implement this?


回答1:


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.

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
       # ...


来源:https://stackoverflow.com/questions/4090804/how-can-i-pass-variables-between-two-classes-windows-in-pygtk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!