2011-09-28 16 views
1

我正在使用的wxPython,我的代码pecie是,我有一个类来设计一个GUI其中FRAM保存组合框的选择值在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”我宣布它选择的值与

空字符串作为初始值,然后我想用新保存的值这个变量,但是当我运行

程序,该变量包含高清组合框的价值!所以请什么是在

我的工作的问题?

回答

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()