PySimpleGUI/Demo_Menus.py

61 lines
2.0 KiB
Python
Raw Normal View History

2018-09-27 20:24:09 +00:00
#!/usr/bin/env python
import sys
if sys.version_info[0] >= 3:
2018-10-08 17:30:33 +00:00
import PySimpleGUI as sg
else:
import PySimpleGUI27 as sg
2018-09-10 03:31:13 +00:00
"""
Demonstration of MENUS!
How do menus work? Like buttons is how.
Check out the variable menu_def for a hint on how to
define menus
"""
2018-09-11 00:24:04 +00:00
def SecondForm():
layout = [[sg.Text('The second form is small \nHere to show that opening a window using a window works')],
[sg.OK()]]
window = sg.Window('Second Form').Layout(layout)
b, v = window.Read()
2018-09-11 00:24:04 +00:00
2018-09-10 03:31:13 +00:00
def TestMenus():
2018-10-08 17:30:33 +00:00
2018-09-10 03:31:13 +00:00
sg.ChangeLookAndFeel('LightGreen')
sg.SetOptions(element_padding=(0, 0))
2018-09-11 00:24:04 +00:00
# ------ Menu Definition ------ #
2018-10-08 17:30:33 +00:00
menu_def = [['&File', ['&Open', '&Save', '&Properties', 'E&xit' ]],
['&Edit', ['&Paste', ['Special', 'Normal',], 'Undo'],],
['&Toolbar', ['---', 'Command &1', 'Command &2', '---', 'Command &3', 'Command &4']],
['&Help', '&About...'],]
2018-09-10 03:31:13 +00:00
2018-09-11 00:24:04 +00:00
# ------ GUI Defintion ------ #
2018-09-10 03:31:13 +00:00
layout = [
2018-10-08 17:30:33 +00:00
[sg.Menu(menu_def, tearoff=True)],
2018-09-18 16:05:44 +00:00
[sg.Output(size=(60,20))],
[sg.In('Test', key='input', do_not_clear=True)]
2018-09-10 03:31:13 +00:00
]
2018-10-08 17:30:33 +00:00
window = sg.Window("Windows-like program", default_element_size=(12, 1), auto_size_text=False,
auto_size_buttons=False, default_button_element_size=(12, 1)).Layout(layout)
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:
button, values = window.Read()
2018-09-10 03:31:13 +00:00
if button is None or button == 'Exit':
return
print('Button = ', button)
2018-09-11 00:24:04 +00:00
# ------ Process menu choices ------ #
2018-09-10 03:31:13 +00:00
if button == 'About...':
window.Hide()
sg.Popup('About this program','Version 1.0', 'PySimpleGUI rocks...', grab_anywhere=True)
window.UnHide()
2018-09-10 03:31:13 +00:00
elif button == 'Open':
filename = sg.PopupGetFile('file to open', no_window=True)
print(filename)
2018-09-11 00:24:04 +00:00
elif button == 'Properties':
SecondForm()
2018-09-10 03:31:13 +00:00
TestMenus()