wxpython下拉选择框_wxpython自定义下拉列表框

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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值