0

Code:

Mods = {"links_too":[module_1,module_2,module_3,module_4,module_5,module_6],"Buttons":[],"previous":Mods}
module_1 = {"links_too":[],"Buttons":[],"previous":Mods}

def create_menu_buttons():
    global Mods
    for x in Mods["links_too"]:
        x = ttk.Button(window,text=x,command=nav_load_menu(x))

the goal of the code is to create a button for each dictionary in the "links_too" section. to do this all the buttons need names so i am naming then by the name of the dictionary. i know this will cause issues as it will have the same name as the dictionary itself but i do not know of another way to do this. Is there a way to do this in the for loop where the buttons will have different names.

1 Answers1

0

There are actually two ways that i know that make this possible.

The first one is pretty simple but not recommended. Its make use of globals() where you can create variables out of a dictionary.

Here is an example:

# I've edited the dict a bit to make it runable
Mods = {"links_too":["module_1"],"Buttons":[],"previous":"Mods"}

window = ttk.Tk()

for x in Mods["links_too"]:
    globals()[x + "_button"] = ttk.Button(window,text=x).place(x=0, y=0) #,command=lambda x=x:nav_load_menu(x))

window.mainloop()
print(globals())

This will create a global variable module_1_button where the actual button is stored.

The second one is by using the value of a dict key as a variable like so. It will do the same thing in a better way.

# A recommended way
window = ttk.Tk()

buttons = {
    "button1": {
        "id": "module_1_button"
    }
}

for button in buttons:
    buttons[button]["id"] == ttk.Button(window,text=buttons[button]["id"]).place(x=0, y=0)


window.mainloop()

Note: I asked a pretty similar question, you can find it here

LiiVion
  • 312
  • 1
  • 10