在wxpython中保存combobox的选中值
我正在用wxpython设计一个图形界面,其中有一段代码是关于一个类的声明,还有一些我想根据下拉框选择来改变值的变量。
我做了以下操作:
class myMenu(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(900, 700))
self.ct = 0
self.phaseSelection = ""
self.opSelection = ""
self.instSelection = ""
self.orgSelection = ""
panel = wx.Panel(self, -1)
panel.SetBackgroundColour('#4f3856')
phasesList = ["preOperations", "inOperations", "postOperations"]
self.cbPhases = wx.ComboBox(panel, 500, 'Phase', (50, 150), (160,-1), phasesList, wx.CB_DROPDOWN)
self.Bind(wx.EVT_COMBOBOX, self.OnPhaseSelection, id = self.cbPhases.GetId())
这是“OnPhaseSelection”事件的代码:
def OnPhaseSelection(self, event):
self.phaseSelection = self.cbPhases.GetValue()
我想把选中的值保存到我之前声明的变量“self.phaseSelection”中,最开始这个变量是一个空字符串。然后我想用这个变量的新值,但当我运行程序时,这个变量却还是下拉框的默认值!请问我的代码哪里出了问题?
1 个回答
3
我不太确定哪里出了问题。看起来应该是可以正常工作的。我把大部分内容复制过来,做了一个可以在Windows上运行的示例:
import wx
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")
panel = wx.Panel(self, wx.ID_ANY)
self.ct = 0
self.phaseSelection = ""
self.opSelection = ""
self.instSelection = ""
self.orgSelection = ""
phasesList = ["preOperations", "inOperations", "postOperations"]
self.combo = wx.ComboBox(panel, choices=phasesList)
self.combo.Bind(wx.EVT_COMBOBOX, self.onCombo)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.combo)
panel.SetSizer(sizer)
#----------------------------------------------------------------------
def onCombo(self, event):
"""
"""
self.phaseSelection = self.combo.GetValue()
print self.phaseSelection
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm().Show()
app.MainLoop()