您好,登錄后才能下訂單哦!
這篇文章主要介紹了wxpython自定義下拉列表框過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
自定義wxpython下拉列表框,支持修改邊框顏色,按鈕圖標(biāo)的動(dòng)態(tài)變換
原理同前兩片文章一樣,使用了兩個(gè)wx.staticText做邊框,一個(gè)文本框來顯示下拉列表的數(shù)據(jù),和一個(gè)圖片按鈕,實(shí)現(xiàn)下拉的標(biāo)志,和一個(gè)自帶的列表框,
影藏該列表框,不要原來的樣式,這里只需要使用它的展示列表的數(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') #點(diǎn)擊文本框顯示數(shù)據(jù) if not readOnly: self.textCtrl.Bind(wx.EVT_LEFT_DOWN,self.__OnClick) #創(chuàng)建下拉點(diǎn)擊按鈕 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) #設(shè)置顯示下列列表按鈕 arrow_button.SetBackgroundColour('white') font = wx.Font(self.defaultfontSize,wx.DEFAULT,wx.NORMAL,wx.NORMAL,False,'微軟雅黑') self.textCtrl.SetFont(font) #設(shè)置只讀情況的樣式 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()!='請(qǐng)選擇': 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'請(qǐng)選擇' self.textCtrl.SetValue(value) self.combox.SetValue(value) def SetList(self,list): """設(shè)置下拉列表中的數(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)
圖片:, ,這個(gè)需要下載下去,或者自己找漂亮的圖片
測試代碼:
# coding:utf-8 import wx from wxpython import Mywxpython app = wx.App() frame = wx.Frame(None, title="Gui Test Editor", pos=(1000, 200), size=(500, 400)) panel = wx.Panel(frame) panel.SetBackgroundColour('white') # path_text = wx.TextCtrl(panel, size=(260, 36)) # # my_text = Mywxpython.MyText(panel,pos=(10, 50),size=(260,36)) # my_text1 = Mywxpython.MyText(panel,pos=(10, 100),size=(260,36),readOnly=True) # my_text.SetBorderColor('red') list = ['1','2','3','4'] #wx.ComboBox(panel,-1,value="",size=(80,-1),pos = (100,110),choices=list,style=wx.TE_READONLY) #my_button = Mywxpython.MyButton(panel,title="點(diǎn)我",pos=(10, 150)) combox = Mywxpython.MyComBox(panel,choices=['1','2','3','4'],pos=(10, 150)) #combox .SetValue("請(qǐng)選擇") frame.Show() app.MainLoop()
結(jié)果圖:
按鈕又有點(diǎn)丑,需要自己定義,搞兩個(gè)好看得圖標(biāo),
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。