wxPython handling events (button and radio button) between different methods
I'm learning wxpython for one of my project, and I have a problem.. How do I store the value of the RadioButton once I click my button?
I have a
class SerialFrame(wx.Frame):
inside that I have
def __init__(self, parent, title):
super(SerialFrame, self).__init__(parent, title=title,
size=(550, 400))
self.SetMinSize(self.GetSize())
self.InitUI()
self.Center()
self.Show()
the InitUI method sets up my UI which has a bunch of stuff including 3 Radio Buttons and a button like so
def InitUI(self):
mypanel = wx.Panel(self, -1)
...
baudRadioButton1 = wx.RadioButton(mypanel, -1, '9600', style=wx.RB_GROUP)
baudRadioButton2 = wx.RadioButton(mypanel, -1, 开发者_C百科'14400')
baudRadioButton3 = wx.RadioButton(mypanel, -1, '19200')
...
stopButton = wx.Button(mypanel, 2, label='Stop', size = (70,20))
...
mypanel.Bind(wx.EVT_BUTTON, self.clickStart, id=1)
mypanel.Bind(wx.EVT_RADIOBUTTON, self.setRadioValues, id=baudRadioButton1.GetId())
I tried something like
def clickStart(self, event):
baudRate1 = str(self.baudRadioButton1.GetValue())
self.Close(True)
But it won't work. P.S. my OOP knowledge is still limited.
i'm assuming you have UI. radio button are used to select Baud rate.
baudRadioButton1 = wx.RadioButton(mypanel, -1, '9600', style=wx.RB_GROUP)
here 9600 is only printed on GUI. you can put your name instead of 9600.
You have to understand that nothing is happening automatically. we have to tell wxpython how to react when a radio button is selected.
you haven't done binding of BaudradioButton1 with clickStart1. Bind is used to specify in occurrence of an event which method/fun has to be called.
So when an radio button is selected then an 'EVT_RADIOBUTTON' event occurs and wxpython will call your clickstart1 method. Inside clickstart you can manipulate baudrate. In short you dont have to save radio button values.
my suggestion is
baudRadioButton1 = wx.RadioButton(mypanel, -1, label='9600', style=wx.RB_GROUP)
baudRadioButton2 = wx.RadioButton(mypanel, -1, label='14400')
baudRadioButton3 = wx.RadioButton(mypanel, -1, label='19200')
do the binding of a radio button with a method
self.Bind(wx.EVT_RADIOBUTTON,self.baudRadioButton1,self.clickstart1)
self.Bind(wx.EVT_RADIOBUTTON,self.baudRadioButton2,self.clickstart2)
self.Bind(wx.EVT_RADIOBUTTON,self.baudRadioButton3,self.clickstart3)
and methods will be
def clickStart1(self, event):
baudRate = 9600
......
def clickStart2(self, event):
baudRate = 14400
......
and so on..
I would suggest you to read about event mechanism.
I hope this helps.
精彩评论