wx.Combobox由一個編輯框和列表組成。它可以顯示為帶有可編輯或只讀文本框的靜態列表,也可以顯示為帶下拉列表的文本區域,或者是不帶文本框的下拉列表。一個wx.Combobox只允許單選。wx.Combobox的選擇項從0開始編號。如果你需要定製wx.Combobox,可以參照wx.ComboCtrl,wx.OwnerDrawnComboBox,wx.ComboPopup和即將可用的wx.BitmapComBox等控件。
1. Style 樣式
wx.CB_SIMPLE 創建一個帶列表顯示的combobox.只支持windows
wx.CB_DROPDOWN 創建一個帶下拉列表的combobox
wx.CB_READONLY 同wx.CB_DROPDOWN,但只有被當做combobox選擇項的字符串才能被選定,不在選擇列表中的字符串無法將無法填入控件中的文本框。
wx.CB_SORT 列表項按字母序排序
wx.TE_PROCES_ENTER 此樣式下控件會產生事件wx.EVT_COMMAND_TEXT_ENTER(否則按下Enter鍵要麼被控件內部處理,要麼被用於控件導航)。僅支持windows。
2. 事件
EVT_COMBOBOX(id,func) //當列表中的一個項被選擇時,處理wx.EVT_COMMAND_COMBOBOX_SELECTED事件。注意到調用GetValue返回的是選擇後的值。
EVT_TEXT(id,func) //當combobox文本改變時,處理wx.EVT_COMMAND_TEXT_UPDATED事件
EVT_TEXT_ENTER(id,func) //當在combobox中按下回車鍵時,處理wx.EVT_TEXT_ENTER事件。
#-*- coding: UTF-8 -*-
#-------------------------------------------------------------------------------#Name: 模塊1#Purpose:#
#Author: ankier#
#Created: 10/10/2012#Copyright: (c) ankier 2012#Licence: #-------------------------------------------------------------------------------
importwxclassComboBoxFrame(wx.Frame):def __init__(self):
wx.Frame.__init__(self, None, -1, 'Combo Box Example', size=(350, 300))
panel= wx.Panel(self, -1)
sampleList= ['zero', 'one', 'two', 'three', 'four', 'five','six', 'seven', 'eight']
flexSizer= wx.FlexGridSizer(4,4,6,6)
flexSizer.AddGrowableCol(1)
flexSizer.AddGrowableCol(3)#定義普通樣式的comboBox, 接受鍵盤輸入,但不會改變Item
commonLable = wx.StaticText(panel, -1, "普通樣式")
commonComboBox= wx.ComboBox(panel, -1, value = "zero", choices = sampleList, style =wx.CB_DROPDOWN)#定義簡單樣式的ComboBox,
simpleLable = wx.StaticText(panel, -1, "簡單樣式:")
simpleComboBox= wx.ComboBox(panel, -1, value = "zero", choices = sampleList, style =wx.CB_SIMPLE)#定義只讀樣式的ComboBox,
readonlyLable = wx.StaticText(panel, -1, "只讀樣式:")
readonlyComboBox= wx.ComboBox(panel, -1, value = "zero", choices = sampleList, style =wx.CB_READONLY)#定義二級聯動下拉列表
cityDict = {'安徽':["合肥", "淮南"], "江蘇":["蘇州","南京"]}#定義第一級菜單
proviceLable = wx.StaticText(panel, -1, "省份:")
proviceComboBox= wx.ComboBox(panel, -1, value = cityDict.keys()[0], choices = cityDict.keys(), style =wx.CB_READONLY)#定義第二級菜單
cityLable = wx.StaticText(panel, -1, "城市:")
cityComboBox= wx.ComboBox(panel, -1, value = cityDict[cityDict.keys()[0]][0], choices = cityDict[cityDict.keys()[0]], style =wx.CB_READONLY)
flexSizer.AddMany([
(commonLable, 0, wx.SHAPED|wx.ALIGN_RIGHT), (commonComboBox, 1, wx.EXPAND), (simpleLable, 0, wx.SHAPED|wx.ALIGN_RIGHT),(simpleComboBox, 1, wx.EXPAND)
,(readonlyLable, 0, wx.SHAPED|wx.ALIGN_RIGHT), (readonlyComboBox, 1, wx.EXPAND),(wx.Size(6,6), 0, wx.SHAPED|wx.ALIGN_RIGHT), (wx.Size(6,6), 1, wx.EXPAND)
,(proviceLable, 0, wx.SHAPED|wx.ALIGN_RIGHT), (proviceComboBox, 1, wx.EXPAND),(wx.Size(6,6), 0, wx.SHAPED|wx.ALIGN_RIGHT), (wx.Size(6,6), 1, wx.EXPAND)
,(cityLable, 0, wx.SHAPED|wx.ALIGN_RIGHT), (cityComboBox, 1, wx.EXPAND),(wx.Size(6,6), 0, wx.SHAPED|wx.ALIGN_RIGHT), (wx.Size(6,6), 1, wx.EXPAND)
])
panel.SetSizerAndFit(flexSizer)#定義 一級下拉列表切換時,刷新二級菜單項的響應事件
self.__ProvinceComboBox =proviceComboBox
self.__SecityDict =cityDict
self.__CityComboBox=cityComboBox
panel.Bind(wx.EVT_COMBOBOX, self.__OnComboBoxSelected, proviceComboBox,)def __OnComboBoxSelected(self, event):
currentProvinceIndex= self.__ProvinceComboBox.GetSelection()if wx.NOT_FOUND == currentProvinceIndex: returnvalue= self.__ProvinceComboBox.GetItems()[currentProvinceIndex]#注意中文在List dict 等存儲時候, utf-8 格式不一致問題
value = value.encode('utf-8')
cityList= self.__SecityDict[value]
self.__CityComboBox.SetItems(cityList)
self.__CityComboBox.SetValue(cityList[0])defmain():
app=wx.PySimpleApp()
ComboBoxFrame().Show()
app.MainLoop()if __name__ == '__main__':
main()