On PySimpleGUI, how can I bold only a letter in a word with sg.Text(), like Hello?
I can only bold the entire word or with space between "H" and "ello" when I do [sg.Text("H", font=("bold")), sg.Text("ello").] Thanks.
On PySimpleGUI, how can I bold only a letter in a word with sg.Text(), like Hello?
I can only bold the entire word or with space between "H" and "ello" when I do [sg.Text("H", font=("bold")), sg.Text("ello").] Thanks.
Credit to Jason Yang (in comments); here is how you can use a Multiline element to bold a particular part of your text:
import PySimpleGUI as sg
# Note that the layout includes a Multiline element with a key to reference it, and it is disabled to keep users from editing the text
layout = [
[sg.Multiline("", key='-VARIEDTEXT-', disabled=True)]
]
window = sg.Window("Test", layout, finalize=True)
# Use the .print function to add segments of text with specified font types
window["-VARIEDTEXT-"].print("H", font='bold', end='')
window["-VARIEDTEXT-"].print("ello", end='')
# This is just the regular window settings
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
In order for the .print
function to work, you must set the window to finalize=True
.
Also, by default, end='\n'
which will create a new line. This is why you must set end=''
to keep it all on the same line.