Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在wxPython帧之间传递信息?_Python_Wxpython_Wxwidgets - Fatal编程技术网

如何在wxPython帧之间传递信息?

如何在wxPython帧之间传递信息?,python,wxpython,wxwidgets,Python,Wxpython,Wxwidgets,我使用wxPython创建父窗口“a”(wx.Frame) 在“A”中,我创建了一个类似弹出窗口“B”的子对话框(同样是一个wx.Frame) 每当我按下父级“a”中的给定按钮时,我调用此代码: import windowB as bb class windowA (wx.Frame): ... some other methods go here... def on_data_setup(self, event): os.remove("tmp.tmp")

我使用wxPython创建父窗口“a”(wx.Frame)

在“A”中,我创建了一个类似弹出窗口“B”的子对话框(同样是一个wx.Frame)

每当我按下父级“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时将信息传递回父窗口A(B)…我如何实现这一点?
谢谢!

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

我想我会继续发表这篇文章,因为它帮助我解决了一个类似的问题

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

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

嗯……是的,但我的问题是孩子与家长之间的异步通信。也就是说,当我填充完孩子的文本CRTL,准备关闭“孩子”窗口时,我需要将我键入的所有信息发送到parent@EffePelosa异步并不限制引用的使用让我重新表述一下:我可以引用子框架我不知道如何从子窗口将数据发送回父窗口。您应该更新您的问题以反映您实际想要完成的任务。self.GetParent()将返回帧的父窗口