In my GUI i use TextCtrls for User Inputs. The tool guides the user through some steps where different Inputs are mandatory. So, if a TextCtrl is mandatory but empty i want to highlight them as common on many tools and websites with an Red Border. After some research i noticed that this isn't possible without creating an Custom widget.
So is there a "standard" way to highlight it in some ways other than changing its background color?
Minimal example if somebody need to test anything:
import wx
class Example(wx.Frame):
def __init__(self, *args, **kw):
super(Example, self).__init__(*args, **kw)
self.InitUI()
def InitUI(self):
pnl = wx.Panel(self)
test_text_ctrl = wx.TextCtrl(pnl)
self.SetSize((350, 250))
self.Centre()
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
My native solution would be to place each textctrl over their own panel that can be colored. This would create the illusion of a border around each textctrl. Here's an example:
import wx, traceback
# sets the width of the highlight border
HIGHLIGHT_WIDTH = 2
HIGHLIGHT_COLOR = (255, 0, 0)
class Mainframe(wx.Frame):
def __init__(self, parent=None):
self.bg_color = wx.SystemSettings.GetColour(wx.SYS_COLOUR_MENU)
self.highligt_color = wx.Colour(HIGHLIGHT_COLOR)
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title="Highlight TextCtrl Test", size=wx.Size(500, 300),
style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
self.SetBackgroundColour(self.bg_color)
main_sizer = wx.BoxSizer(wx.VERTICAL)
self.textctrl_panel = wx.Panel(self)
self.textctrl_panel.SetBackgroundColour(self.highligt_color)
textctrl_panel_sizer = wx.BoxSizer(wx.VERTICAL)
self.textctrl = wx.TextCtrl(self.textctrl_panel)
textctrl_panel_sizer.Add(self.textctrl, 0, wx.ALL, HIGHLIGHT_WIDTH)
self.textctrl_panel.SetSizer(textctrl_panel_sizer)
self.textctrl_panel.Layout()
textctrl_panel_sizer.Fit(self.textctrl_panel)
main_sizer.Add(self.textctrl_panel, 0, wx.ALIGN_CENTER_HORIZONTAL, 5)
self.SetSizer(main_sizer)
self.Layout()
self.Centre(wx.BOTH)
self.textctrl.Bind(wx.EVT_TEXT, self.on_text)
# to reduce flickering
self.SetDoubleBuffered(True)
self.CenterOnScreen(wx.BOTH)
self.Show()
def on_text(self, event):
""" triggered every time the text ctrl text is updated, schedules validate_text() to run after the event """
event.Skip()
wx.CallAfter(self.validate_text)
def validate_text(self):
""" sets the textctrl panel background color to give the appearance
of a red highlight if there is no text in the text ctrl """
color = self.bg_color if self.textctrl.GetValue() else self.highligt_color
self.textctrl_panel.SetBackgroundColour(color)
# force the window to repaint
self.textctrl_panel.Refresh()
try:
app = wx.App()
frame = Mainframe()
app.MainLoop()
except:
input(traceback.format_exc())