- PySimpleGUI Tutorial
- PySimpleGUI - Home
- PySimpleGUI - Introduction
- PySimpleGUI - Environment Setup
- PySimpleGUI - Hello World
- PySimpleGUI - Popup Windows
- PySimpleGUI - Window Class
- PySimpleGUI - Element Class
- PySimpleGUI - Events
- PySimpleGUI - Menubar
- PySimpleGUI - Matplotlib Integration
- PySimpleGUI - Working with PIL
- PySimpleGUI - Debugger
- PySimpleGUI - Settings
- PySimpleGUI Useful Resources
- PySimpleGUI - Quick Guide
- PySimpleGUI - Useful Resources
- PySimpleGUI - Discussion
PySimpleGUI - Text Element
The Text element is one of the basic and most commonly used elements. An object of the Text class displays a non-editable, single line of text containing Unicode characters. Although most of the times, it is not used to respond to events, it can emit the event having its key as the name.
The Text element has the following properties in addition to those derived from the Element class −
Sr.No. | Property & Description |
---|---|
1 | text The text to display. Can include /n to achieve multiple lines. |
2 | justification How string should be aligned within space provided by size. Valid choices = "left", "right", "center" |
3 | pad Amount of padding to put around element in pixels |
4 | expand_x If True the element will automatically expand in the "X" direction to fill available space |
5 | expand_y If True the element will automatically expand in the "Y" direction to fill available space |
6 | tooltip Text that will appear when mouse hovers over the element |
The most important method defined in the Text class is the get() method that retrieves the current value of the displayed text, to be used programmatically elsewhere. You can also change the displayed text programmatically by capturing the click event, which should be enabled in the constructor.
The following example initially displays "Hello World" on the Text element, which changes to "Hello Python", when clicked.
import PySimpleGUI as psg layout = [[psg.Text('Hello World', enable_events=True, key='-TEXT-', font=('Arial Bold', 20), expand_x=True, justification='center')], ] window = psg.Window('Hello', layout, size=(715, 100)) while True: event, values = window.read() print(event, values) if event == '-TEXT-': window['-TEXT-'].update("Hello Python") if event == psg.WIN_CLOSED or event == 'Exit': break window.close()
Run the above program. Click the label to change its text as shown below −