国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

wxPython修改文本框顏色過程解析

2020-02-15 21:12:33
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了wxPython修改文本框顏色過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

由于工作需要使用wxPython實現一個美觀的新增數據界面,這個界面上的文本框要像html中文本框一樣可以設置邊框顏色,和字體垂直居中。

當時也看了許多資料,發現wxpython并沒有提供這樣的修改方法,后來,花了一段時間,想出基于wxpython,自定義文本框控件。

具體思路如下:

1、 去除現有wxpython 的wx.TextCtrl控件的邊框,再使用wx.StaticText給wx.TextCtrl做一個邊框。(要相信,界面上看到的東西,只是開發人想讓你看到的)

2、 這個邊框需要使用兩個wx.StaticText控件,為啥要用兩個?

a) 模擬邊框是需要色差的,由于色差存在,所以看得像一個邊框。

b) 先使用一個wx.StaticText控件,設置一個黑色背景色,再在這個wx.StaticText控件上添加一個白色背景,并且長寬小于父親2px的wx.StaticText控,這個界面上就能1px的黑色線條。這就是我們需要的邊框,并且這個邊框可以邊框顏色和大小。(只需要改父親控件的背景設,和子wx.StaticText的大小就行)

c) 再同理,來把無邊框的wx.TextCtrl放入這個邊框中,設置位置,就得到了自定義的可以改變邊框顏色和文本垂直居中的文本框

3. 合成示意圖

自定義控件代碼:

import wxclass MyText:  """自定義文本框"""  def __init__(self,parent,pos,size=(80,36),readOnly= False):    self.defaultFontSize= 10 #默認字體大小    self.TextCtrlColor = 'white' #文本框的背景色    self.defaultBorderColoe = '#EAEAEA' #默認邊框顏色    self.textCtrl, self.border,self.bg = self.__CreateTextCtrl(parent,pos,size,self.defaultBorderColoe,readOnly)  def __CreateTextCtrl(self,parent,pos,size,borderColor,readOnly=True, borderSize=1):    """創建文本框"""    border = wx.StaticText(parent, -1, '', size=size, pos=pos) #創建邊框    border.SetBackgroundColour(borderColor)  #設置邊框要展現的顏色    bg = wx.StaticText(border, -1, '', size=((size[0]-borderSize*2), (size[1]-borderSize*2))                , pos=(borderSize,borderSize))    if readOnly:    #設置文本框是否只讀,還有去自帶的邊框      style = wx.TE_READONLY|wx.NO_BORDER    else:      style = wx.NO_BORDER    textCtrl = wx.TextCtrl(bg, -1, size=((size[0]-10),self.defaultFontSize*2)                , pos=(5,(size[1]-2*self.defaultFontSize-borderSize*2)/2),style =style)    font = wx.Font(self.defaultFontSize,wx.DEFAULT,wx.NORMAL,wx.NORMAL,False,'微軟雅黑')    textCtrl.SetFont(font)    if readOnly:      bg.SetBackgroundColour('rgb(240,240,240)')      self.TextCtrlColor = 'rgb(240,240,240)'    else:      bg.SetBackgroundColour(textCtrl.GetBackgroundColour())      self.TextCtrlColor = textCtrl.GetBackgroundColour()    bg.Bind(wx.EVT_LEFT_UP,self.__ClickEvent)    return textCtrl,border,bg  def __ClickEvent(self,evt):    """點擊時焦點設置在文本框上"""    self.textCtrl.SetFocus()  def SetValue(self,value):    if not value:      value = ''    self.textCtrl.SetValue(value)  def GetValue(self):    return self.textCtrl.GetValue()  def SetBorderColor(self,color):    self.border.SetBackgroundColour(color)    self.border.Refresh()  def SetFontColor(self,color):    self.textCtrl.SetForegroundColour(color)    self.textCtrl.SetBackgroundColour(self.TextCtrlColor)  def SetFont(self,font):    self.textCtrl.SetFont(font)  def SetBackgroundColour(self,color):    self.bg.SetBackgroundColour(color)    self.textCtrl.SetBackgroundColour(color)    self.textCtrl.Refresh()            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 秭归县| 鹰潭市| 罗定市| 响水县| 社会| 谷城县| 九江县| 教育| 五峰| 永州市| 澄城县| 天祝| 仙居县| 阳城县| 简阳市| 通山县| 乌兰察布市| 郎溪县| 漳州市| 绥中县| 石家庄市| 丹寨县| 鄂尔多斯市| 武夷山市| 蒲江县| 麻江县| 海林市| 博客| 长子县| 哈尔滨市| 灌南县| 当雄县| 越西县| 宜阳县| 岳西县| 连山| 保定市| 陵川县| 朝阳县| 安徽省| 阿拉善左旗|