Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse the wxPython to modify the text box color process

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to parse wxPython to modify the text box color process, the editor feels very practical, so share with you to learn, I hope you can learn something after reading this article, do not say much, follow the editor to have a look.

Due to the need to use wxPython to implement a beautiful new data interface, the text box on this interface can set the border color like the html text box, and the font can be centered vertically.

At that time, I also read a lot of materials and found that wxpython did not provide such a modification method. Later, it took a while to come up with a custom text box control based on wxpython.

The specific ideas are as follows:

1. Remove the border of the existing wxpython wx.TextCtrl control, and then use wx.StaticText to make a border for wx.TextCtrl. (believe that what you see on the interface is just what the developer wants you to see.)

2. This border requires two wx.StaticText controls, so why use two?

A) the simulation frame needs color difference, and because of the color difference, it looks like a frame.

B) first use a wx.StaticText control, set a black background color, and then add a white background on the wx.StaticText control, and the length and width is smaller than the father 2px wx.StaticText control, this interface can 1px the black line. This is the border we need, and the border can be of color and size. (you only need to change the background setting of the parent control and the size of the child wx.StaticText)

C) similarly, put the borderless wx.TextCtrl in this border, set the position, and get a custom text box that can change the border color and center the text vertically.

3. Synthetic schematic diagram

Custom control code:

Import wxclass MyText: "" Custom text box "" def _ init__ (self,parent,pos,size= (80Power36), readOnly= False): self.defaultFontSize= 10 # default font size self.TextCtrlColor = 'white' # text box background color self.defaultBorderColoe =' # EAEAEA' # default border color 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): "" create text box "border = wx.StaticText (parent,-1,', size=size, pos=pos) # create border border.SetBackgroundColour (borderColor) # set the color of the border to display bg = wx.StaticText (border,-1,'', size= ((size [0]-borderSize*2), (size [1]-borderSize*2)), pos= (borderSize,borderSize) if readOnly: # set whether the text box is read-only And bring the frame 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 (240240240)') self.TextCtrlColor = 'rgb (240240240)' 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): "when clicked, focus is set on the text box"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 ()

Test the code:

# coding:utf-8import wxfrom wxpython import Mywxpythonapp = wx.App () frame = wx.Frame (None, title= "Gui Test Editor", pos= (200,200), size= (500,400) panel = wx.Panel (frame) path_text = wx.TextCtrl (panel, size= (260F 36)) my_text = Mywxpython.MyText (panel,pos= (10,50), size= (260F 36)) my_text1 = Mywxpython.MyText (panel,pos= (10,100), size= (260F 36)) ReadOnly=True) my_text.SetBorderColor ('red') frame.Show () app.MainLoop ()

Result picture: the self-contained control above, and the red border below is customized

The above is how to analyze wxPython to modify the text box color process, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report