Mark McGookin
Mark McGookin

Reputation: 1032

PySimpleGui - Possible to alter font per line of sg.Output

I have a simple PySimpleGui screen that I am using for a 'chat' application. I would like to be able to differ between people's messages by changing the font (Ideally colour) used for each row printed... I just output using print('message') but was wondering if I could add something to be like print('message', color=red, weight=bold) or something like that... so I could bold my own messages and have others come in with different colours.

The chat window code is just this:

chatWindow = [[sg.Output(size=(90, 37), font=('Helvetica 10'))],
                [sg.Multiline(size=(75, 5), enter_submits=False, key='-QUERY-', do_not_clear=False),
                sg.Button('SEND', button_color=(sg.YELLOWS[0], sg.BLUES[0]), bind_return_key=True),
                sg.Button('EXIT', button_color=(sg.YELLOWS[0], sg.GREENS[0]))]]

enter image description here

Upvotes: 0

Views: 4498

Answers (1)

Alan
Alan

Reputation: 3042

Close. It's not color=red though, it's text_color='red' as in print(1,2,3,4,end='', text_color='red', background_color='yellow').

Have a look at the appropriate Cookbook recipe - Recipe Printing - #3/4 Print to Multiline Element

Code reprinted here for convenience:

import PySimpleGUI as sg

layout = [  [sg.Text('Demonstration of Multiline Element Printing')],
            [sg.MLine(key='-ML1-'+sg.WRITE_ONLY_KEY, size=(40,8))],
            [sg.MLine(key='-ML2-'+sg.WRITE_ONLY_KEY,  size=(40,8))],
            [sg.Button('Go'), sg.Button('Exit')]]

window = sg.Window('Window Title', layout, finalize=True)


# Note, need to finalize the window above if want to do these prior to calling window.read()
window['-ML1-'+sg.WRITE_ONLY_KEY].print(1,2,3,4,end='', text_color='red', background_color='yellow')
window['-ML1-'+sg.WRITE_ONLY_KEY].print('\n', end='')
window['-ML1-'+sg.WRITE_ONLY_KEY].print(1,2,3,4,text_color='white', background_color='green')
counter = 0

while True:             # Event Loop
    event, values = window.read(timeout=100)
    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    if event == 'Go':
        window['-ML1-'+sg.WRITE_ONLY_KEY].print(event, values, text_color='red')
    window['-ML2-'+sg.WRITE_ONLY_KEY].print(counter)
    counter += 1
window.close()

enter image description here

See how the text in the edit box changes colour from red to white to red?

Upvotes: 4

Related Questions