PySimpleGUI – Menubar

PySimpleGUI – Menubar ”; Previous Next Most of the desktop applications have a menu system to trigger different operations based on user’s choice of options in the menu. In a typical application window, the menu bar is placed just below the title bar and above the client area of the window. A menubar is a horizontal bar consisting of clickable buttons. When any of these buttons is clicked it generates a pull down list of option buttons. Such an option button triggers a click event which can be processed inside an event loop. The menu system is designed just as the window layout is specified. It is also a list of lists. Each list has one or more strings. The starting string of the list at the first level is the caption for the button appearing in the horizontal menu bar. It is followed by a list of caption strings for the option buttons in the drop down menu. These option captions are in a list inside the first level list. You may have a sub-menu under an option button, in which case the captions are put in a third level list. Likewise, the captions can be nested up to any level. The general format of a menu definition is as follows: menu_def = [ [”Memu1”, [”btn1”, ”btn2”, ”btn3”, ”btn4”,]], [”menu2”, [”btn5”, ”btn6”,”btn7”, ”btn8”],], ] To attach the menu system to the main layout of PysimpleGUI window, place the Menu object in the first row of the layout. The Menu constructor is given the menu_def list as the argument. Other rows of the main layout may be given after the row having Menu object. layout= [[psg.Menu(menu_def),[..], [..]] In the code given below, we have a menu bar with File, Edit and Help menus, each having a few menu buttons in respective menu bar. import PySimpleGUI as psg menu_def = [[”File”, [”New”, ”Open”, ”Save”, ”Exit”, ]], [”Edit”, [”Cut”, ”Copy”, ”Paste”, ”Undo”], ], [”Help”, ”About…”], ] layout = [[psg.Menu(menu_def)], [psg.Multiline(“”, key=”-IN-”, expand_x=True, expand_y=True)], [psg.Multiline(“”, key=”-OUT-”, expand_x=True, expand_y=True)], [psg.Text(“”, key=”-TXT-”, expand_x=True, font=(“Arial Bold”, 14))] ] window = psg.Window(“Menu”, layout, size=(715, 300)) while True: event, values = window.read() print(event, values) if event != psg.WIN_CLOSED: window[”-TXT-”].update(values[0] + “Menu Button Clicked”) if event == ”Copy”: txt = window[”-IN-”].get() if event == ”Paste”: window[”-OUT-”].update(value=txt) if event == psg.WIN_CLOSED: break window.close() Below the Menubar, two Multiline elements are placed. The last row has a Text element. When any menu option button is clicked, the event so generated is the caption of the button. This caption is displayed on the Text label in the last row. Refer to the following figure − When the Copy event occurs, the text in the upper multiline box with -INkey is stored in a txt variable. Afterwards, when Paste button is pressed, the -OUT- box is updated with the value of txt. Menu button with Hot Key To map a menu button with a key on the keyboard, put an ampersand & character before the desired character. For example, put & before File so that the string is ”&File”. By doing so, the File menu can be accessed by pressing “Alf+F” key. Here “F” key is said to be a hot key. Add hot keys to the menu buttons in our menu definition. menu_def = [ [”&File”, [”&New”, ”&Open”, ”&Save”, ”E&xit”,]], [”&Edit”, [”C&ut”, ”&Copy”,”&Paste”, ”&Undo”],], [”&Help”, ”&About…”], ] When the code is run, the hot keys in the menu are shown as underlined. Right-click Menu This menu is detached from the menubar which is at the top of the application window. Whenever the user presses the right click button of the mouse, this menu pops up at the same position where the click takes place. In the menubar defined above, each list is a definition of a single menu. Such single menu definition can be attached to any element by the right_click_menu parameter in the constructor. This parameter can also be passed while constructing the main Window object. Let us use rightclick as a variable for the list corresponding to the Edit menu. rightclick=[”&Edit”, [”C&ut”, ”&Copy”,”&Paste”, ”&Undo”]] menu_def = [ [”&File”, [”&New”, ”&Open”, ”&Save”, ”E&xit”,]], rightclick, [”&Help”, ”&About…”], ] Use it as the value of right_click_menu parameter in the Window constructor. See the following snippet − window=psg.Window(“Menu”, layout, size=(715, 300), right_click_menu=rightclick) Make these changes and run the code. Click anywhere in the window. The menu pops up as shown − ButtonMenu This menu is similar to the right click menu, except that it is attached to a button and pops up when the button is clicked. In the last row of the main layout, we add a ButtonMenu element and use the rightclick list as its layout. layout= [ [psg.Menu(menu_def)], [psg.Multiline(“”, key=”-IN-”, expand_x=True, expand_y=True)], [psg.Multiline(“”, key=”-OUT-”, expand_x=True, expand_y=True)], [psg.Text(“”, key=”-TXT-”, expand_x=True, font=(“Arial Bold”, 14)), psg.ButtonMenu(”ButtonMenu”, rightclick, key=”-BMENU-”)] ] When the button at the lower right is clicked, the menu comes up as can be seen in the following figure − Print Page Previous Next Advertisements ”;

PySimpleGUI – Debugger

PySimpleGUI – Debugger ”; Previous Next In addition to the built-in debugger that most IDEs such as PyCharm or VS Code have, PySimpleGUI offers its own debugger. This debugger provides you the ability to “see” and interact with your code, while it is running. To use the debugger service effectively, the window should be red asynchronously, i.e., you should provide a timeout to the read() function. The debugger window is invoked by calling show_debugger_window() function anywhere inside the program as shown below − import PySimpleGUI as sg sg.show_debugger_window(location=(10,10)) window = sg.Window(”Debugger Demo”, [[sg.Text(”Debugger”), sg.Input(”Input here”), sg.Button(”Push Me”)]] ) while True: event, values = window.read(timeout=500) if event == sg.TIMEOUT_KEY: continue if event == sg.WIN_CLOSED: break print(event, values) window.close() The PySimpleGUI debugger window appears at the specified screen location. The window shows two tabs Variables and REPL. Click on the Variables tab. A list of variables to auto-watch is shown Check the ones that you want to watch during the execution of the program. The second tab about REPL gives a Python interactive console to be executed around your program’s environment so that you can inspect the values of desired variables in the code. Print Page Previous Next Advertisements ”;

PySimpleGUI – Home

PySimpleGUI Tutorial PDF Version Quick Guide Resources Job Search Discussion PySimpleGui is an open source, cross-platform GUI library for Python. It aims to provide a uniform API for creating desktop GUIs based on Python’s Tkinter, PySide and WxPython toolkits. PySimpleGUI also has a port for Remi which is useful for building GUIs for the web. PySimpleGui lets you build GUIs quicker than by directly using the libraries it uses. Audience This tutorial is designed for Python developers who want to learn how to build cross-platform desktop as well as web based GUI designs using PySimpleGui library. Prerequisites Before you proceed, make sure that you understand the basics of procedural and object-oriented programming in Python. For understanding the advanced topics such as integration of PySimpleGui with Matplotlib and OpenCV packages, their understanding is essential. Print Page Previous Next Advertisements ”;

PySimpleGUI – Element Class

PySimpleGUI – Element Class ”; Previous Next The PySimpleGUI library consists of a number of GUI widgets that can be placed on top of the Window object. For instance, the buttons or the textboxes that we have used in the above examples. All these widgets are in fact objects of classes defined in this library, in which Element class acts as the base for all other widget classes. An object of this Element class is never declared explicitly. It defines the common properties like size, color, etc. Here is the list of the available widgets (also called elements) Sr.No. Widget & Description 1 Text Element Display some text in the window. Usually this means a single line of text. 2 Input Element Display a single text input field. 3 Multiline Element Display and/or read multiple lines of text. This is both an input and output element. 4 Combo Element A combination of a single-line input and a drop-down menu. 5 OptionMenu Element Similar to Combo. Only on TKinter port 6 Checkbox Element Displays a checkbox and text next to it. 7 Radio Element Used in a group of other Radio Elements to provide user with ability to select only one choice in a list of choices. 8 Spin Element A spinner with up/down buttons and a single line of text. 9 Button Element Defines all possible buttons. The shortcuts such as Submit, FileBrowse, … each create a Button 10 ButtonMenu element Creates a button that when clicked will show a menu similar to right click menu. 11 Slider Element Horizontal or vertical slider to increment/decrement a value. 12 Listbox Element Provide a list of values for the user to choose one or more of. Returns a list of selected rows when a window.read() is executed. 13 Image Element Show an image in the window. Should be a GIF or a PNG only. 14 Graph Element Creates area to draw graph 15 Canvas Element An area to draw shapes 16 ProgressBar Element Displays a colored bar that is shaded as progress of some operation is made. 17 Table Element Display data in rows and columns 18 Tree Element Presents data in a tree-like manner, much like a file/folder browser. 19 Sizer Element This element is used to add more space. 20 StatusBar Element A StatusBar Element creates the sunken text-filled strip at the bottom. 21 Frame Element The Frame element is a container object that holds on or more elements of other types. 22 Column Element It is very useful if you want to design the GUI window elements represented in one or more vertical columns. 23 Tab Element The use of Tab elements makes the design very convenient, effective and easy for the user to navigate. Tab element is also a container element such as Frame or Column. Properties of Element Class Following are the properties of the Element Class − Sr.No. Property & Description 1 size (w=characters-wide, h=rows-high) 2 font specifies the font family, size 3 background_color color of background 4 text_color element”s text color 5 key Identifies an Element 6 visible set visibility state of the element (Default = True) Methods of Element Class Following are the methods of the Element Class − Sr.No. Method & Description 1 set_tooltip() Called by application to change the tooltip text for an Element 2 set_focus() Sets the current focus to be on this element 3 set_size() Changes the size of an element to a specific size 4 get_size() Return the size of an element in Pixels 5 expand() Causes the Element to expand to fill available space in the X and Y directions 6 set_cursor() Sets the cursor for the current Element 7 set_right_click_menu() Sets right click menu to be invoked when clicked Print Page Previous Next Advertisements ”;