【问题标题】:wxPython - How to "highlight" an TextCtrl?wxPython - 如何“突出显示”一个 TextCtrl?
【发布时间】:2023-03-29 20:27:01
【问题描述】:

在我的 GUI 中,我使用 TextCtrls 进行用户输入。该工具将引导用户完成一些必须输入不同输入的步骤。因此,如果 TextCtrl 是强制性的但为空,我想在许多带有红色边框的工具和网站上将它们突出显示为常见。 经过一些研究,我注意到如果不创建自定义小部件,这是不可能的。

那么除了改变它的背景颜色之外,有没有一种“标准”的方式来突出它?

如果有人需要测试任何东西的最小示例:

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() 

【问题讨论】:

    标签: python python-3.x wxpython


    【解决方案1】:

    我的原生解决方案是将每个 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())
    

    【讨论】:

    • 感谢您的回答。有点难过,这是最好的事情之一:-D
    猜你喜欢
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    相关资源
    最近更新 更多