wxPython修改文本框顏色過程解析
這篇文章主要介紹了wxPython修改文本框顏色過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
由于工作需要使用wxPython實(shí)現(xiàn)一個(gè)美觀的新增數(shù)據(jù)界面,這個(gè)界面上的文本框要像html中文本框一樣可以設(shè)置邊框顏色,和字體垂直居中。
當(dāng)時(shí)也看了許多資料,發(fā)現(xiàn)wxpython并沒有提供這樣的修改方法,后來,花了一段時(shí)間,想出基于wxpython,自定義文本框控件。
具體思路如下:
1、 去除現(xiàn)有wxpython 的wx.TextCtrl控件的邊框,再使用wx.StaticText給wx.TextCtrl做一個(gè)邊框。(要相信,界面上看到的東西,只是開發(fā)人想讓你看到的)
2、 這個(gè)邊框需要使用兩個(gè)wx.StaticText控件,為啥要用兩個(gè)?
a) 模擬邊框是需要色差的,由于色差存在,所以看得像一個(gè)邊框。
b) 先使用一個(gè)wx.StaticText控件,設(shè)置一個(gè)黑色背景色,再在這個(gè)wx.StaticText控件上添加一個(gè)白色背景,并且長寬小于父親2px的wx.StaticText控,這個(gè)界面上就能1px的黑色線條。這就是我們需要的邊框,并且這個(gè)邊框可以邊框顏色和大小。(只需要改父親控件的背景設(shè),和子wx.StaticText的大小就行)
c) 再同理,來把無邊框的wx.TextCtrl放入這個(gè)邊框中,設(shè)置位置,就得到了自定義的可以改變邊框顏色和文本垂直居中的文本框

3. 合成示意圖
自定義控件代碼:
import wx
class MyText:
"""自定義文本框"""
def __init__(self,parent,pos,size=(80,36),readOnly= False):
self.defaultFontSize= 10 #默認(rèn)字體大小
self.TextCtrlColor = 'white' #文本框的背景色
self.defaultBorderColoe = '#EAEAEA' #默認(rèn)邊框顏色
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):
"""創(chuàng)建文本框"""
border = wx.StaticText(parent, -1, '', size=size, pos=pos) #創(chuàng)建邊框
border.SetBackgroundColour(borderColor) #設(shè)置邊框要展現(xiàn)的顏色
bg = wx.StaticText(border, -1, '', size=((size[0]-borderSize*2), (size[1]-borderSize*2))
, pos=(borderSize,borderSize))
if readOnly: #設(shè)置文本框是否只讀,還有去自帶的邊框
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):
"""點(diǎn)擊時(shí)焦點(diǎn)設(shè)置在文本框上"""
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()
測試代碼:
# 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)
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')
frame.Show()
app.MainLoop()

結(jié)果圖:上面的自帶的控件,下面紅色邊框是自定義的
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python機(jī)器學(xué)習(xí)之scikit-learn庫中KNN算法的封裝與使用方法
這篇文章主要介紹了Python機(jī)器學(xué)習(xí)之scikit-learn庫中KNN算法的封裝與使用方法,結(jié)合實(shí)例形式分析了scikit-learn庫中KNN算法的相關(guān)調(diào)用與使用技巧,需要的朋友可以參考下2018-12-12
Python寫入MySQL數(shù)據(jù)庫的三種方式詳解
Python 讀取數(shù)據(jù)自動(dòng)寫入 MySQL 數(shù)據(jù)庫,這個(gè)需求在工作中是非常普遍的,主要涉及到 python 操作數(shù)據(jù)庫,讀寫更新等。本文總結(jié)了Python寫入MySQL數(shù)據(jù)庫的三種方式,需要的可以參考一下2022-06-06
Python selenium模塊實(shí)現(xiàn)定位過程解析
這篇文章主要介紹了python selenium模塊實(shí)現(xiàn)定位過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07

