From 9f4f7d125e0f7c5d5c8859f44107cb5850a3080f Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Sun, 9 Sep 2018 23:22:10 -0400 Subject: [PATCH 1/2] MENUS! Updated Desktop Timer Widget --- Demo_Desktop_Widget_Timer.py | 4 +-- PySimpleGUI.py | 63 +++++++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/Demo_Desktop_Widget_Timer.py b/Demo_Desktop_Widget_Timer.py index f9b77181..3fc230ab 100644 --- a/Demo_Desktop_Widget_Timer.py +++ b/Demo_Desktop_Widget_Timer.py @@ -15,13 +15,13 @@ import time def Timer(): # ---------------- Create Form ---------------- - sg.ChangeLookAndFeel('Dark') + sg.ChangeLookAndFeel('Black') sg.SetOptions(element_padding=(0,0)) # Make a form, but don't use context manager # Create the form layout form_rows = [[sg.Text('')], [sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center', key='text')], - [sg.ReadFormButton('Pause', key='button'), sg.ReadFormButton('Reset'), sg.Exit(button_color=('white','firebrick4'))]] + [sg.ReadFormButton('Pause', key='button', button_color=('white', '#001480')), sg.ReadFormButton('Reset', button_color=('white', '#007339')), sg.Exit(button_color=('white','firebrick4'))]] # Layout the rows of the form and perform a read. Indicate the form is non-blocking! form = sg.FlexForm('Running Timer', no_titlebar=True, auto_size_buttons=False, keep_on_top=True, grab_anywhere=True) form.Layout(form_rows) diff --git a/PySimpleGUI.py b/PySimpleGUI.py index c9a00233..d0548d63 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -922,8 +922,6 @@ class Button(Element): self.TKButton.config(foreground=button_color[0], background=button_color[1]) except: return - if new_text is not None: - self.ButtonText = new_text self.DefaultValue = value def __del__(self): @@ -1431,10 +1429,10 @@ class TKCalendar(ttk.Frame): # Canvas # # ---------------------------------------------------------------------- # class Menu(Element): - def __init__(self, text, command=None, background_color=None, scale=(None, None), size=(None, None), pad=None, key=None): + def __init__(self, menu_definition, command=None, background_color=None, scale=(None, None), size=(None, None), pad=None, key=None): self.BackgroundColor = background_color if background_color is not None else DEFAULT_BACKGROUND_COLOR self.Command = command - self.Text = text + self.MenuDefinition = menu_definition self.TKMenu = None super().__init__(ELEM_TYPE_MENUBAR, background_color=background_color, scale=scale, size=size, pad=pad, key=key) @@ -2117,6 +2115,41 @@ def _FindElementFromKeyInSubForm(form, key): return element +def AddMenuItem(top_menu, sub_menu_info, element, is_sub_menu=False, skip=False): + if type(sub_menu_info) is str: + if not is_sub_menu and not skip: + # print(f'Adding command {sub_menu_info}') + top_menu.add_command(label=sub_menu_info, command=lambda: Menu.MenuItemChosenCallback(element, sub_menu_info)) + else: + i = 0 + while i < (len(sub_menu_info)): + item = sub_menu_info[i] + if i != len(sub_menu_info) - 1: + if type(sub_menu_info[i+1]) == list: + new_menu = tk.Menu(top_menu) + top_menu.add_cascade(label=sub_menu_info[i], menu=new_menu) + AddMenuItem(new_menu, sub_menu_info[i+1], element, is_sub_menu=True) + i += 1 # skip the next one + else: + AddMenuItem(top_menu, item, element) + else: + AddMenuItem(top_menu, item, element) + i += 1 + + + + + # print(f'Looping through {sub_menu_info}') + # print(f'Type is {type(sub_menu_info)}') + # if type(sub_menu_info[1]) is list: + # new_menu = tk.Menu(top_menu) + # top_menu.add_cascade(label=sub_menu_info[0], menu=new_menu) + # AddMenuItem(new_menu, sub_menu_info[1], element, is_sub_menu=True) + # else: + # for item in sub_menu_info: + # AddMenuItem(top_menu, item, element) + + # ------------------------------------------------------------------------------------------------------------------ # # ===================================== TK CODE STARTS HERE ====================================================== # # ------------------------------------------------------------------------------------------------------------------ # @@ -2522,13 +2555,21 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form): element.TKCanvas.pack(side=tk.LEFT, padx=element.Pad[0], pady=element.Pad[1]) # ------------------------- MENUBAR element ------------------------- # elif element_type == ELEM_TYPE_MENUBAR: - element.TKMenu = tk.Menu(toplevel_form.TKroot) - filemenu = tk.Menu(element.TKMenu, tearoff=0) - filemenu.add_command(label="Open", command=lambda: Menu.MenuItemChosenCallback(element,'Open')) - element.TKMenu.add_cascade(label="File", menu=filemenu) - helpmenu = tk.Menu(element.TKMenu) - element.TKMenu.add_cascade(label='Help', menu=helpmenu) - helpmenu.add('command', label='About...', command=lambda: Menu.MenuItemChosenCallback(element, 'About...')) + menu_def = (('File', ('Open', 'Save')), + ('Help', 'About...'),) + # ('Help',)) + + menu_def = element.MenuDefinition + + element.TKMenu = tk.Menu(toplevel_form.TKroot, tearoff=0) # create the menubar + menubar = element.TKMenu + for menu_entry in menu_def: + # print(f'Adding a Menubar ENTRY') + baritem = tk.Menu(menubar) + menubar.add_cascade(label=menu_entry[0], menu=baritem) + if len(menu_entry) > 1: + AddMenuItem(baritem, menu_entry[1], element) + toplevel_form.TKroot.configure(menu=element.TKMenu) # ------------------------- Frame element ------------------------- # elif element_type == ELEM_TYPE_FRAME: From ce4f456cb38d1259f31463357ac2d77c1ae41043 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Sun, 9 Sep 2018 23:31:13 -0400 Subject: [PATCH 2/2] Demo for MENUS! --- Demo_Menus.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Demo_Menus.py diff --git a/Demo_Menus.py b/Demo_Menus.py new file mode 100644 index 00000000..1d7625f9 --- /dev/null +++ b/Demo_Menus.py @@ -0,0 +1,42 @@ +import PySimpleGUI as sg + +""" + 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 +""" +def TestMenus(): + import PySimpleGUI as sg + + sg.ChangeLookAndFeel('LightGreen') + sg.SetOptions(element_padding=(0, 0)) + + menu_def = [['File', ['Open', 'Save',]], + ['Edit', ['Paste', ['Special', 'Normal',], 'Undo'],], + ['Help', 'About...'],] + + layout = [ + [sg.Menu(menu_def)], + [sg.Output(size=(60,20))] + ] + + form = sg.FlexForm("Windows-like program", default_element_size=(12, 1), auto_size_text=False, auto_size_buttons=False, + default_button_element_size=(12, 1)) + form.Layout(layout) + form.ReadNonBlocking() # so the print message will show up... yes, a kludge + + print('Give those menus a try!') + while True: + button, values = form.Read() + if button is None or button == 'Exit': + return + print('Button = ', button) + if button == 'About...': + sg.Popup('About this program','Version 1.0', 'PySimpleGUI rocks...') + elif button == 'Open': + filename = sg.PopupGetFile('file to open', no_window=True) + print(filename) + + +TestMenus() \ No newline at end of file