wxPython - 如何 "highlight" 一个 TextCtrl?
wxPython - How to "highlight" an TextCtrl?
在我的 GUI 中,我使用 TextCtrls 进行用户输入。该工具会指导用户完成一些必须输入不同输入的步骤。因此,如果 TextCtrl 是必需的但为空,我想突出显示它们在许多带有红色边框的工具和网站上很常见。
经过一些研究,我注意到如果不创建自定义小部件,这是不可能的。
那么除了改变背景颜色之外,还有什么方法可以 "standard" 突出显示它吗?
如果有人需要测试任何东西的最小示例:
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()
我的原始解决方案是将每个 textctrl 放在它们自己的可以着色的面板上。这会在每个 textctrl 周围产生边框的错觉。这是一个例子:
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())
在我的 GUI 中,我使用 TextCtrls 进行用户输入。该工具会指导用户完成一些必须输入不同输入的步骤。因此,如果 TextCtrl 是必需的但为空,我想突出显示它们在许多带有红色边框的工具和网站上很常见。 经过一些研究,我注意到如果不创建自定义小部件,这是不可能的。
那么除了改变背景颜色之外,还有什么方法可以 "standard" 突出显示它吗?
如果有人需要测试任何东西的最小示例:
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()
我的原始解决方案是将每个 textctrl 放在它们自己的可以着色的面板上。这会在每个 textctrl 周围产生边框的错觉。这是一个例子:
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())