Wxpython将数据从一帧传递到另一帧
Wxpython passing data from one frame to another
我有一个文本控件、一个选择控件和一个按钮。我想在按下按钮时将文本控件和选择控件的值传递给另一个框架。在下面代码的 on_press 方法中,我将两个值作为列表返回。如何在 PanelTwo 中访问此列表。这是代码:
import wx
class PanelOne(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
box = wx.BoxSizer(wx.VERTICAL)
self.label = wx.StaticText(self,label = "Your Name:" ,style = wx.ALIGN_CENTRE)
box.Add(self.label, 1 , wx.EXPAND |wx.ALIGN_CENTER_HORIZONTAL |wx.ALL, 20)
self.inputTxtOne = wx.TextCtrl(self,wx.ID_ANY,value='')
box.Add(self.inputTxtOne, 1 , wx.EXPAND |wx.ALIGN_CENTER_HORIZONTAL |wx.ALL, 20)
languages = ['ZIRA', 'DAVID']
chlbl = wx.StaticText(self,label = "Voice control",style = wx.ALIGN_CENTRE)
box.Add(chlbl,1,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,5)
self.choice = wx.Choice(self,choices = languages)
box.Add(self.choice,1,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,5)
btn = wx.Button(self, label='Submit',style=wx.TRANSPARENT_WINDOW)
btn.SetFont(wx.Font(15,wx.FONTFAMILY_DEFAULT,wx.FONTSTYLE_NORMAL,wx.FONTWEIGHT_NORMAL))
btn.Bind(wx.EVT_BUTTON, self.on_press)
box.Add(btn,1,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,5)
self.SetSizer(box)
btn.Bind(wx.EVT_BUTTON, self.on_press)
def on_press(self,event):
name = self.inputTxtOne.GetValue()
voice_choice = self.choice.GetValue()
nv_list = [name,voice_choice]
parent_frame = self.GetParent()
parent_frame.Close()
frame = FrameTwo()
frame.Show()
return nv_list
class PanelTwo(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
box = wx.BoxSizer(wx.VERTICAL)
self.label = wx.StaticText(self,label = "Your Name is "+name ,style = wx.ALIGN_CENTRE)
box.Add(self.label, 1 , wx.EXPAND |wx.ALIGN_CENTER_HORIZONTAL |wx.ALL, 20)
self.SetSizer(box)
class FrameOne(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="First Frame")
panel = PanelOne(self)
self.Show()
class FrameTwo(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Second Frame")
panel = PanelTwo(self)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = FrameOne()
app.MainLoop()
您的代码中有几个错误。一步步来吧。
首先,PanelOne中名为"name"的变量,必须是class的一部分,而不是"on_press"方法的局部变量:
变化:
name = self.inputTxtOne.GetValue()
voice_choice = self.choice.GetValue()
作者:
# Notice the use of self. In this way, we can access it from anywhere in the class
self.name = self.inputTxtOne.GetValue()
# What version of wx are you using? Get the choices value with GetSelection() method.
voice_choice = self.choice.GetSelection()
现在我们需要从另一个 class (PanelTwo) 访问这个变量。为此,我们会将 PanelOne class 的一个实例传递给第二帧 (Frame2),并将其存储为成员变量(使用 self,正如我们在前面的示例中看到的那样)。
# When you instantiate the Frame2 in PanelOne must pass the self instance of PanelOne
frame = FrameTwo(self)
然后在Frame2中的init方法中:
def __init__(self, parent):
wx.Frame.__init__(self, None, title="Second Frame")
#Now the reference to PanelOne is a member varialble of class Frame2.
self.panelOne = parent
#Pass Frame2 instance (self) to PanelTwo in constructor:
panel = PanelTwo(self)
# Rest of your code...
终于在 PanelTwo 中:
class PanelTwo(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self)
# parent is Frame2.
# panelOne is a public and member variable of Frame2 that references to PanelOne instance.
# name is a member and accesible variable of PanelOne.
print(parent.panelOne.name)
一个建议:使用WxGlade 来制作最好的代码。有了它,您可以了解很多关于 wxpython 中的事情是如何完成的。
祝你好运!
我有一个文本控件、一个选择控件和一个按钮。我想在按下按钮时将文本控件和选择控件的值传递给另一个框架。在下面代码的 on_press 方法中,我将两个值作为列表返回。如何在 PanelTwo 中访问此列表。这是代码:
import wx
class PanelOne(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
box = wx.BoxSizer(wx.VERTICAL)
self.label = wx.StaticText(self,label = "Your Name:" ,style = wx.ALIGN_CENTRE)
box.Add(self.label, 1 , wx.EXPAND |wx.ALIGN_CENTER_HORIZONTAL |wx.ALL, 20)
self.inputTxtOne = wx.TextCtrl(self,wx.ID_ANY,value='')
box.Add(self.inputTxtOne, 1 , wx.EXPAND |wx.ALIGN_CENTER_HORIZONTAL |wx.ALL, 20)
languages = ['ZIRA', 'DAVID']
chlbl = wx.StaticText(self,label = "Voice control",style = wx.ALIGN_CENTRE)
box.Add(chlbl,1,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,5)
self.choice = wx.Choice(self,choices = languages)
box.Add(self.choice,1,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,5)
btn = wx.Button(self, label='Submit',style=wx.TRANSPARENT_WINDOW)
btn.SetFont(wx.Font(15,wx.FONTFAMILY_DEFAULT,wx.FONTSTYLE_NORMAL,wx.FONTWEIGHT_NORMAL))
btn.Bind(wx.EVT_BUTTON, self.on_press)
box.Add(btn,1,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,5)
self.SetSizer(box)
btn.Bind(wx.EVT_BUTTON, self.on_press)
def on_press(self,event):
name = self.inputTxtOne.GetValue()
voice_choice = self.choice.GetValue()
nv_list = [name,voice_choice]
parent_frame = self.GetParent()
parent_frame.Close()
frame = FrameTwo()
frame.Show()
return nv_list
class PanelTwo(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
box = wx.BoxSizer(wx.VERTICAL)
self.label = wx.StaticText(self,label = "Your Name is "+name ,style = wx.ALIGN_CENTRE)
box.Add(self.label, 1 , wx.EXPAND |wx.ALIGN_CENTER_HORIZONTAL |wx.ALL, 20)
self.SetSizer(box)
class FrameOne(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="First Frame")
panel = PanelOne(self)
self.Show()
class FrameTwo(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Second Frame")
panel = PanelTwo(self)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = FrameOne()
app.MainLoop()
您的代码中有几个错误。一步步来吧。
首先,PanelOne中名为"name"的变量,必须是class的一部分,而不是"on_press"方法的局部变量:
变化:
name = self.inputTxtOne.GetValue()
voice_choice = self.choice.GetValue()
作者:
# Notice the use of self. In this way, we can access it from anywhere in the class
self.name = self.inputTxtOne.GetValue()
# What version of wx are you using? Get the choices value with GetSelection() method.
voice_choice = self.choice.GetSelection()
现在我们需要从另一个 class (PanelTwo) 访问这个变量。为此,我们会将 PanelOne class 的一个实例传递给第二帧 (Frame2),并将其存储为成员变量(使用 self,正如我们在前面的示例中看到的那样)。
# When you instantiate the Frame2 in PanelOne must pass the self instance of PanelOne
frame = FrameTwo(self)
然后在Frame2中的init方法中:
def __init__(self, parent):
wx.Frame.__init__(self, None, title="Second Frame")
#Now the reference to PanelOne is a member varialble of class Frame2.
self.panelOne = parent
#Pass Frame2 instance (self) to PanelTwo in constructor:
panel = PanelTwo(self)
# Rest of your code...
终于在 PanelTwo 中:
class PanelTwo(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self)
# parent is Frame2.
# panelOne is a public and member variable of Frame2 that references to PanelOne instance.
# name is a member and accesible variable of PanelOne.
print(parent.panelOne.name)
一个建议:使用WxGlade 来制作最好的代码。有了它,您可以了解很多关于 wxpython 中的事情是如何完成的。
祝你好运!