classMyComBox:"""自定义下拉列表框"""
def __init__(self,parent,pos,size=(200,35),choices=[],readOnly=False,borderColor=‘#EAEAEA‘,borderSize=1):
self.defaultfontSize= 10self.defaultBorderColor= ‘#EAEAEA‘self.defaultFontColor= ‘black‘self.textCtrl,self.combox,self.background,self.arrow_button= self.__CreateComBox(parent,pos,size,
choices,readOnly,borderColor,borderSize)def __CreateComBox(self,parent,pos,size,list,readOnly,borderColor,borderSize):#创建边框
border = wx.StaticText(parent,-1,"",pos=pos,size=size)
border.SetBackgroundColour(borderColor)
bg= wx.StaticText(border,-1,"",size=((size[0]-borderSize*2),(size[1]-borderSize*2)),pos=(borderSize,borderSize))
style= wx.TE_READONLY |wx.NO_BORDER#创建数据展示框
self.textCtrl = wx.TextCtrl(bg,-1,size=((size[0]-30),(self.defaultfontSize*2)),
pos=(5,(size[1]-2*self.defaultfontSize-borderSize*2)/2),style=style)
self.textCtrl.SetBackgroundColour(‘white‘)#点击文本框显示数据
if notreadOnly:
self.textCtrl.Bind(wx.EVT_LEFT_DOWN,self.__OnClick)#创建下拉点击按钮
bmp = wx.Image("xia.jpg",wx.BITMAP_TYPE_ANY).ConvertToBitmap()
arrow_button= wx.BitmapButton(bg,-1,bmp,size = (20,size[1]),pos=(size[0]-22,0),style =wx.NO_BORDER)#构建列表框,展示列表的数据
self.chooseBox = wx.ComboBox(parent,-1,value="",size=(size[0],-1),pos = (pos[0],pos[1]+10),choices=list,style=wx.TE_READONLY)
self.chooseBox.Hide()
self.chooseBox.Bind(wx.EVT_COMBOBOX_CLOSEUP,self.__GetValue)#设置显示下列列表按钮
arrow_button.SetBackgroundColour(‘white‘)
font= wx.Font(self.defaultfontSize,wx.DEFAULT,wx.NORMAL,wx.NORMAL,False,‘微软雅黑‘)
self.textCtrl.SetFont(font)#设置只读情况的样式
ifreadOnly:
bg.SetBackgroundColour(‘rgb(240,240,240)‘)
self.textCtrl.SetBackgroundColour(‘rgb(240,240,240)‘)
arrow_button.SetBackgroundColour(‘rgb(240,240,240)‘)else:#bg.SetBackgroundColour(self.textCtrl.GetBackgroundColour())
arrow_button.Bind(wx.EVT_BUTTON,self.__OnClick)returnself.textCtrl,self.chooseBox,border,arrow_buttondef __GetValue(self,event):if self.chooseBox.GetValue()!=‘‘:
self.textCtrl.SetValue(self.chooseBox.GetValue())
self.chooseBox.Hide()
bmp= wx.Image("xia.jpg",wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.arrow_button.SetBitmap(bmp)if self.chooseBox.GetValue()!=‘请选择‘:
self.textCtrl.SetForegroundColour(self.defaultFontColor)def __OnClick(self,event):
self.chooseBox.Show()
self.chooseBox.Popup()
bmp= wx.Image("shang.jpg", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.arrow_button.SetBitmap(bmp)defGetValue(self):returnself.textCtrl.GetValue()defSetValue(self,value):if notvalue:
value= u‘请选择‘self.textCtrl.SetValue(value)
self.combox.SetValue(value)defSetList(self,list):"""设置下拉列表中的数据"""self.combox.SetItems(list)defSetBorderColor(self,color):
self.background.SetBackgroundColour(color)defSetFont(self,font):
self.textCtrl.SetFont(font)defSetForegroundColour(self,color):
self.textCtrl.SetForegroundColour(color)def Bind(self, event, handler, source=None, id=wx.ID_ANY, id2=wx.ID_ANY):
self.textCtrl.Bind(event,handler)