這篇文章主要介紹了wxpython自定義下拉列表框過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
自定義wxpython下拉列表框,支持修改邊框顏色,按鈕圖標的動態(tài)變換
原理同前兩片文章一樣,使用了兩個wx.staticText做邊框,一個文本框來顯示下拉列表的數(shù)據(jù),和一個圖片按鈕,實現(xiàn)下拉的標志,和一個自帶的列表框,
影藏該列表框,不要原來的樣式,這里只需要使用它的展示列表的數(shù)據(jù)功能
自定義列表框的代碼:
class MyComBox: """自定義下拉列表框""" def __init__(self,parent,pos,size=(200,35),choices=[],readOnly=False,borderColor='#EAEAEA',borderSize=1): self.defaultfontSize = 10 self.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): #創(chuàng)建邊框 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 #創(chuàng)建數(shù)據(jù)展示框 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') #點擊文本框顯示數(shù)據(jù) if not readOnly: self.textCtrl.Bind(wx.EVT_LEFT_DOWN,self.__OnClick) #創(chuàng)建下拉點擊按鈕 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) #構(gòu)建列表框,展示列表的數(shù)據(jù) 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) #設置只讀情況的樣式 if readOnly: 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) return self.textCtrl,self.chooseBox,border,arrow_button def __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) def GetValue(self): return self.textCtrl.GetValue() def SetValue(self,value): if not value: value = u'請選擇' self.textCtrl.SetValue(value) self.combox.SetValue(value) def SetList(self,list): """設置下拉列表中的數(shù)據(jù)""" self.combox.SetItems(list) def SetBorderColor(self,color): self.background.SetBackgroundColour(color) def SetFont(self,font): self.textCtrl.SetFont(font) def SetForegroundColour(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)
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
名稱欄目:wxpython自定義下拉列表框過程圖解-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://vcdvsql.cn/article32/eigsc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設計公司、動態(tài)網(wǎng)站、手機網(wǎng)站建設、網(wǎng)站策劃、品牌網(wǎng)站建設、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容