From cb9770f57eb8794b7ddaa0d09163f106c988bf3b Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Sun, 20 Mar 2022 09:58:21 +0100 Subject: [PATCH 1/3] remove unused code --- gui_helper.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/gui_helper.py b/gui_helper.py index f9f2b73..c961fa8 100644 --- a/gui_helper.py +++ b/gui_helper.py @@ -1,4 +1,3 @@ -import tkinter as tk import util @@ -67,18 +66,3 @@ menu_structure = { #FIXME: come up with a way to uniquely declare separators so they don't become the same element of a dict -def build_menu(structure_dict, menu): - for entry in structure_dict: - if structure_dict[entry]==None: - if entry==None: - menu.add_separator() - else: - menu.add_command(label=entry) - menu.entryconfig(entry, state="disabled") - if isinstance(structure_dict[entry], dict): - submenu = tk.Menu(menu, tearoff=False) - build_menu(structure_dict[entry], submenu) - menu.add_cascade(label=entry, menu=submenu) - if callable(structure_dict[entry]): - menu.add_command(label=entry, command=structure_dict[entry]) - From 7d3f60630430e3d3320ee2309e0364b1cb7ebe2d Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Sun, 20 Mar 2022 10:01:00 +0100 Subject: [PATCH 2/3] update format of the menu structure dict to allow for multiple separators in one menu, also rewrite the update_menus function to better reflect the spec for the menu dict --- gui_handler.py | 48 +++++++++++++++++++++++------------------------- gui_helper.py | 15 ++++++--------- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/gui_handler.py b/gui_handler.py index 72b8a30..9ef2cb5 100644 --- a/gui_handler.py +++ b/gui_handler.py @@ -26,32 +26,30 @@ class Window(QtWidgets.QMainWindow): def set_size(self, size_x, size_y): self.resize(size_x, size_y) - def update_menus(self, menu_dict): - menu = self.menuBar() + def update_menus(self, menu_dict, menu=None): + # if not a sub menu + if menu == None: + menu = self.menuBar() + #Looping through entire menu_dict - for topmenu, submenu in menu_dict.items(): - #making top level menu - menu_item = menu.addMenu(topmenu) - - #adding menu items (populating menu) - for inner, data in submenu.items(): - #Adding submenu and populating it - if type(data) == dict: - sub = menu_item.addMenu(inner) - for label, func in data.items(): - sub.addAction(label) - - if func == None: - sub.triggered.connect(lambda x: None) - else: - sub.triggered.connect(func) - #Adding separators - elif inner == None: - menu_item.addSeparator() - else: - item = menu_item.addAction(inner) - item.triggered.connect(data) - + for entry in menu_dict: + # inactive or separator + if menu_dict[entry] == None: + # determine if entry is a separator or an inactive menu item + if type(entry) == int: + menu.addSeparator() + else: + menu_item = menu.addAction(entry) + menu_item.setEnabled(False) + # sub menus + if type(menu_dict[entry]) == dict: + submenu = menu.addMenu(entry) + # recurse because sub menus may have sub menus + self.update_menus(menu_dict[entry], menu=submenu) + # ordinary menu entries + if callable(menu_dict[entry]): + menu_item = menu.addAction(entry) + menu_item.triggered.connect(menu_dict[entry]) #TODO: This needs to run in a thread but Qt really doesn't want it to. There are two ways around this: # - create the QtWidgets.QApplication inside a thread and run all QT stuff inside that thread diff --git a/gui_helper.py b/gui_helper.py index c961fa8..43a5885 100644 --- a/gui_helper.py +++ b/gui_helper.py @@ -8,13 +8,13 @@ def not_implemented(): # "":{} -> menu or submenu # "":function -> menu entry # "":None -> disabled menu entry -# None:None -> separator +# int:None -> separator # # Entries with ... at the end are supposed to open dialogs whereas entries without dots are supposed to take effect immediately menu_structure = { "IDE": { "Preferences...": not_implemented, - None: None, + 0: None, "Quit": not_implemented }, "Project": { @@ -25,7 +25,7 @@ menu_structure = { "Close": { "No open projects": None }, - None: None, + 0: None, "Preferences...": not_implemented, "Search...": not_implemented, "Build": not_implemented @@ -35,7 +35,7 @@ menu_structure = { "Open...": not_implemented, "Save": not_implemented, "Close": not_implemented, - None: None, + 0: None, "Rename...": not_implemented, "Move...": not_implemented, "View in File Explorer...": not_implemented @@ -45,9 +45,9 @@ menu_structure = { "Copy": not_implemented, "Paste": not_implemented, "Move code...": not_implemented, - None: None, + 0: None, "Search and Replace...": not_implemented, - None: None, + 1: None, "Format": not_implemented, "Indent": not_implemented, "Unindent": not_implemented, @@ -63,6 +63,3 @@ menu_structure = { "About IDE...": not_implemented, } } - -#FIXME: come up with a way to uniquely declare separators so they don't become the same element of a dict - From bcd9d00a47c746f302c96ed9c6cdc75b84dbe5f7 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Sun, 20 Mar 2022 10:19:10 +0100 Subject: [PATCH 3/3] fix an issue where updating the menu bar multiple times would actually just add to it --- gui_handler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/gui_handler.py b/gui_handler.py index 9ef2cb5..b14616d 100644 --- a/gui_handler.py +++ b/gui_handler.py @@ -30,6 +30,7 @@ class Window(QtWidgets.QMainWindow): # if not a sub menu if menu == None: menu = self.menuBar() + menu.clear() #Looping through entire menu_dict for entry in menu_dict: