Python Menu, Button Menu and Option Menu with PySimpleGUI

Menus are an important part of an interactive GUI application. Python menu driven application can be created by using the Menu, ButtonMenu and OptionMenu elements provided in PySimpleGUI package.

Menu

Menu is a textual navigation element usually placed at the top of an application window. A menu provides all the actions, tasks, processes or activities available in the application containing the menu. The menu is available in all the windows of an application where different tasks are to be displayed for user interaction.

Menu in a Python application is created as follows-

Menu(menu_list_def,[attribute=value, attribute=value,…])

menu_list_def is the list of lists of String, this is the mandatory attribute/parameter that contains the definition of the menu as lists of lists with string value as the menu texts visible to the users.

Attributes:

background_color- String, color name to set  the background color of the menu.

text_color- String, color name to define the text color of the menu.

disabled_text_color – String, color name to define the text color of the disabled options in the menu.

Size – (integer, integer), size of the menu in width and height.

font – String, font to be applied to the text of the  menu.

key/K – String,integer, tuple or object, the identification of the menu  to be used by the window read method.

visible – Boolean, used to set whether the menu should be visible to the user or not.

Button Menu

Button Python Menu is also a menu that is series of buttons to display set dropdowns of options to a user. It can be placed anywhere in the application window. It will be displayed as a button which displays a list of options when clicked.

Button Menu in a Python application is created as follows-

ButtonMenu(button_text, menu_options_list,[attribute=value, attribute=value,…])
  • button_text  is mandatory and represents the text that appears on the button for a menu list.  If you don’t want text to appear on button set it to blank string (“”)
  • menu_options_list is the list of lists of Strings, a mandatory attribute/parameter that contains the list of lists of options to be given to the users to select from.

Attributes:

default_value – String, value that is stored by the Button Menu if the user does not select one of the options in the list.

disabled- Boolean, set True or False to disable or enable  to control use of button menu for interaction.

tooltip – String, a small help text that appears with mouse hover over the element.

default – Boolean, to enable or disable this pySimpleGUI element to allow or disallow the interaction of user with this element.

image_filename – String, name of the image file to display on the Button for this menu.

image_size (integer, integer), Size of the image in width and height.

border_width– Integer, to set width of the border of button.

background_color- String, color name to define the background color of the Python menu of buttons.

text_color- String, color name to define the text color of the Button Menu.

button_color – String, color name to define the color of the button of the menu

disabled_text_color – String, color name to define the text color of the disabled button in the button menu.

Size – (integer, integer), Size of the button menu in width and height.

font – String, font to be applied to the text of the  button menu.

key/K – String, integer, tuple or object, the identification of the button menu  by the window read method.

visible – Boolean, defines if the button menu is visible or not.

Option Menu

Option Menu is a just like a dropdown linked to a button to display a set of options to user. It can be placed anywhere in the application window. It will be displayed as a button with a small button when clicked drops a list of options.

Option Menu in a Python application is created as follows-

OptionMenu(values=Options_list,[attribute=value, attribute=value,…])

values is the list of Strings, a mandatory attribute/parameter that contains the list of options visible to the users.

Attributes:

default_value – String, value that is stored by the option menu if the user does not select one of the options in the list.

default – Boolean, to enable or disable the pySimpleGUI element to allow or disallow the interaction of user with this element.

background_color- String, color name to define the background color of the menu.

text_color- String, color name to define the text color of the option menu.

Size – (integer, integer), Size of the option menu in width and height.

font – String, font to be applied to the text of the  option menu.

key/K – String, integer, tuple or object, the identification of the option menu  by the window read method.

visible – Boolean, defines if the option menu is visible or not.

tooltip – String, a small help text that appears with mouse hover over the element.

Example of Python Menu, Button Menu and Option Menu

import PySimpleGUI as sg
#set the theme for the screen/window
sg.theme('SystemDefault')
menu_def=['&File', ['&New File', '&Open...','Open &Module','---', '!&Recent Files','C&lose']],['&Save',['&Save File', 'Save &As','Save &Copy'  ]],['&Edit', ['&Cut', '&Copy', '&Paste']]
#define layout
layout=[[sg.Menu(menu_def, background_color='lightsteelblue',text_color='navy', disabled_text_color='yellow', font='Verdana', pad=(10,10))],
        [sg.ButtonMenu('', [['nf', 'op','om','---','rf','cl'],['New File', 'Open','Open Module','---','Recent Files','Close']],image_filename ='icon_biggrin.gif',border_width=5),sg.ButtonMenu('', [['rm', 'rc','sm','ps'],['Run', 'Run Module','Shell','Python module']],image_filename ='icon_cry.gif',background_color='teal'),sg.ButtonMenu('Terminate', [['ex', 'cl','---','ab'],['Exit', 'Close','---','About us...']])],
        [sg.Multiline(size=(80,10),tooltip='Write your Text here')],
        [sg.Text('File Name'),sg.Input(),sg.OptionMenu(values=['.txt','.pdf','.gif', '.jpg','.mp4','.gif','.dat','.sql'],size=(4,8),default_value='.doc',key='ftype')],
        [sg.Button('SAVE', font=('Times New Roman',12)),sg.Button('CANCEL', font=('Times New Roman',12))]]
#Define Window
win =sg.Window('Your Custom Editor',layout)
#Read  values entered by user
events,values=win.read()
#close first window
win.close()
Python Menu Using PySimpleGUI

Font attribute set for menu with yellow color for disabled menu option

image 2
Image 3
image 4

Button Menu with images on button and border-width= 5 for first button.

image Button menu

background-color set to ‘teal’ of last Button Menu.

Button Menu BG color

Option Menu displaying the extension options for name of the file.

Option Menu

Be First to Comment

Leave a Reply

Your email address will not be published.