簡體   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