我使用wxPython创建一个父窗口“ A”(一个wx.Frame)。
在“ A”中,我创建了一个子级(又是wx.Frame)类似于对话框的弹出窗口“ B”。
每当我按下父级“ A”中的给定按钮时,我就会将此代码称为:
import windowB as bb
class windowA (wx.Frame):
... some other methods go here...
def on_data_setup(self, event):
os.remove("tmp.tmp")
popUpWindow = bb.popUp(None, title='Data Manager')
popUpWindow.Show()
while not os.path.exists("tmp.tmp"):
time.sleep(1)
with open("tmp.tmp","r") as ifh:
l = ifh.readlines()
print l
其中windowB包含在另一个文件中,它看起来像:
class windowB (wx.Frame):
...
def on_exit(self,event):
with open("tmp.tmp","w") as ofh:
ofh.write("test")
self.Close()
这显然没什么意思,因为当我在父类中调用“ sleep”时,整个程序冻结了-我需要杀死它,因为我无法与孩子“ B”互动:(。
此外,通过临时文件传递信息的效率非常低。
问题是,关闭后,我需要我的子窗口/弹出窗口B将信息传递回父窗口A(B)。如何实现? 谢谢!
答案 0 :(得分:0)
使用引用是在wxpython框架之间进行通信的一种简便方法。下面的示例创建一个名为“父母”的框架。父框架创建子框架并保存对其的引用。您可能永远不需要在同一过程中将信息写入磁盘以进行交换。
import wx
class Parent(wx.Frame):
def __init__(self):
super().__init__(None, title="parent frame")
btn = wx.Button(self, label="click to send a message to the child frame")
btn.Bind(wx.EVT_BUTTON, self.on_btn)
self.Show()
self.CenterOnScreen(wx.BOTH)
self.child_frame = None # type: wx.Frame
self.create_child_frame()
def create_child_frame(self):
child = wx.Frame(self, title="child frame")
child.text = wx.TextCtrl(child)
child.Show()
child.SetPosition(self.GetRect().GetBottomLeft())
# save a reference to the child frame on the parent
self.child_frame = child
def send_message_to_child(self, msg):
# re-open the child frame if it's been closed
if not bool(self.child_frame):
self.create_child_frame()
# accessing child frame attributes from the parent frame
self.child_frame.text.SetValue(f"Message from parent frame: '{msg}'")
def on_btn(self, evt):
with wx.TextEntryDialog(self,
"Enter a message to pass to the child frame") as dialog: # type: wx.TextEntryDialog
if dialog.ShowModal() == wx.ID_OK:
msg = dialog.GetValue()
self.send_message_to_child(msg)
app = wx.App()
parent = Parent()
app.MainLoop()
答案 1 :(得分:0)
我想继续将其发布,因为它帮助我解决了一些类似的问题...
当需要从子窗口更新主窗口时...请参见下面的更新脚本:
import wx
class MainPage(wx.Frame):
def __init__(self):
super().__init__(None, title="parent frame")
btn = wx.Button(self, size=(200, 20), pos=(80,160), label="click to send a message ")
btn.Bind(wx.EVT_BUTTON, self.on_btn)
#self.CenterOnScreen(VERTICAL)
MainPage.text = wx.TextCtrl(self,size=(360, 140), pos=(10,10))
self.Show()
self.child_frame = None # type: wx.Frame
self.create_child_frame()
def create_child_frame(self):
child = wx.Frame(self, title="child frame")
child.text = wx.TextCtrl(child)
child.Show()
child.SetPosition(self.GetRect().GetBottomLeft())
# save a reference to the child frame on the parent
self.child_frame = child
def send_message_to_child(self, msg):
# re-open the child frame if it's been closed
if not bool(self.child_frame):
self.create_child_frame()
# accessing child frame attributes from the parent frame
self.child_frame.text.SetValue(f"Message from child frame: '{msg}'")
self.text.SetValue(f"Message from child frame: '{msg}'")
def on_btn(self, evt):
with wx.TextEntryDialog(self,
"Enter a message to pass to the child frame") as dialog: # type: wx.TextEntryDialog
if dialog.ShowModal() == wx.ID_OK:
msg = dialog.GetValue()
self.send_message_to_child(msg)
app = wx.App()
parent = MainPage()
app.MainLoop()