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]))]]
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()
See how the text in the edit box changes colour from red to white to red?
Related
How do you clear text in an input box PySimpleGui? I'm trying window ['-INPUT-'] ('') but I recieve a key error. I want it so that the text in the box gets replaced with an empty string after each iteration so the user doesn't have to delete the text themselves.
I want it so that the text in the box gets replaced with an empty
string after each iteration so the user doesn't have to delete the
text themselves.
Ah!
There is a parameter meant specifically for this purpose.
Set do_not_clear=False
import PySimpleGUI as sg
layout = [ [sg.Text('Input element that clears after every read')],
[sg.Input('Initial text', key='-I-', do_not_clear=False)],
[sg.Button('Go'), sg.Button('Exit')] ]
window = sg.Window('Input auto-clear', layout)
while True: # Event Loop
event, values = window.read()
if event in (None, 'Exit'):
break
print(event, values)
window.close()
I am using PySimpleGUI to create a text output box.
On opening the program, I would like the Output to display some default text before any button is pressed.
How can I get this to occur? window.Read() waits for a button press. window.refresh() doesn't appear to force the text to the window.
import PySimpleGUI as sg
initialString = "I want this text to display on window opening."
def gui2():
layout = [
[sg.Output(size=(90,20), background_color='black', text_color='white')],
[sg.Button('Do things'), sg.Button('Exit')]
]
window = sg.Window("Funny Title", layout)
#window.read() #I need to press a button before the text will display
#window.refresh() #doesn't refresh the output
print(initialString)
#window.refresh() #doesn't refresh the output
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
elif event == 'Do things':
print("You pressed the button")
window.close()
gui2()
and of course, the answer is found 5 minutes after I post.
window = sg.Window("Funny Title", layout, finalize = True)
This fixes the window, and the subsequent print statement appears in the Output as wanted.
.
Edit:
From the comment below, I've also changed the layout from sg.Outline to sg.Multiline:
sg.Multiline(size=(90,20),
background_color='black',
text_color='white',
reroute_stdout=True,
reroute_stderr=True,
autoscroll = True)],
I'm creating a textbox as follows:
sg.Text(size=(57, 10), background_color='white', text_color='red',
key='_console')
And it works fine, except the text isn't selectable!
I want the user to be able to copy the message to clipboard (by mouse selection and "copy").
How can it be done?
thx
According to the git hub issue related to this, the way to go about doing this is to create a read-only input and format it to look like the normal text elements:
https://github.com/PySimpleGUI/PySimpleGUI/issues/2928
import PySimpleGUI as sg
sg.theme('Dark Red')
layout = [ [sg.Text('My Window')],
[sg.InputText('You can select this text', use_readonly_for_disable=True, disabled=True, key='-IN-')],
[sg.Button('Go'), sg.Button('Exit')] ]
window = sg.Window('Window Title', layout, finalize=True)
window['-IN-'].Widget.config(readonlybackground=sg.theme_background_color())
window['-IN-'].Widget.config(borderwidth=0)
while True: # Event Loop
event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':
break
window.close()
I am following the PySimpleGUI documentation and making my own edits as I go along. I am very new to it and have had experience with using Tkinter. There is a Textbox in Tkinter which you an get with the code Text(window, width=?, height=?, wrap=WORD, background=yellow). However in PySimpleGUI with similar code: layout = [[sg.Text('Some text on Row 1')]] - creates a label. My code is:
import PySimpleGUI as sg
sg.theme('DarkAmber') # Add a touch of color
# All the stuff inside your window.
layout = [ [sg.Text('Some text on Row 1')],
[sg.Text('Enter something on Row 2'), sg.InputText()],
[sg.Button('Ok'), sg.Button('Close Window')],
[sg.Text('This is some text', font='Courier 12', text_color='blue', background_color='green')],
[sg.Listbox(values=('value1', 'value2', 'value3'), size=(30, 2), key='_LISTBOX_')]]
# Create the Window
window = sg.Window('Test', layout).Finalize()
window.Maximize()
# Event Loop to process "events" and get the "values" of the inputs
while True:
event, values = window.read()
if event in (None, 'Close Window'): # if user closes window or clicks cancel
break
print('You entered ', values[0])
window.close()
I have attempted using PySimpleGui: How to enter text in the text box? but the Text Box here is actually a list box:
which is nothing like the TextBox I want:
The TextBox is surrounded by the red lines. Can someone please help me find the code that will give me the TextBox that I desire?
You can use sg.Multiline(...) which is Text widget of tkinter.
To get the content of the sg.Multiline, you can assign an unique key to it and use this key to get its content in the values dict.
Below is an example based on your code:
import PySimpleGUI as sg
sg.theme('DarkAmber') # Add a touch of color
# All the stuff inside your window.
layout = [ [sg.Text('Some text on Row 1')],
[sg.Text('Enter something on Row 2'), sg.InputText()],
[sg.Button('Ok'), sg.Button('Close Window')],
[sg.Multiline(size=(30, 5), key='textbox')]] # identify the multiline via key option
# Create the Window
window = sg.Window('Test', layout).Finalize()
#window.Maximize()
# Event Loop to process "events" and get the "values" of the inputs
while True:
event, values = window.read()
if event in (None, 'Close Window'): # if user closes window or clicks cancel
break
print('You entered in the textbox:')
print(values['textbox']) # get the content of multiline via its unique key
window.close()
I am very new to PySimpleGUI. I am building a GUI for a desktop application and wanted to use a calendar. But there came a problem with retrieving the value of Calendar Button events in the while loop without timeouts in window.read() when I chose from Calendar Picker Popup. I tried to get its value using event == 'CalendarButton's text', but couldn't, though its button text changes every time if you choose to set a different date. Can you please help with that, or how can I get its (or any element's) value using its key inside the while loop. Or at least can I use Tkinter calendar package. If so, how can I connect Tkinter Calendar with PySimpleGUI window, will I have to use an element's key bindings with Tkinter?
Here's my code for Calendar Button definition which I put into my window's layout:
sg.CalendarButton('Calendar', target='_CALENDAR_', pad=None, font=('MS Sans Serif', 10, 'bold'),
button_color=('red', 'white'), key='_CALENDAR_', format=('%d %B, %Y'))
and here's the while loop events handling part
# LOOP EVENTS #
while True:
# READ EVENT VALUES #
event, values = window.read()
# EXIT OR CLOSE EVENT #
if event is None or event == 'Exit':
break
# BUTTON EVENTS
# CALENDAR BUTTON CLICKED EVENT #
if event == 'Calendar':
window['_CALENDAR_'](disabled=True)
# CLOSE WINDOW
window.close()
# POPUP
sg.Popup('Title',
'The results of the window.',
'The button clicked was "{}"'.format(event),
'The values are', values)
Also, I cannot see this event's value in the sg.Popup() output after I exit the window
'EDITED' Sorry, there were errors in sg.Popup(). Now fixed it.
The way to both save the value and get the event is to create a hidden input field. Enable events for the input field and you'll get an event when the calendar fills in the input field that you set as the target.
import PySimpleGUI as sg
layout = [ [sg.Text('Calendar example')],
[sg.In(key='-CAL-', enable_events=True, visible=False), sg.CalendarButton('Calendar', target='-CAL-', pad=None, font=('MS Sans Serif', 10, 'bold'),
button_color=('red', 'white'), key='_CALENDAR_', format=('%d %B, %Y'))],
[sg.Exit()]]
window = sg.Window('Calendar', layout)
while True: # Event Loop
event, values = window.read()
print(event, values)
if event in (None, 'Exit'):
break
window.close()