繁体   English   中英

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

[英]Save the selected value of a combobox in 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”中,并用

空字符串作为初始值,那么我想将此变量与新保存的值一起使用,但是当我运行时

程序中,该变量包含组合框的默认值! 所以请问是什么问题

我的工作 ?

我不确定这是怎么回事。 看起来应该可以使用。 我复制了其中的大部分内容,并将其放入可在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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM