我有一个单选框,用户可以选择一个值。稍后,当用户单击“运行”按钮时,我想获取该单选框值并使用它。我不知道如何。本质上,您如何获得单选框选择的值并在程序中的其他位置使用它。我尝试过的其他方式仅返回默认选择,好像用户选择新值时无法识别
import wx
class MainFrame(wx.Frame):
def __init__(self):
title = "Example"
wx.Frame.__init__(self, None, title=title, pos=(200,200), size=(400,100))
panel_number = ChooseNumber(self)
panel_calc = PerformCalc(self)
boxh = wx.BoxSizer(wx.HORIZONTAL)
boxh.Add(panel_number, 0, wx.EXPAND)
boxh.Add(panel_calc, 0, wx.EXPAND)
self.SetAutoLayout(True)
self.SetSizer(boxh)
self.Layout()
def OnClose(self, event):
self.Destroy()
class ChooseNumber(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, style=wx.SIMPLE_BORDER, size=(200,100))
lblList = ['1', '2']
self.rbox = wx.RadioBox(self, id=wx.ID_ANY, label="Choose Number", choices=lblList)
self.rbox.Bind(wx.EVT_RADIOBOX, lambda event: self.onRadio())
boxh = wx.BoxSizer(wx.HORIZONTAL)
boxv = wx.BoxSizer(wx.VERTICAL)
boxh.Add(self.rbox, 0, wx.CENTER)
boxv.Add(boxh, 0, wx.CENTER)
self.SetSizer(boxv)
def onRadio(self):
if self.rbox.GetStringSelection() == "1":
result = 1+5
else:
result = 2*10
print(result)
class PerformCalc(wx.Panel):
def __init__(self, MainFrame):
wx.Panel.__init__(self, MainFrame, id = wx.ID_ANY, style=wx.SIMPLE_BORDER, size=(200,100))
txt = "Run Calculation"
self.label = wx.StaticText(self, -1, txt, (0,0), (200,20), wx.ALIGN_CENTER)
self.label.Wrap(150)
file_button = wx.Button(self, label="Run")
file_button.Bind(wx.EVT_BUTTON, self.RunProgram)
#Set layout on panel-------------------------------------------------
boxh = wx.BoxSizer(wx.HORIZONTAL)
boxv = wx.BoxSizer(wx.VERTICAL)
boxv.Add(self.label, 0, wx.CENTER)
boxv.Add(file_button, 0, wx.CENTER)
boxh.Add(boxv, 0, wx.CENTER)
self.SetSizer(boxh)
self.Layout()
#---------------------------------------------------------------------
def RunProgram(self, event):
print("Run Calculation")
answer = ChooseNumber.onRadio()
print(answer)
def main():
app = wx.App(redirect=True)
top = MainFrame()
top.Show()
app.MainLoop()
if __name__ == "__main__":
main()