remi
remi copied to clipboard
[question / feature request] Would it be possible to format bits of strings in a remi.gui.TextInput widget
For example have one word in one color, the next word in another color, etc. Is it possible to do that or would it need a lot of work?
Here is an example for you:
import remi.gui as gui
from remi import start, App
import os
class styled_txt(gui.Widget, gui._MixinTextualWidget):
def __init__(self, text, *args, **kwargs):
super(styled_txt, self).__init__(*args, **kwargs)
self.type = 'span'
self.set_text(text)
class MyApp(App):
def main(self):
#creating a container VBox type, vertical (you can use also HBox or Widget)
main_container = gui.VBox(width=300, height=200, style={'margin':'0px auto'})
red = {'color':'red'}
bold = {'font-weight':'bolder'}
blue = {'color':'blue'}
green = {'color':'green'}
label = gui.Label("", children=[styled_txt("text "), styled_txt("red ", style=red), styled_txt("bold ", style=bold),])
#you can of course use append
label.append( styled_txt("blue ", style=blue))
#THE FOLLOWING LINE IS ONLY COMPATIBLE WITH PYTHON 3
label.append( styled_txt("green and bold ", style={**green, **bold}))
main_container.append(label)
# returning the root widget
return main_container
if __name__ == "__main__":
# starts the webserver
with this solution you can't use set_text to set the content of the label. you must use append.
I forgot to say that this is valid only for Label and can't be used for TextInput.
Thanks so much. That was really lovely! Yeah, i've been trying to adapt it for TextInput, well, the reason is i've got a bunch of text I want to present to the user by selectively coloring bits for emphasis.