PySimpleGUI – Window Class ”; Previous Next Popups have a predefined configuration of buttons, text labels and text input fields. The Window class allows you to design a GUI of more flexible design. In addition to these elements, other elements like listbox, checkbox, radio buttons, etc., are available. You can also provide a menu system to the GUI. Certain specialized widgets such as spinner, sliders, etc., can also be used to make the design more effective. A window can be a non-persistent window, similar to the popups. It blocks the program flow till the user closes it by clicking a button on the client area or the close (X) button in the title bar. A persistent window on the other hand continues to be visible till the event causing it to be closed occurs. The asynchronous window is the one whose contents are periodically updated. Layout Structure The placement of elements or widgets in the window’s client area is controlled by list of list objects. Each list element corresponds to one row on the window surface, and may contain one or more GUI elements available in PySimpleGUI library. The first step is to visualize the placement of elements by making a drawing as follows − The elements on the window are placed in four rows. First three rows have a Text element (displays a static text) and an InputText element (in which user can enter). Last row has two buttons, Ok and Cancel. This is represented in the list of lists as below − import PySimpleGUI as psg layout = [ [psg.Text(”Name ”),psg.Input()], [psg.Text(”Address ”), psg.Input()], [psg.Text(”Email ID ”), psg.Input()], [psg.OK(), psg.Cancel()] ] This list object is used as the value of layout parameter for the constructor of the Window class. window = psg.Window(”Form”, layout) This will display the desired window. The user inputs are stored in a dictionary named as values. The read() method of Window class is called as the user presses the Ok button, and the window closes immediately. The complete code to render the window is as follows − import PySimpleGUI as psg psg.set_options(font=(”Arial Bold”, 16)) layout = [ [psg.Text(”Name ”, size=(15,1)),psg.Input(expand_x=True)], [psg.Text(”Address ”, size=(15,1)), psg.Input(expand_x=True)], [psg.Text(”Email ID ”, size=(15,1)), psg.Input(expand_x=True)], [psg.OK(), psg.Cancel()] ] window = psg.Window(”Form”, layout, size=(715,207)) event, values = window.read() print (event, values) window.close() Here is the output as displayed − Enter the data as shown and press the “OK” button. The values will be printed as below − OK {0: ”Kiran Gupta”, 1: ”Mumbai”, 2: ”[email protected]”} If, after filling the data, you press the “Cancel” button, the result printed will be − Cancel {0: ”Kiran Gupta”, 1: ”Mumbai”, 2: ”[email protected]”} Persistent Window Note that this window gets closed as soon as any button (or the “X” button in the title bar) is clicked. To keep the window alive till a special type of button called Exit is pressed or if the window is closed by pressing “X”, the read() method is placed in an infinite loop with provision to break when WIN_CLOSED event occurs (when Exit button is pressed) or Exit event occurs (when “X” button is pressed). Let us change the Cancel button in the above code with Exit button. import PySimpleGUI as psg layout = [ [psg.Text(”Name ”), psg.Input()], [psg.Text(”Address ”), psg.Input()], [psg.Text(”Email ID ”), psg.Input()], [psg.OK(), psg.Exit()] ] window = psg.Window(”Form”, layout) while True: event, values = window.read() if event == psg.WIN_CLOSED or event == ”Exit”: break print (event, values) window.close() The appearance of the window will be similar as before, except that instead of Cancel, it has Exit button. The entered data will be printed in the form of a tuple. First element is the event, i.e., the caption of button, and second is a dictionary whose key is incrementing number and value is the text entered. OK {0: ”kiran”, 1: ”Mumbai”, 2: ”[email protected]”} OK {0: ”kirti”, 1: ”Hyderabad”, 2: ”[email protected]”} OK {0: ”karim”, 1: ”Chennai”, 2: ”[email protected]”} Window Methods The important method defined in the Window class is the read() method, to collect the values entered in all input elements. The Window class has other methods to customize the appearance and behaviour. They are listed below − Sr.No. Method & Description 1 AddRow Adds a single row of elements to a window”s “self.Rows” variable 2 AddRows Loops through a list of lists of elements and adds each row, list, to the layout. 3 close Closes the window so that resources are properly freed up. 4 disable Disables window from taking any input from the user 5 disappear Causes a window to “disappear” from the screen, but remain on the taskbar. 6 enable Re-enables window to take user input 7 fill Fill in elements that are input fields with data provided as dictionary. 8 find_element Find element object associated with the provided key. It is equivalent to “element = window[key]” 9 get_screen_dimensions Get the screen dimensions. 10 hide Hides the window from the screen and the task bar 11 load_from_disk Restore values from a Pickle file created by the “SaveToDisk” function 12 layout Populates the window with a list of widget lists. 13 read Get all of your data from your Window. Pass in a timeout (in milliseconds) to wait. 14 reappear Causes a disappeared window to display again. 15 save_to_disk Saves the values contained in each of the input elements to a pickle file. 16
Category: pysimplegui
Aug
09