From 04a2825854bae66e4f4ab71d5137739a8415280d Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Wed, 3 Feb 2021 13:17:51 -0500 Subject: [PATCH] New demo to show how to make Multline element input justification correct --- ...Demo_Multiline_Elem_Input_Justification.py | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 DemoPrograms/Demo_Multiline_Elem_Input_Justification.py diff --git a/DemoPrograms/Demo_Multiline_Elem_Input_Justification.py b/DemoPrograms/Demo_Multiline_Elem_Input_Justification.py new file mode 100644 index 00000000..7eebf14c --- /dev/null +++ b/DemoPrograms/Demo_Multiline_Elem_Input_Justification.py @@ -0,0 +1,46 @@ +""" + Multline Element - Input Justification + + The justification of text for the Multiline element defaults to Left Justified + Because of the way tkinter's widget works, setting the justification when creating the element + is not enough to provide the correct justification. + + The demo shows you the technique to achieve justified input + + Key points: + * Enable events on the multiline + * Add 2 lines of code to your event loop + * If get mline element event + * Set the contents of the multline to be the correct justificaiton + + Copyright 2021 PySimpleGUI +""" + +import PySimpleGUI as sg + +def main(): + justification = 'l' # start left justified + + layout = [[sg.Text('Multiline Element Input Justification')], + [sg.Multiline(size=(40,10), key='-MLINE-', justification=justification, enable_events=True, autoscroll=True)], + # We'll be fancy and allow user to choose which justification to use in the demo + [sg.Radio('Left', 0, True, k='-L-'), sg.Radio('Center', 0, k='-C-'),sg.Radio('Right', 0, k='-R-'),], + [sg.Button('Go'), sg.Button('Exit')]] + + window = sg.Window('Window Title', layout, keep_on_top=True, resizable=True, finalize=True) + + while True: # Event Loop + event, values = window.read() + if event == sg.WIN_CLOSED or event == 'Exit': + break + # Get desired justication from radio buttons. You don't need this if you know your justification already + justification = 'l' if values['-L-'] else 'r' if values['-R-'] else 'c' + + # This is the important bit of code. It sets the current contents of the multiline to be the correct justification + if event == '-MLINE-': + window['-MLINE-'].update(values['-MLINE-'][:-1], justification=justification) + + window.close() + +if __name__ == '__main__': + main() \ No newline at end of file