开发者

Save the selected value of a combobox in wxpython

I'm working to design a gui using wxpython, a pecie of my code is that i have a class which a fram

declaration and also i declared variables that i wanna chage their values based on the comboboxes

selection. i did the folowing:

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

and this is the code of the "OnPhaseSelection" event :

def OnPhaseSelection(self, event):
    self.phaseSelection = self.cbPhases.GetValue()

where i wanna save the s开发者_运维问答elected value in the variable "self.phaseSelection" that i declared it with an

empty string as initial value, then i wanna use this variable with the new saved value, but when i run

the program, the variable contains the default value of the combobox! so please what is the problem in

my work ?


I'm not sure what's wrong with that. It looks like it should work. I copied most of it and put it into a runnable example that works on 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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜