### Install PySimpleGUI Demos Package Source: https://freesimplegui.readthedocs.io/en/latest/index Installs the 'psgdemos' package, which includes over 300 demo programs and the Demo Browser for PySimpleGUI. This is the recommended way to quickly access and learn from examples. ```shell python -m pip install psgdemos ``` ```shell python3 -m pip install psgdemos ``` -------------------------------- ### Install FreeSimpleGUI Source: https://freesimplegui.readthedocs.io/en/latest/readme Installs the FreeSimpleGUI library using pip. This is the primary method to get the package onto your system for use in Python projects. ```bash pip install FreeSimpleGUI ``` -------------------------------- ### Install FreeSimpleGUI Demos with pip (Python) Source: https://freesimplegui.readthedocs.io/en/latest/readme Installs the `psgdemos` package, which includes demo programs and the demo browser for FreeSimpleGUI, using pip with the `python` command. This is the primary method for installing the demos. ```shell python -m pip install psgdemos ``` -------------------------------- ### Basic File Selection Example Source: https://freesimplegui.readthedocs.io/en/latest/index A simple example demonstrating how to use popup_get_file to prompt the user for a filename and then display the result. ```python import PySimpleGUI as sg text = sg.popup_get_file('Please enter a file name') sg.popup('Results', 'The value returned from popup_get_file', text) ``` -------------------------------- ### Basic FreeSimpleGUI Window Example Source: https://freesimplegui.readthedocs.io/en/latest/index A 'Jump-Start' example demonstrating the creation of a simple window with text, an input field, and buttons using FreeSimpleGUI. It includes the event loop to process user interactions and window closing. ```python import FreeSimpleGUI 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('Cancel')] ] # Create the Window window = sg.Window('Window Title', layout) # Event Loop to process "events" and get the "values" of the inputs while True: event, values = window.read() if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel break print('You entered ', values[0]) window.close() ``` -------------------------------- ### Example Usage of popup_get_folder Source: https://freesimplegui.readthedocs.io/en/latest/index Demonstrates a typical call to popup_get_folder to get a folder path from the user, followed by displaying the result. ```Python import PySimpleGUI as sg # Display the folder selection dialog folder_path = sg.popup_get_folder('Please enter a folder name') # Display the result in another popup sg.popup('Results', 'The value returned from popup_get_folder', folder_path) ``` -------------------------------- ### Run PySimpleGUI Demo Browser Source: https://freesimplegui.readthedocs.io/en/latest/cookbook Instructions on how to start the main demo browser script. ```python Browser_START_HERE_Demo_Programs_Browser.py ``` -------------------------------- ### Install PySimpleGUI Demo Programs Source: https://freesimplegui.readthedocs.io/en/latest/cookbook Installs the PySimpleGUI Demo Programs and Demo Browser using pip. This package provides over 300 demo programs to learn PySimpleGUI patterns. ```shell python -m pip install psgdemos ``` ```shell python3 -m pip install psgdemos ``` -------------------------------- ### Manual Installation of PySimpleGUI Source: https://freesimplegui.readthedocs.io/en/latest/index For offline installations or when using the latest code from GitHub, you can manually place the `PySimpleGUI.py` file into your application's folder. Ensure you remove this local copy if you later install via pip to avoid conflicts. ```python # Place PySimpleGUI.py in the same folder as your application script. # Your application will automatically find and use it. ``` -------------------------------- ### Install Required Packages Source: https://freesimplegui.readthedocs.io/en/latest/cookbook Installs the FreeSimpleGUI and PyInstaller packages required for creating executables. These are necessary prerequisites before using PyInstaller. ```shell pip install FreeSimpleGUI pip install PyInstaller ``` -------------------------------- ### Install FreeSimpleGUI Demos with pip (Python 3) Source: https://freesimplegui.readthedocs.io/en/latest/readme Installs the `psgdemos` package for users on systems where `python3` is the command to launch Python. This ensures compatibility across different operating system configurations. ```shell python3 -m pip install psgdemos ``` -------------------------------- ### Launch PySimpleGUI Demo Browser Source: https://freesimplegui.readthedocs.io/en/latest/cookbook Launches the PySimpleGUI Demo Programs Browser from the command line after installation. ```shell psgdemos ``` -------------------------------- ### Set User Setting Entry (Error Example) Source: https://freesimplegui.readthedocs.io/en/latest/index Provides an example of setting a user setting entry that can lead to an error, typically due to an invalid file path or lack of permissions. ```python import PySimpleGUI as sg def main(): sg.user_settings_filename(path='...') sg.user_settings_set_entry('-test-',123) main() ``` -------------------------------- ### Install FreeSimpleGUI Source: https://freesimplegui.readthedocs.io/en/latest/index Instructions for installing the FreeSimpleGUI library using pip. It covers both pip and pip3 for Python 2 and Python 3 environments respectively. ```bash pip install FreeSimpleGUI or pip3 install FreeSimpleGUI ``` -------------------------------- ### Launch FreeSimpleGUI Demo Browser Source: https://freesimplegui.readthedocs.io/en/latest/readme Launches the FreeSimpleGUI demo browser application after the `psgdemos` package has been successfully installed. This tool allows users to search, edit, and run demo programs. ```shell psgdemos ``` -------------------------------- ### Basic Usage Example Source: https://freesimplegui.readthedocs.io/en/latest/index Demonstrates how to use the popup_get_text function to prompt the user for input and then display the result. ```python import FreeSimpleGUI as sg text = sg.popup_get_text('Title', 'Please input something') sg.popup('Results', 'The value returned from PopupGetText', text) ``` -------------------------------- ### Import FreeSimpleGUI and Create a Popup Source: https://freesimplegui.readthedocs.io/en/latest/index Demonstrates the basic steps to import the FreeSimpleGUI library and display a simple popup window. This is the starting point for creating GUI applications with the library. ```python import FreeSimpleGUI as sg sg.popup('This is my first popup') ``` -------------------------------- ### Launch PySimpleGUI Demo Browser Source: https://freesimplegui.readthedocs.io/en/latest/index Launches the PySimpleGUI Demo Browser from the command line after the 'psgdemos' package has been installed. This tool allows searching, editing, and running demo programs. ```shell psgdemos ``` -------------------------------- ### PySimpleGUI Spin Element Example Source: https://freesimplegui.readthedocs.io/en/latest/index Provides an example of a PySimpleGUI Spin element, an up/down spinner control. It takes a list of valid values and an initial value, allowing users to cycle through options. ```python import PySimpleGUI as sg layout = [[sg.Spin([i for i in range(1,11)], initial_value=1), sg.Text('Volume level')]] window = sg.Window('Spin Element Example', layout) while True: event, values = window.read() if event == sg.WINDOW_CLOSED: break print(f'Spin value: {values[0]}') # Assuming default key '0' window.close() ``` -------------------------------- ### Install PySimpleGUI and PyInstaller Source: https://freesimplegui.readthedocs.io/en/latest/index Installs the necessary Python packages, FreeSimpleGUI and PyInstaller, using pip. These packages are required for creating executable files. ```shell pip install FreeSimpleGUI pip install PyInstaller ``` -------------------------------- ### Get Two Files for Comparison with FreeSimpleGUI Source: https://freesimplegui.readthedocs.io/en/latest/cookbook This example shows how to prompt the user for two filenames using FileBrowse buttons and Input elements. Users can either browse for files or paste paths directly into the input fields. ```python import FreeSimpleGUI as sg sg.theme('Light Blue 2') layout = [[sg.Text('Enter 2 files to comare')], [sg.Text('File 1', size=(8, 1)), sg.Input(), sg.FileBrowse()], [sg.Text('File 2', size=(8, 1)), sg.Input(), sg.FileBrowse()], [sg.Submit(), sg.Cancel()]] window = sg.Window('File Compare', layout) event, values = window.read() window.close() print(f'You clicked {event}') print(f'You chose filenames {values[0]} and {values[1]}') ``` -------------------------------- ### Testing PySimpleGUI Installation via REPL Source: https://freesimplegui.readthedocs.io/en/latest/index You can test your PySimpleGUI installation by importing the library in the Python REPL and calling the `main()` function. This will launch the test harness, allowing you to verify functionality and check version details. ```python # For Python 2.7 import FreeSimpleGUI27 PySimpleGUI27.main() ``` ```python # For Python 3 import FreeSimpleGUI PySimpleGUI.main() ``` -------------------------------- ### Complete FreeSimpleGUI Window Example Source: https://freesimplegui.readthedocs.io/en/latest/index A full Python script demonstrating how to create, display, and read events from a basic FreeSimpleGUI window. It includes window creation, event loop, and closing the window. ```python import FreeSimpleGUI as sg layout = [[sg.Text('Enter a Number')], [sg.Input()], [sg.OK()]] window = sg.Window('Enter a number example', layout) event, values = window.read() window.close() sg.Popup(event, values[0]) ``` -------------------------------- ### Install PySimpleGUI using pip Source: https://freesimplegui.readthedocs.io/en/latest/index Standard commands to install or upgrade the PySimpleGUI package using pip for Python 3. Includes variations for different systems and common upgrade flags. ```shell pip install --upgrade PySimpleGUI ``` ```shell pip3 install --upgrade PySimpleGUI ``` ```shell sudo pip3 install --upgrade pysimplegui ``` ```shell pip install --upgrade --no-cache-dir PySimpleGUI ``` -------------------------------- ### Install PySimpleGUI27 and future for Python 2.7 Source: https://freesimplegui.readthedocs.io/en/latest/index Commands to install the specific PySimpleGUI27 package and the 'future' library for Python 2.7 environments. ```shell pip install --upgrade PySimpleGUI27 ``` ```shell pip2 install --upgrade PySimpleGUI27 ``` ```shell pip install future ``` ```shell pip2 install future ``` -------------------------------- ### One-shot Window - SHA Hash Example Source: https://freesimplegui.readthedocs.io/en/latest/index Illustrates the 'One-shot Window' pattern for a single interaction. This example creates a window to input file paths for SHA-1 and SHA-256 hashing, using FileBrowse for input. The window is read once and then closed. ```python import FreeSimpleGUI as sg sg.theme('Dark Blue 3') # please make your windows colorful layout = [[sg.Text('SHA-1 and SHA-256 Hashes for the file')], [sg.InputText(), sg.FileBrowse()], [sg.Submit(), sg.Cancel()]] window = sg.Window('SHA-1 & 256 Hash', layout) event, values = window.read() window.close() source_filename = values[0] # the first input element is values[0] ``` -------------------------------- ### Full To-Do List Example Program Source: https://freesimplegui.readthedocs.io/en/latest/index Presents the complete Python code for a to-do list application using FreeSimpleGUI. It includes the import statement, the definition of the `ToDoItem` element, layout creation, window initialization, and the main event loop. ```python import FreeSimpleGUI as sg def ToDoItem(num): return [sg.Text(f'{num}. '), sg.CBox(''), sg.In()] layout = [ToDoItem(x) for x in range(1,6)] + [[sg.Button('Save'), sg.Button('Exit')]] window = sg.Window('To Do List Example', layout) event, values = window.read() ``` -------------------------------- ### Button Image Example (Media Player) Source: https://freesimplegui.readthedocs.io/en/latest/index Provides a specific Python code snippet for creating a button with an image, as used in a media player demo. It highlights the use of image_filename, image_size, and image_subsample. ```python sg.Button('Pause', button_color=(sg.theme_background_color(), sg.theme_background_color()), image_filename=image_pause, image_size=(50, 50), image_subsample=2, border_width=0) ``` -------------------------------- ### Anatomy of a Simple FreeSimpleGUI Program Source: https://freesimplegui.readthedocs.io/en/latest/readme Illustrates the fundamental structure of a basic FreeSimpleGUI application, broken down into five essential sections: import, layout definition, window creation, event loop, and processing results. This example shows a 'one-shot' window pattern. ```python import FreeSimpleGUI as sg # Part 1 - The import # Define the window's contents layout = [ [sg.Text("What's your name?")], # Part 2 - The Layout [sg.Input()], [sg.Button('Ok')] ] # Create the window window = sg.Window('Window Title', layout) # Part 3 - Window Defintion # Display and interact with the Window event, values = window.read() # Part 4 - Event loop or Window.read call # Do something with the information gathered print('Hello', values[0], "! Thanks for trying FreeSimpleGUI") ``` -------------------------------- ### Complete Window Closed Event Loop Example Source: https://freesimplegui.readthedocs.io/en/latest/index A full example demonstrating how to integrate the window closure check into a FreeSimpleGUI event loop. It includes window creation, event reading, the closure check, and explicit window closing. ```Python import FreeSimpleGUI as sg layout = [[sg.Text('Very basic Window')], [sg.Text('Click X in titlebar or the Exit button')], [sg.Button('Go'), sg.Button('Exit')]] window = sg.Window('Window Title', layout) while True: event, values = window.read() print(event, values) if event == sg.WIN_CLOSED or event == 'Exit': break window.close() ``` -------------------------------- ### Basic Persistent Window Example Source: https://freesimplegui.readthedocs.io/en/latest/index Demonstrates the fundamental structure for creating a persistent window in FreeSimpleGUI that remains open until explicitly closed by the user. ```python import FreeSimpleGUI as sg layout = [[sg.Text('Persistent window')], [sg.Input()], [sg.Button('Read'), sg.Exit()]] window = sg.Window('Window that stays open', layout) while True: event, values = window.read() print(event, values) if event in (sg.WIN_CLOSED, 'Exit'): break window.close() ``` -------------------------------- ### PySimpleGUI FolderBrowse Example with Key Target Source: https://freesimplegui.readthedocs.io/en/latest/index Demonstrates using the FolderBrowse button to select a folder and populate an InputText element using its key as the target. This is the preferred method for targeting elements. ```python import PySimpleGUI as sg layout = [[sg.T('Source Folder')], [sg.In(key='input')], [sg.FolderBrowse(target='input'), sg.OK()]] window = sg.Window('Folder Browse Example', layout) while True: event, values = window.read() if event == sg.WIN_CLOSED or event == 'OK': break window.close() ``` -------------------------------- ### Import FreeSimpleGUIWeb Source: https://freesimplegui.readthedocs.io/en/latest/index Demonstrates the simple import statement required to use FreeSimpleGUIWeb in a Python environment, which automatically installs the latest version from PyPI. ```Python import FreeSimpleGUIWeb ``` -------------------------------- ### FreeSimpleGUI Table Simulation Layout Source: https://freesimplegui.readthedocs.io/en/latest/index Combines header and input rows to create a complete table layout for a FreeSimpleGUI window. The example sets a theme and displays the window, reading events and values. ```Python import FreeSimpleGUI as sg sg.theme('Dark Brown 1') headings = ['HEADER 1', 'HEADER 2', 'HEADER 3','HEADER 4'] header = [[sg.Text(' ')] + [sg.Text(h, size=(14,1)) for h in headings]] input_rows = [[sg.Input(size=(15,1), pad=(0,0)) for col in range(4)] for row in range(10)] layout = header + input_rows window = sg.Window('Table Simulation', layout, font='Courier 12') event, values = window.read() ``` -------------------------------- ### PySimpleGUI Listbox Element Example Source: https://freesimplegui.readthedocs.io/en/latest/index Demonstrates the creation and basic usage of a PySimpleGUI Listbox element. Listboxes allow users to select items, and can return multiple selections. Enabling events allows immediate return on user interaction. ```python import PySimpleGUI as sg layout = [[sg.Listbox(values=['Listbox 1', 'Listbox 2', 'Listbox 3'], size=(30, 6))]] window = sg.Window('Listbox Example', layout) while True: event, values = window.read() if event == sg.WINDOW_CLOSED: break print(f'Selected items: {values["Listbox 0"]}') # Assuming default key '0' window.close() ``` -------------------------------- ### Get Tkinter Widget Type via PySimpleGUI Element Source: https://freesimplegui.readthedocs.io/en/latest/index This example shows how to determine the specific Tkinter widget class associated with a PySimpleGUI element. Printing the type of `Element.Widget` allows you to consult the official Tkinter documentation for available methods and properties. ```python print(type(window[your_element_key].Widget)) ``` -------------------------------- ### PySimpleGUI PEP8 Method Renaming and Signature Source: https://freesimplegui.readthedocs.io/en/latest/index Explains the conversion of PySimpleGUI's CamelCase methods to PEP8 snake_case, detailing the renaming rule and providing an example of a method signature with parameters. This change aims to align PySimpleGUI with Python's PEP8 style guide. ```APIDOC PySimpleGUI PEP8 Bindings: Purpose: To provide PEP8 compliant snake_case equivalents for PySimpleGUI's existing CamelCase methods and functions, aligning with Python's standard naming conventions. Renaming Convention: To convert a CamelCase method/function name to snake_case, insert an underscore `_` before each uppercase letter. If there are no uppercase letters, only the first letter is changed to lowercase. Example Conversion: `Window.FindElement` becomes `Window.find_element` Method Signature Example: `Read(self, timeout=None, timeout_key='__TIMEOUT__', close=False)` - Parameters: - `self`: The instance of the Window object. - `timeout`: Optional. The number of seconds to wait for an event. Defaults to None (wait indefinitely). - `timeout_key`: Optional. The key to return when a timeout occurs. Defaults to '__TIMEOUT__'. - `close`: Optional. If True, the window is closed after reading. Defaults to False. - Returns: A tuple containing the event and the values dictionary. Note: CamelCase names are currently supported but will be removed in future releases. A utility is planned to assist developers with the conversion. ``` -------------------------------- ### Install typing module for Python 3.4 Source: https://freesimplegui.readthedocs.io/en/latest/index Command to install the 'typing' module, which is used by PySimpleGUI docstrings and is a separate installation for Python versions prior to 3.5. ```shell pip install typing ``` -------------------------------- ### Install pip and tkinter for Python 2.7 Source: https://freesimplegui.readthedocs.io/en/latest/index Commands for installing pip and tkinter specifically for Python 2.7 on Debian/Ubuntu systems, often required before installing Python packages. ```shell sudo apt install python-pip ``` ```shell sudo apt install python-tkinter ``` -------------------------------- ### Install System Dependencies for tkinter Source: https://freesimplegui.readthedocs.io/en/latest/index Commands to install the tkinter library, which is a requirement for PySimpleGUI, on Debian/Ubuntu-based Linux systems for both Python 2.7 and Python 3. ```shell sudo apt-get install python-tk ``` ```shell sudo apt-get install python3-tk ``` -------------------------------- ### PySimpleGUI Elements Showcase Source: https://freesimplegui.readthedocs.io/en/latest/index This Python script demonstrates all available PySimpleGUI elements in a single, organized window. It serves as a visual reference for each component's appearance and basic usage. The code includes setup for themes, menus, and interactive elements like progress bars and graphs. ```python import FreeSimpleGUI as sg """ Demo - Element List All 34 elements shown in 1 window as simply as possible. Copyright 2022 PySimpleGUI """ use_custom_titlebar = False def make_window(theme=None): NAME_SIZE = 23 def name(name): dots = NAME_SIZE-len(name)-2 return sg.Text(name + ' ' + '•'*dots, size=(NAME_SIZE,1), justification='r',pad=(0,0), font='Courier 10') sg.theme(theme) treedata = sg.TreeData() treedata.Insert("", '_A_', 'Tree Item 1', [1234], ) treedata.Insert("", '_B_', 'B', []) treedata.Insert("_A_", '_A1_', 'Sub Item 1', ['can', 'be', 'anything'], ) layout_l = [[name('Text'), sg.Text('Text')], [name('Input'), sg.Input(s=15)], [name('Multiline'), sg.Multiline(s=(15,2))], [name('Output'), sg.Output(s=(15,2))], [name('Combo'), sg.Combo(sg.theme_list(), default_value=sg.theme(), s=(15,22), enable_events=True, readonly=True, k='-COMBO-')], [name('OptionMenu'), sg.OptionMenu(['OptionMenu',],s=(15,2))], [name('Checkbox'), sg.Checkbox('Checkbox')], [name('Radio'), sg.Radio('Radio', 1)], [name('Spin'), sg.Spin(['Spin',], s=(15,2))], [name('Button'), sg.Button('Button')], [name('ButtonMenu'), sg.ButtonMenu('ButtonMenu', sg.MENU_RIGHT_CLICK_EDITME_EXIT)], [name('Slider'), sg.Slider((0,10), orientation='h', s=(10,15))], [name('Listbox'), sg.Listbox(['Listbox', 'Listbox 2'], no_scrollbar=True, s=(15,2))], [name('Image'), sg.Image(sg.EMOJI_BASE64_HAPPY_THUMBS_UP)], [name('Graph'), sg.Graph((125, 50), (0,0), (125,50), k='-GRAPH-')] ] layout_r = [[name('Canvas'), sg.Canvas(background_color=sg.theme_button_color()[1], size=(125,50))], [name('ProgressBar'), sg.ProgressBar(100, orientation='h', s=(10,20), k='-PBAR-')], [name('Table'), sg.Table([[1,2,3], [4,5,6]], ['Col 1','Col 2','Col 3'], num_rows=2)], [name('Tree'), sg.Tree(treedata, ['Heading',], num_rows=3)], [name('Horizontal Separator'), sg.HSep()], [name('Vertical Separator'), sg.VSep()], [name('Frame'), sg.Frame('Frame', [[sg.T(s=15)]])], [name('Column'), sg.Column([[sg.T(s=15)]])], [name('Tab, TabGroup'), sg.TabGroup([[sg.Tab('Tab1',[[sg.T(s=(15,2))]]), sg.Tab('Tab2', [[]])]])], [name('Pane'), sg.Pane([sg.Col([[sg.T('Pane 1')]]), sg.Col([[sg.T('Pane 2')]])])], [name('Push'), sg.Push(), sg.T('Pushed over')], [name('VPush'), sg.VPush()], [name('Sizer'), sg.Sizer(1,1)], [name('StatusBar'), sg.StatusBar('StatusBar')], [name('Sizegrip'), sg.Sizegrip()] ] layout = [[sg.MenubarCustom([['File', ['Exit']], ['Edit', ['Edit Me', ]]], k='-CUST MENUBAR-',p=0)] if use_custom_titlebar else [sg.Menu([['File', ['Exit']], ['Edit', ['Edit Me', ]]], k='-CUST MENUBAR-',p=0)], [sg.Checkbox('Use Custom Titlebar & Menubar', use_custom_titlebar, enable_events=True, k='-USE CUSTOM TITLEBAR-')], [sg.T('PySimpleGUI Elements - Use Combo to Change Themes', font='_ 18', justification='c', expand_x=True)], [sg.Col(layout_l), sg.Col(layout_r)]] window = sg.Window('The PySimpleGUI Element List', layout, finalize=True, right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_VER_EXIT, keep_on_top=True, use_custom_titlebar=use_custom_titlebar) window['-PBAR-'].update(30) # Show 30% complete on ProgressBar window['-GRAPH-'].draw_image(data=sg.EMOJI_BASE64_HAPPY_JOY, location=(0,50)) # Draw something in the Graph Element return window # Example of how to run the window: # if __name__ == '__main__': # window = make_window() # while True: # event, values = window.read() # if event == sg.WINDOW_CLOSED or event == 'Exit': # break # if event == '-USE CUSTOM TITLEBAR-': # use_custom_titlebar = values['-USE CUSTOM TITLEBAR-'] # window.close() # window = make_window(use_custom_titlebar=use_custom_titlebar) # if event == '-COMBO-': # window.close() # window = make_window(theme=values['-COMBO-']) # window.close() ``` -------------------------------- ### Get Setting with Default Value Source: https://freesimplegui.readthedocs.io/en/latest/index Shows how to retrieve a setting using the `get` method, providing a default value that is returned if the specified key is not found. ```python value = settings.get('-item-', '') ``` -------------------------------- ### PySimpleGUI All Elements Showcase Source: https://freesimplegui.readthedocs.io/en/latest/cookbook This Python script displays all 33 PySimpleGUI elements in a single window. It uses the FreeSimpleGUI library and demonstrates basic element usage, layout construction, and theme switching. The example is for demonstration purposes and not intended for practical application. ```python import FreeSimpleGUI as sg """ Demo - Element List All elements shown in 1 window as simply as possible. Copyright 2022 PySimpleGUI """ use_custom_titlebar = True if sg.running_trinket() else False def make_window(theme=None): NAME_SIZE = 23 def name(name): dots = NAME_SIZE-len(name)-2 return sg.Text(name + ' ' + '•'*dots, size=(NAME_SIZE,1), justification='r',pad=(0,0), font='Courier 10') sg.theme(theme) # NOTE that we're using our own LOCAL Menu element if use_custom_titlebar: Menu = sg.MenubarCustom else: Menu = sg.Menu treedata = sg.TreeData() treedata.Insert("", '_A_', 'Tree Item 1', [1234] ) treedata.Insert("", '_B_', 'B', []) treedata.Insert("_A_", '_A1_', 'Sub Item 1', ['can', 'be', 'anything'] ) layout_l = [ [name('Text'), sg.Text('Text')], [name('Input'), sg.Input(s=15)], [name('Multiline'), sg.Multiline(s=(15,2))], [name('Output'), sg.Output(s=(15,2))], [name('Combo'), sg.Combo(sg.theme_list(), default_value=sg.theme(), s=(15,22), enable_events=True, readonly=True, k='-COMBO-')], [name('OptionMenu'), sg.OptionMenu(['OptionMenu',],s=(15,2))], [name('Checkbox'), sg.Checkbox('Checkbox')], [name('Radio'), sg.Radio('Radio', 1)], [name('Spin'), sg.Spin(['Spin',], s=(15,2))], [name('Button'), sg.Button('Button')], [name('ButtonMenu'), sg.ButtonMenu('ButtonMenu', sg.MENU_RIGHT_CLICK_EDITME_EXIT)], [name('Slider'), sg.Slider((0,10), orientation='h', s=(10,15))], [name('Listbox'), sg.Listbox(['Listbox', 'Listbox 2'], no_scrollbar=True, s=(15,2))], [name('Image'), sg.Image(sg.EMOJI_BASE64_HAPPY_THUMBS_UP)], [name('Graph'), sg.Graph((125, 50), (0,0), (125,50), k='-GRAPH-')] ] layout_r = [[name('Canvas'), sg.Canvas(background_color=sg.theme_button_color()[1], size=(125,40))], [name('ProgressBar'), sg.ProgressBar(100, orientation='h', s=(10,20), k='-PBAR-')], [name('Table'), sg.Table([[1,2,3], [4,5,6]], ['Col 1','Col 2','Col 3'], num_rows=2)], [name('Tree'), sg.Tree(treedata, ['Heading',], num_rows=3)], [name('Horizontal Separator'), sg.HSep()], [name('Vertical Separator'), sg.VSep()], [name('Frame'), sg.Frame('Frame', [[sg.T(s=15)]])], [name('Column'), sg.Column([[sg.T(s=15)]])], [name('Tab, TabGroup'), sg.TabGroup([[sg.Tab('Tab1',[[sg.T(s=(15,2))]]), sg.Tab('Tab2', [[]])]])], [name('Pane'), sg.Pane([sg.Col([[sg.T('Pane 1')]]), sg.Col([[sg.T('Pane 2')]])])], [name('Push'), sg.Push(), sg.T('Pushed over')], [name('VPush'), sg.VPush()], [name('Sizer'), sg.Sizer(1,1)], [name('StatusBar'), sg.StatusBar('StatusBar')], [name('Sizegrip'), sg.Sizegrip()] ] # Note - LOCAL Menu element is used (see about for how that's defined) layout = [[Menu([['File', ['Exit']], ['Edit', ['Edit Me', ]]], k='-CUST MENUBAR-',p=0)], [sg.T('PySimpleGUI Elements - Use Combo to Change Themes', font='_ 14', justification='c', expand_x=True)], [sg.Checkbox('Use Custom Titlebar & Menubar', use_custom_titlebar, enable_events=True, k='-USE CUSTOM TITLEBAR-', p=0)], [sg.Col(layout_l, p=0), sg.Col(layout_r, p=0)]] window = sg.Window('The PySimpleGUI Element List', layout, finalize=True, right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_VER_EXIT, keep_on_top=True, use_custom_titlebar=use_custom_titlebar) window['-PBAR-'].update(30) # Show 30% complete on ProgressBar window['-GRAPH-'].draw_image(data=sg.EMOJI_BASE64_HAPPY_JOY, location=(0,50)) # Draw something in the Graph Element return window window = make_window() while True: event, values = window.read() # sg.Print(event, values) if event == sg.WIN_CLOSED or event == 'Exit': break if event == 'Edit Me': sg.execute_editor(__file__) if values['-COMBO-'] != sg.theme(): sg.theme(values['-COMBO-']) window.close() window = make_window() if event == '-USE CUSTOM TITLEBAR-': use_custom_titlebar = values['-USE CUSTOM TITLEBAR-'] ``` -------------------------------- ### Create and Update Window with Progress Meter Source: https://freesimplegui.readthedocs.io/en/latest/cookbook Demonstrates creating a basic window with a progress meter and updating it within a loop. It includes handling user interaction like cancellation and window closure. Assumes 'layout' and 'sg' are defined. ```python # create the Window window = sg.Window('Custom Progress Meter', layout) # loop that would normally do something useful for i in range(1000): # check to see if the cancel button was clicked and exit loop if clicked event, values = window.read(timeout=10) if event == 'Cancel' or event == sg.WIN_CLOSED: break # update bar with loop value +1 so that bar eventually reaches the maximum window['-PROG-'].update(i+1) # done with loop... need to destroy the window as it's still open window.close() ``` -------------------------------- ### PySimpleGUI Tooltip and Size Parameters Source: https://freesimplegui.readthedocs.io/en/latest/index Explains the 'tooltip' and 'size' parameters in detail, including advanced usage for tooltips and various sizing methods for elements. Covers pixel-based sizing, auto-sizing, and conversion logic for different GUI ports. ```APIDOC Tooltip and Size Parameters: Tooltip: - Purpose: Provides descriptive text when a user hovers over an element. - Customization: The background color of tooltips can be changed using the `TOOLTIP_BACKGROUND_COLOR` constant (default: "#ffffe0"). Size: - Character-based: For elements like Text and Button, `size=(width, height)` is measured in characters. - Auto-size: Text and Button elements have auto-size enabled by default, sizing to their content. This can be turned off to enforce uniform sizes. - Integer input: `size=N` is equivalent to `size=(N, 1)`. - Pixel-based (Non-Tkinter Ports): - `size_px=(width_px, height_px)`: Directly sets size in pixels. - Implicit Pixel Conversion: If `size[0]` exceeds `DEFAULT_PIXEL_TO_CHARS_CUTOFF` (default 20), it's treated as pixels. Some elements like Multline have a higher cutoff (100). - Pixel to Character Scaling: - `DEFAULT_PIXELS_TO_CHARS_SCALING = (10, 26)`: Used for converting pixel dimensions to character-based sizes (width * 10, height * 26). ``` -------------------------------- ### File/Folder Selection GUI in FreeSimpleGUI Source: https://freesimplegui.readthedocs.io/en/latest/index Illustrates creating a GUI window for selecting source folders and files using browse buttons. It demonstrates how to define a layout with input fields, browse buttons, and action buttons like Submit and Cancel. ```python import FreeSimpleGUI as sg sg.theme('Dark Blue 3') # please make your windows colorful layout = [[sg.Text('Rename files or folders')], [sg.Text('Source for Folders', size=(15, 1)), sg.InputText(), sg.FolderBrowse()], [sg.Text('Source for Files ', size=(15, 1)), sg.InputText(), sg.FileBrowse()], [sg.Submit(), sg.Cancel()]] window = sg.Window('Rename Files or Folders', layout) event, values = window.read() window.close() folder_path, file_path = values[0], values[1] # get the data from the values dictionary print(folder_path, file_path) ``` -------------------------------- ### Finding PySimpleGUI Installation Path Source: https://freesimplegui.readthedocs.io/en/latest/index To determine the exact location of your PySimpleGUI installation, you can import the library in the Python REPL or within your script and print the module object. This is crucial for debugging and ensuring you are using the intended version. ```python # In Python REPL >>> import FreeSimpleGUI as sg >>> sg ``` ```python # Within your Python script import FreeSimpleGUI as sg print(sg) print(sg.version) ``` -------------------------------- ### Get Filename with FileBrowse Source: https://freesimplegui.readthedocs.io/en/latest/index Demonstrates a basic FreeSimpleGUI window to get a filename from the user. It includes a text input field and a FileBrowse button, along with OK and Cancel buttons. The selected filename is then displayed in a popup. ```python import FreeSimpleGUI as sg sg.theme('Dark Blue 3') # please make your windows colorful layout = [[sg.Text('Filename')], [sg.Input(), sg.FileBrowse()], [sg.OK(), sg.Cancel()] ] window = sg.Window('Get filename example', layout) event, values = window.read() window.close() sg.Popup(event, values[0]) ``` -------------------------------- ### Popup Input Functions Source: https://freesimplegui.readthedocs.io/en/latest/index Provides functions for getting single-item input from the user via popups. These include getting text, filenames, and folder names. Use these for simple input tasks before resorting to custom window creation. ```APIDOC Popup Input Functions: popup_get_text: Get a single line of text from the user. popup_get_file: Get a filename from the user. popup_get_folder: Get a folder name from the user. These functions offer customizable settings for creating specific input dialogs. If the available parameters are insufficient, consider creating a custom window. ``` -------------------------------- ### Python FreeSimpleGUI Media Player with Button Graphics Source: https://freesimplegui.readthedocs.io/en/latest/cookbook Demonstrates creating a media player GUI using FreeSimpleGUI with custom button images. It sets up themed buttons, displays events, and includes an event loop for user interaction. Requires image files for buttons. ```python #!/usr/bin/env python import FreeSimpleGUI as sg # # An Async Demonstration of a media player # Uses button images for a super snazzy look # See how it looks here: # https://user-images.githubusercontent.com/13696193/43159403-45c9726e-8f50-11e8-9da0-0d272e20c579.jpg # def MediaPlayerGUI(): # Set the backgrounds the same as the background on the buttons # Images are located in a subfolder in the Demo Media Player.py folder image_pause = './ButtonGraphics/Pause.png' image_restart = './ButtonGraphics/Restart.png' image_next = './ButtonGraphics/Next.png' image_exit = './ButtonGraphics/Exit.png' # Use the theme APIs to set the buttons to blend with background sg.theme_button_color((sg.theme_background_color(), sg.theme_background_color())) sg.theme_border_width(0) # make all element flat # define layout of the rows layout= [[sg.Text('Media File Player',size=(17,1), font=("Helvetica", 25))], [sg.Text(size=(15, 2), font=("Helvetica", 14), key='-OUTPUT-')], [sg.Button(image_filename=image_restart, image_size=(50, 50), image_subsample=2, key='-RESTART SONG-'), sg.Text(' ' * 2), sg.Button(image_filename=image_pause, image_size=(50, 50), image_subsample=2, key='-PAUSE-'), sg.Text(' ' * 2), sg.Button(image_filename=image_next, image_size=(50, 50), image_subsample=2, key='-NEXT-'), sg.Text(' ' * 2), sg.Text(' ' * 2), sg.Button(image_filename=image_exit, image_size=(50, 50), image_subsample=2, key='Exit')], [sg.Text('_'*20)], [sg.Text(' '*30)], [ sg.Slider(range=(-10, 10), default_value=0, size=(10, 20), orientation='vertical', font=("Helvetica", 15)), sg.Text(' ' * 2), sg.Slider(range=(-10, 10), default_value=0, size=(10, 20), orientation='vertical', font=("Helvetica", 15)), sg.Text(' ' * 2), sg.Slider(range=(-10, 10), default_value=0, size=(10, 20), orientation='vertical', font=("Helvetica", 15))], [sg.Text(' Bass', font=("Helvetica", 15), size=(9, 1)), sg.Text('Treble', font=("Helvetica", 15), size=(7, 1)), sg.Text('Volume', font=("Helvetica", 15), size=(7, 1))] ] # Open a form, note that context manager can't be used generally speaking for async forms window = sg.Window('Media File Player', layout, default_element_size=(20, 1), font=("Helvetica", 25)) # Our event loop while True: event, values = window.read(timeout=100) # Poll every 100 ms if event == 'Exit' or event == sg.WIN_CLOSED: break # If a button was pressed, display it on the GUI by updating the text element if event != sg.TIMEOUT_KEY: window['-OUTPUT-'].update(event) MediaPlayerGUI() ``` -------------------------------- ### Chat Window Example with Output Element in FreeSimpleGUI Source: https://freesimplegui.readthedocs.io/en/latest/index A complete example of a chat window using the Output element to display received messages. Data is displayed by simply printing it, which is then routed to the Output element. This pattern is useful for real-time feedback within an application. ```python import FreeSimpleGUI as sg def ChatBot(): layout = [[(sg.Text('This is where standard out is being routed', size=[40, 1]))], [sg.Output(size=(80, 20))], [sg.Multiline(size=(70, 5), enter_submits=True), sg.Button('SEND', button_color=(sg.YELLOWS[0], sg.BLUES[0])), sg.Button('EXIT', button_color=(sg.YELLOWS[0], sg.GREENS[0]))]] window = sg.Window('Chat Window', layout, default_element_size=(30, 2)) # ---===--- Loop taking in user input and using it to query HowDoI web oracle --- # while True: event, value = window.read() if event == 'SEND': print(value) else: break window.close() ChatBot() ``` -------------------------------- ### Running PySimpleGUI Test Harness and Upgrade Utilities Source: https://freesimplegui.readthedocs.io/en/latest/index These commands are used to launch the PySimpleGUI test harness or initiate the GitHub upgrade process directly from your terminal. They are convenient alternatives to calling functions within your Python code. ```shell psgmain psgupgrade ``` ```shell python -m PySimpleGUI.PySimpleGUI upgrade ``` ```shell python -m PySimpleGUI.PySimpleGUI ``` ```shell # For Linux/Mac users, if python defaults to Python 2 python3 -m PySimpleGUI.PySimpleGUI ``` -------------------------------- ### FreeSimpleGUI Frame Element Layout Example Source: https://freesimplegui.readthedocs.io/en/latest/index Illustrates the usage of the `sg.Frame` element to create a labelled section within a FreeSimpleGUI window. This example shows a window containing a frame with text and checkboxes, alongside submit and cancel buttons, demonstrating how frames group related elements. ```python frame_layout = [ [sg.T('Text inside of a frame')], [sg.CB('Check 1'), sg.CB('Check 2')], ] layout = [ [sg.Frame('My Frame Title', frame_layout, font='Any 12', title_color='blue')], [sg.Submit(), sg.Cancel()] ] window = sg.Window('Frame with buttons', layout, font=("Helvetica", 12)) ```