简体   繁体   English

如何跨 wxPython 框架传递信息?

[英]How do I pass information across wxPython frames?

I use wxPython to create a parent window "A" (a wx.Frame).我使用 wxPython 创建一个父窗口“A”(一个 wx.Frame)。

Within "A", I create a child (again a wx.Frame) dialog-like popup window "B".在“A”中,我创建了一个子(也是一个 wx.Frame)对话框式弹出窗口“B”。

Whenever I press a given button in the parent "A", I call this code:每当我按下父“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

where windowB is contained in another file and it looks like:其中 windowB 包含在另一个文件中,它看起来像:

class windowB (wx.Frame):
    ...
    def on_exit(self,event):
        with open("tmp.tmp","w") as ofh:
            ofh.write("test")
        self.Close()

This obviously does not word because as I call 'sleep' in the parent class, the whole program freezes - and I need to kill it as I can't interact with the child "B" neither :(.这显然没有用,因为当我在父类中调用 'sleep' 时,整个程序都冻结了 - 我需要杀死它,因为我既不能与孩子“B”交互 :(。

Also, passing information via temp files is very inefficient.此外,通过临时文件传递信息的效率非常低。

The point is, I need my child/popUp window B to pass information back to the parent window A when I close it (B).. How can I achieve that?关键是,当我关闭它时,我需要我的子/弹出窗口 B 将信息传递回父窗口 A(B)。我怎样才能做到这一点? Thanks!谢谢!

Using references is an easy way to communicate between wxpython frames.使用引用是一种在 wxpython 框架之间进行通信的简单方法。 The following example creates a frame called 'Parent.'下面的示例创建一个名为“Parent”的框架。 The parent frame creates a child frame and saves a reference to it.父框架创建一个子框架并保存对它的引用。 You should probably never need to write information to disk to exchange it within the same process.您可能永远不需要将信息写入磁盘以在同一进程中交换它。

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

Thought I would go ahead and post this as it helped me solve a somewhat similar problem...以为我会继续发布这个,因为它帮助我解决了一个有点类似的问题......

When needing to update Main Window from a child window... See the updated script below:当需要从子窗口更新主窗口时......请参阅下面的更新脚本:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM