介绍
本篇文章为大家展示了怎么在wxPython中修改文本框颜色,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
具体思路如下:
1,去除现有wxPython的wx.TextCtrl控件的边框,再使用wx.StaticText给wx。TextCtrl做一个边框。(要相信,界面上看到的东西,只是开发人想让你看到的)
2,这个边框需要使用两个wx.StaticText控件,为啥要用两个?
)模拟边框是需要色差的,由于色差存在,所以看得像一个边框。
b)先使用一个wx.StaticText控件,设置一个黑色背景色,再在这个wx.StaticText控件上添加一个白色背景,并且长宽小于父亲2 px的wx.StaticText控,这个界面上就能1 px的黑色线条。这就是我们需要的边框,并且这个边框可以边框颜色和大小。(只需要改父亲控件的背景设,和子wx.StaticText的大小就行)
c)再同理,来把无边框的wx。TextCtrl放入这个边框中,设置位置,就得到了自定义的可以改变边框颜色和文本垂直居中的文本框
3。合成示意图
自定义控件代码:
import wx class MyText: “““才能自定义文本框“““ def 才能;__init__(自我、父母、pos、大?(80年,36),只读的=,False): ,,,self.defaultFontSize=, 10, #默认字体大小 ,,,self.TextCtrlColor =, & # 39;白色# 39;,#文本框的背景色 ,,,self.defaultBorderColoe =, & # 39; # EAEAEA& # 39;, #默认边框颜色 ,,,,,,self.textCtrl self.border, self.bg =, self.__CreateTextCtrl(父母、pos、大小self.defaultBorderColoe只读) def 才能;__CreateTextCtrl(自我、父母、pos、大小、borderColor readOnly=True,, borderSize=1): ,,,“““创建文本框“““ ,,,border =, wx.StaticText(父母,,1,,& # 39;& # 39;,,大?大小,pos=pos), #创建边框 ,,,border.SetBackgroundColour (borderColor),, #设置边框要展现的颜色 ,,,bg =, wx.StaticText(边界,,1,,& # 39;& # 39;,,大?(([0]-borderSize * 2大小),(大小[1]-borderSize * 2)) ,,,,,,,,,,,,,,,,,pos=(borderSize borderSize)) ,,,if 只读的:,,,,#设置文本框是否只读,还有去自带的边框 ,,,,,style =, wx.TE_READONLY | wx.NO_BORDER ,,,: ,,,,,style =wx.NO_BORDER ,,,textCtrl =, wx.TextCtrl (bg,, 1,,大?((-10年大小[0]),self.defaultFontSize * 2) ,,,,,,,,,,,,,,,,,pos=(5(大小[1]2 * self.defaultFontSize-borderSize * 2)/2), style =风格) ,,,font =, wx.Font (self.defaultFontSize、wx.DEFAULT wx.NORMAL, wx.NORMAL,假的,& # 39;微软雅黑& # 39;) ,,,textCtrl.SetFont(字体) ,,,if 只读的: ,,,,,bg.SetBackgroundColour (& # 39; rgb (240240240) & # 39;) ,,,,,self.TextCtrlColor =, & # 39; rgb (240240240) & # 39; ,,,: ,,,,,bg.SetBackgroundColour (textCtrl.GetBackgroundColour ()) ,,,,,self.TextCtrlColor =, textCtrl.GetBackgroundColour () ,,,bg.Bind (wx.EVT_LEFT_UP self.__ClickEvent) ,,,return textCtrl,边境,bg def 才能;__ClickEvent(自我,evt): ,,,“““点击时焦点设置在文本框上“““ ,,,self.textCtrl.SetFocus () def 才能SetValue(自我价值): ,,,if not 价值: ,,,,,value =, & # 39; & # 39; ,,,self.textCtrl.SetValue(值) def 才能GetValue(自我): ,,,return self.textCtrl.GetValue () def 才能SetBorderColor(自我、颜色): ,,,self.border.SetBackgroundColour(颜色) ,,,self.border.Refresh () def 才能SetFontColor(自我、颜色): ,,,self.textCtrl.SetForegroundColour(颜色) ,,,self.textCtrl.SetBackgroundColour (self.TextCtrlColor) def 才能SetFont(自我、字体): ,,,self.textCtrl.SetFont(字体) def 才能SetBackgroundColour(自我、颜色): ,,,self.bg.SetBackgroundColour(颜色) ,,,self.textCtrl.SetBackgroundColour(颜色) ,,,self.textCtrl.Refresh ()
测试代码:
#,编码:utf - 8 import 天气 得到wxpython import  Mywxpython 时间=app wx.App () frame =, wx.Frame(没有,,title=癎ui  Test Editor",, pos=(1000,, 200),大?(500,,400)) 时间=panel wx.Panel(框架) path_text =, wx.TextCtrl(面板,大?(260年,36)) my_text =, Mywxpython.MyText(面板、pos=(10, 50),大?(260年,36)) my_text1 =, Mywxpython.MyText(面板、pos=(10, 100),大?(260年,36),只读的=True) my_text.SetBorderColor(& # 39;红色# 39;) frame.Show () null怎么在wxPython中修改文本框颜色