# -*- coding: UTF-8 -*- import wx class ComboBoxFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, 'Combo Box Example', size=(1300, 750)) panel = wx.Panel(self, -1) dict1 = { u'甘肃': [u'张掖', u'武威'], u'江苏': [u'南通', u'南京'] } dict2 = { u'张掖': [u'高台', u'肃南'], u'武威': [u'古浪', u'民勤'], u'南通': [u'海门县', u'海安县'], u'南京': [u'玄武区', u'朱雀区'] } proviceLable = wx.StaticText(panel, -1, "省份:", pos=(80, 60)) # 省份的静态说明文本框 proviceComboBox = wx.ComboBox(panel, -1, value=list(dict1.keys())[0], choices=list(dict1.keys()), style=wx.CB_READONLY, pos=(170, 55)) # 省份的下拉文本框 cityLable = wx.StaticText(panel, -1, "市区:", pos=(80, 120)) # 市区~ shiquComboBox = wx.ComboBox(panel, -1, value=dict1[list(dict1.keys())[0]][0], choices=dict1[list(dict1.keys())[0]], style=wx.CB_READONLY, pos=(170, 115)) # 市区~ value1 = dict1[list(dict1.keys())[0]][0] # 这个变量用于得到省份字典的第一个key值所对应的value值,主要用于对县级下拉列表的初始化 xianquLable = wx.StaticText(panel, -1, "县区:", pos=(80,180)) # 县区~ xianquComboBox = wx.ComboBox(panel, -1, value=dict2[value1][0], choices=dict2[value1], style=wx.CB_READONLY, pos=(170, 175)) # 县区~ # 定义一级列表被点击时响应二级列表和三级列表的刷新事件 self.__ProvinceComboBox = proviceComboBox self.__SecityDict = dict1 self.__CityComboBox = shiquComboBox panel.Bind(wx.EVT_COMBOBOX, self.__OnComboBoxSelected1, proviceComboBox, ) # 定义二级列表被点击时响应三级列表的刷新事件 self.__SecityDict1 = dict2 self._XianquCombobox = xianquComboBox panel.Bind(wx.EVT_COMBOBOX, self.__OnComboBoxSelected2, shiquComboBox, ) # 定义三级列表被点击时响应的事件 panel.Bind(wx.EVT_COMBOBOX, self.__OnComboBoxSelected3, xianquComboBox, ) def __OnComboBoxSelected(self, event): currentProvinceIndex = self.__ProvinceComboBox.GetSelection() if wx.NOT_FOUND == currentProvinceIndex: return value = 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]) def __OnComboBoxSelected1(self, event): # 省份列表点击的响应函数 currentProvinceIndex1 = self.__ProvinceComboBox.GetSelection() print currentProvinceIndex1 if wx.NOT_FOUND == currentProvinceIndex1: return value1 = self.__ProvinceComboBox.GetItems()[currentProvinceIndex1] print self.__SecityDict[value1] cityList = self.__SecityDict[value1] self.__CityComboBox.SetItems(cityList) self.__CityComboBox.SetValue(cityList[0]) self.__OnComboBoxSelected2(self) def __OnComboBoxSelected2(self, event): # 市区列表点击的响应函数 currentShiquIndex = self.__CityComboBox.GetSelection() if wx.NOT_FOUND == currentShiquIndex: return value = self.__CityComboBox.GetItems()[currentShiquIndex] cityList = self.__SecityDict1[value] self._XianquCombobox.SetItems(cityList) self._XianquCombobox.SetValue(cityList[0]) def __OnComboBoxSelected3(self, event): # 响应县区下拉文本框点击的响应函数 currentXianquIndex = self._XianquCombobox.GetSelection() value = self._XianquCombobox.GetItems()[currentXianquIndex] self._cityname = value def main(): app = wx.PySimpleApp() ComboBoxFrame().Show() app.MainLoop() if __name__ == '__main__': main()