在wxpython中保存组合框的选定值

时间:2011-09-28 20:32:06

标签: python user-interface combobox wxpython

我正在使用wxpython设计一个gui,我的代码的一个特点就是我有一个类fram

声明,并且我声明了变量,我想根据组合框

来改变它们的值

选择。我做了以下的事情:

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 个答案:

答案 0 :(得分: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()