【问题标题】:How do I pass information across wxPython frames?如何跨 wxPython 框架传递信息?
【发布时间】:2019-09-10 11:44:35
【问题描述】:

我使用 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()

这显然不是字,因为当我在父类中调用“睡眠”时,整个程序冻结 - 我需要杀死它,因为我也不能与孩子“B”互动:(。

另外,通过临时文件传递信息效率非常低。

关键是,当我关闭它时,我需要我的子/弹出窗口 B 将信息传递回父窗口 A (B)。我该如何实现呢? 谢谢!

【问题讨论】:

    标签: python wxpython wxwidgets


    【解决方案1】:

    使用引用是在 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()
    

    【讨论】:

    • 嗯...是的,但我的问题是从孩子到父母的异步通信。即当我完成填充孩子的 textCrtls 并准备关闭子窗口时,我需要将我输入的所有信息发送给父母
    • @EffePelosa 是异步的并不限制引用的使用
    • 让我改述一下:我可以从父级引用子框架,并通过引用向它发送信息。我不明白如何从子窗口将数据发送回父级..
    • 您应该更新您的问题以反映您实际想要完成的任务。 self.GetParent() 将返回框架的父窗口
    【解决方案2】:

    我想我会继续发布这个,因为它帮助我解决了一个类似的问题......

    当需要从子窗口更新主窗口时...请参阅下面的更新脚本:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2020-04-05
      • 2011-12-19
      相关资源
      最近更新 更多