问题
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