从一个 class 发送消息到另一个 class
Send message from one class to another class
问题:我有一个框架(FirstFrame
class),它有一个调用另一个框架的按钮(SecondFrame
class).当我单击 SecondFrame
中的按钮时,我希望关闭此框架并向 FirstFrame
.
发送一条消息(“我是第一帧”,见下文)
到目前为止,这是我的代码:
import wx
class FirstFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, None, title="First Frame", size=(400,400))
self.UI()
self.Centre()
self.Show()
def UI(self):
self.panel1 = wx.Panel(self, -1)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.panel1, 1, flag=wx.EXPAND)
b = wx.Button(self.panel1, label='second window', size=(100,100), pos=(100,100))
b.Bind(wx.EVT_BUTTON, self.OnB)
self.SetSizer(self.sizer)
def OnB(self, event):
frame = SecondFrame()
frame.Show()
class SecondFrame(wx.Frame):
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Second Frame")
panel = wx.Panel(self)
txt = wx.StaticText(panel, label="I'm the second frame!")
self.button = wx.Button(panel, -1, "Close window and send message", pos = (100,100))
self.button.Bind(wx.EVT_BUTTON, self.onClose)
def onClose(self,event):
self.Close()
#----------------------------------------------------------------------
app = wx.App(False)
FirstFrame(None, title='')
app.MainLoop()
我只是不知道如何连接这两个 classes。即在第二个框架点击按钮后,如何与调用它的第一个框架通信。有什么建议么?单击 SecondFrame
上的按钮后,我想要获得的是与此类似的东西:
提前致谢。
That is, after clicking the button in the second frame, how to
communicate with the first frame that called it. Any suggestions?
创建第二个框架时,向其发送self
:
frame = SecondFrame(self) #Inside the first Frame, self is the first Frame
或者,等价地:
first_frame = self
frame = SecondFrame(first_frame)
在第二个框架的构造函数中这样做:
class SecondFrame(wx.Frame):
def __init__(self, first_frame):
"""Constructor"""
wx.Frame.__init__(self, None, title="Second Frame")
self.first_frame = first_frame
然后,在第二个 Frame 的 onclick 处理程序中,您可以这样做:
def onClose(self,event):
self.first_frame.do_something("I'm the first frame")
self.Close()
在第一帧中,您可以这样做:
class FirstFrame(wx.Frame):
...
...
def do_something(self, msg):
self.msg = msg #Save the message
print(msg) #Do something else with the message....
或者,在您的特定情况下:
def do_something(self, msg):
wx.StaticText(self.panel1, label="I'm the first frame")
请注意,在您的第一个 Frame 构造函数中:
def __init__(self, parent, title):
wx.Frame.__init__(self, None, title="First Frame", size=(400,400))
您没有使用 parent 或 title 参数变量,因此为这些变量指定参数没有任何作用:
FirstFrame(None, title='')
标题不会是空白,而是 "First Frame"。你想做这样的事情:
class FirstFrame(wx.Frame):
def __init__(self, the_title="First Frame", the_parent=None):
wx.Frame.__init__(
self,
the_parent,
title=the_title,
size=(400,400)
)
self.UI()
self.Centre()
self.Show()
然后,如果你想使用默认值,你可以像这样调用第一个 Frame 构造函数:
app = wx.App(False)
FirstFrame()
app.MainLoop()
而且,如果您想使用其他标题,您可以这样写:
app = wx.App(False)
FirstFrame("Testing")
app.MainLoop()
问题:我有一个框架(FirstFrame
class),它有一个调用另一个框架的按钮(SecondFrame
class).当我单击 SecondFrame
中的按钮时,我希望关闭此框架并向 FirstFrame
.
到目前为止,这是我的代码:
import wx
class FirstFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, None, title="First Frame", size=(400,400))
self.UI()
self.Centre()
self.Show()
def UI(self):
self.panel1 = wx.Panel(self, -1)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.panel1, 1, flag=wx.EXPAND)
b = wx.Button(self.panel1, label='second window', size=(100,100), pos=(100,100))
b.Bind(wx.EVT_BUTTON, self.OnB)
self.SetSizer(self.sizer)
def OnB(self, event):
frame = SecondFrame()
frame.Show()
class SecondFrame(wx.Frame):
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Second Frame")
panel = wx.Panel(self)
txt = wx.StaticText(panel, label="I'm the second frame!")
self.button = wx.Button(panel, -1, "Close window and send message", pos = (100,100))
self.button.Bind(wx.EVT_BUTTON, self.onClose)
def onClose(self,event):
self.Close()
#----------------------------------------------------------------------
app = wx.App(False)
FirstFrame(None, title='')
app.MainLoop()
我只是不知道如何连接这两个 classes。即在第二个框架点击按钮后,如何与调用它的第一个框架通信。有什么建议么?单击 SecondFrame
上的按钮后,我想要获得的是与此类似的东西:
提前致谢。
That is, after clicking the button in the second frame, how to communicate with the first frame that called it. Any suggestions?
创建第二个框架时,向其发送self
:
frame = SecondFrame(self) #Inside the first Frame, self is the first Frame
或者,等价地:
first_frame = self
frame = SecondFrame(first_frame)
在第二个框架的构造函数中这样做:
class SecondFrame(wx.Frame):
def __init__(self, first_frame):
"""Constructor"""
wx.Frame.__init__(self, None, title="Second Frame")
self.first_frame = first_frame
然后,在第二个 Frame 的 onclick 处理程序中,您可以这样做:
def onClose(self,event):
self.first_frame.do_something("I'm the first frame")
self.Close()
在第一帧中,您可以这样做:
class FirstFrame(wx.Frame):
...
...
def do_something(self, msg):
self.msg = msg #Save the message
print(msg) #Do something else with the message....
或者,在您的特定情况下:
def do_something(self, msg):
wx.StaticText(self.panel1, label="I'm the first frame")
请注意,在您的第一个 Frame 构造函数中:
def __init__(self, parent, title):
wx.Frame.__init__(self, None, title="First Frame", size=(400,400))
您没有使用 parent 或 title 参数变量,因此为这些变量指定参数没有任何作用:
FirstFrame(None, title='')
标题不会是空白,而是 "First Frame"。你想做这样的事情:
class FirstFrame(wx.Frame):
def __init__(self, the_title="First Frame", the_parent=None):
wx.Frame.__init__(
self,
the_parent,
title=the_title,
size=(400,400)
)
self.UI()
self.Centre()
self.Show()
然后,如果你想使用默认值,你可以像这样调用第一个 Frame 构造函数:
app = wx.App(False)
FirstFrame()
app.MainLoop()
而且,如果您想使用其他标题,您可以这样写:
app = wx.App(False)
FirstFrame("Testing")
app.MainLoop()