PySimpleGUI/DemoPrograms/Demo_Menus.py

75 lines
2.5 KiB
Python
Raw Permalink Normal View History

2018-09-27 20:24:09 +00:00
#!/usr/bin/env python
2019-10-07 20:37:01 +00:00
import PySimpleGUI as sg
2018-09-10 03:31:13 +00:00
"""
Demo of Menu element, ButtonMenu element and right-click menus
The same basic structure is used for all menus in PySimpleGUI.
Each entry is a list of items to display. If any of those items is a list, then a cancade menu is added.
Copyright 2018, 2019, 2020, 2021, 2022 PySimpleGUI
2018-09-10 03:31:13 +00:00
"""
2019-10-07 20:37:01 +00:00
def second_window():
2018-09-11 00:24:04 +00:00
layout = [[sg.Text('The second form is small \nHere to show that opening a window using a window works')],
[sg.OK()]]
2019-10-07 20:37:01 +00:00
window = sg.Window('Second Form', layout)
event, values = window.read()
window.close()
2018-09-11 00:24:04 +00:00
def test_menus():
2018-10-08 17:30:33 +00:00
2019-12-24 23:52:47 +00:00
sg.theme('LightGreen')
2019-10-07 20:37:01 +00:00
sg.set_options(element_padding=(0, 0))
2018-09-10 03:31:13 +00:00
2018-09-11 00:24:04 +00:00
# ------ Menu Definition ------ #
menu_def = [
['&File', ['&Open Ctrl-O', '&Save Ctrl-S', '&Properties', 'E&xit']],
['&Edit', ['&Paste', ['Special', 'Normal', ], 'Undo', 'Options::this_is_a_menu_key'], ],
['&Toolbar', ['---', 'Command &1', 'Command &2',
'---', 'Command &3', 'Command &4']],
['&Help', ['&About...']]
]
2018-09-10 03:31:13 +00:00
2019-12-24 23:52:47 +00:00
right_click_menu = ['Unused', ['Right', '!&Click', '&Menu', 'E&xit', 'Properties']]
2018-09-11 00:24:04 +00:00
# ------ GUI Defintion ------ #
2018-09-10 03:31:13 +00:00
layout = [
[sg.Menu(menu_def, tearoff=True, font='_ 12', key='-MENUBAR-')],
2019-12-24 23:52:47 +00:00
[sg.Text('Right click me for a right click menu example')],
[sg.Output(size=(60, 20))],
[sg.ButtonMenu('ButtonMenu', right_click_menu, key='-BMENU-', text_color='red', disabled_text_color='green'), sg.Button('Plain Button')],
]
2018-09-10 03:31:13 +00:00
2018-10-29 00:01:03 +00:00
window = sg.Window("Windows-like program",
2019-10-07 20:37:01 +00:00
layout,
2018-10-29 00:01:03 +00:00
default_element_size=(12, 1),
2019-12-24 23:52:47 +00:00
default_button_element_size=(12, 1),
right_click_menu=right_click_menu)
2018-09-10 03:31:13 +00:00
2018-09-11 00:24:04 +00:00
# ------ Loop & Process button menu choices ------ #
2018-09-10 03:31:13 +00:00
while True:
2019-10-07 20:37:01 +00:00
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
2019-12-24 23:52:47 +00:00
break
print(event, values)
2018-09-11 00:24:04 +00:00
# ------ Process menu choices ------ #
if event == 'About...':
2019-10-07 20:37:01 +00:00
window.disappear()
sg.popup('About this program', 'Version 1.0', 'PySimpleGUI Version', sg.get_versions())
2019-10-07 20:37:01 +00:00
window.reappear()
elif event == 'Open':
2019-10-07 20:37:01 +00:00
filename = sg.popup_get_file('file to open', no_window=True)
2018-09-10 03:31:13 +00:00
print(filename)
elif event == 'Properties':
2019-10-07 20:37:01 +00:00
second_window()
2018-09-11 00:24:04 +00:00
window.close()
test_menus()