代码之家  ›  专栏  ›  技术社区  ›  Alexey Orlov

matplotlib。小部件。文本框:更改颜色

  •  2
  • Alexey Orlov  · 技术社区  · 7 年前

    有办法改变吗 TextBox 随时显示文本颜色?我已经试过谷歌了;我的问题看起来太琐碎了,但我还是不知所措。

    文本框 方法:

    : dir(matplotlib.widgets.TextBox)
    Out[63]: 
    [
     ...
     'active',
     'begin_typing',
     'connect_event',
     'disconnect',
     'disconnect_events',
     'drawon',
     'eventson',
     'get_active',
     'ignore',
     'on_submit',
     'on_text_change',
     'position_cursor',
     'set_active',
     'set_val',
     'stop_typing']
    

    AxesWidget 超类方法:

    : dir(matplotlib.widgets.AxesWidget)
    Out[64]: 
    [
     ...
     'active',
     'connect_event',
     'disconnect_events',
     'drawon',
     'eventson',
     'get_active',
     'ignore',
     'set_active']
    

    没有任何暗示。至少在我看来是这样。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Bonlenfum    7 年前

    仅部分回答-不知道更完整的应用程序,不清楚这是否对您有帮助。有两段文字可以更改其颜色:标签和编辑框。下面显示了如何更改每个,一次。

    import matplotlib.widgets
    import matplotlib.pyplot as plt
    
    plt.figure(1)
    ax = plt.subplot(111)
    tb = matplotlib.widgets.TextBox(ax, "Name:", initial="Jane Doe")
    tb.label.set_color('red')      # label color
    tb.text_disp.set_color('blue') # text inside the edit box
    

    如果您只是想让标签文本不同,则这种情况会持续存在。但只要编辑框中的文本( text_disp )更改后,它将再次变为黑色。 这是因为小部件重新创建文本 ( by removing and then re-generating 它将再次变成黑色。

    这个 source for the text create method 没有用户可以修改的任何参数(颜色、字体大小/权重等)或包含为TextBox实例属性。

    您可以编写自己的子类来重写此方法。或者仅仅在输入文本后设置它就足够了?