Home > Blockchain >  Tkinter Option menu and config
Tkinter Option menu and config

Time:07-07

I am trying to make a GUI that have two dropdown menus and a text label. The value selected in the first dropdown menu should update the list of options in the second dropdown menu. (this part of the code works correctly!). Then, once the user select a value from the second dropdown menu the text label should update. I am trying to update the text label with a using the config method, but it does not seem to work. any ideas?

import tkinter as tk
from tkinter import *

root = Tk()
root.title("My app")
root.minsize(width=330,height=280)

options1 = ["Option 1","Option 2"]
options2 = [""]
options21 =["Option 1.1","Option 1.2","Option 1.3","Option 1.4"]
options22 = ["Option 2.1","Option 2.2","Option 2.3","Option 2.4"]
labelvuot=Label(root,text= "                ")

def weigthtxt(event):
    if om2.get() == "":
        pass
    else:
        mydamage = int(om2.get().split(".")[1])
        if mydamage == 1:
            labelweigth.config(text="1")
        elif mydamage == 2:
            labelweigth.config(text="2")
        elif mydamage == 3:
            labelweigth.config(text="3")
        elif mydamage == 4:
            labelweigth.config(text="4")
        else:
            labelweigth.config(text="No number")

def add_option(self):
    om2.set("")
    labelweigth.config(text="")
    answer = om1.get()
    global options2
    options2.clear()
    if answer == "Option 1":
        options2 = options2 options21
        menu = drop2["menu"]
        menu.delete(0, "end")
        for x in options2:
            menu.add_command(label=x,
                             command=lambda value=x: om2.set(value))
    elif answer == "Option 2":
        options2 = options2   options22
        menu = drop2["menu"]
        menu.delete(0, "end")
        for x in options2:
            menu.add_command(label=x,
                             command=lambda value=x: om2.set(value))

def save():
    element = om1.get()
    damage = om2.get()
    inten = r1.get()
    exten = r2.get()
    pergiu = r3.get()
    txt = note.get(1.0,END)
    print(element,damage,inten,exten,pergiu,txt,int(om2.get().split(".")[1]))

label1=Label(root,text= "Select Element").grid(row=1,column=3)
labelvuot.grid(row=1,column=5)
om1 = tk.StringVar()
om1.set("")
drop = tk.OptionMenu(root, om1, *options1, command= add_option)
drop.config(width=20)
drop.grid(row=1,column=7,columnspan=3)

label2=Label(root,text= "Damage Type").grid(row=2,column=3)
om2 = tk.StringVar()
om2.set("")
drop2 = tk.OptionMenu(root,om2, *options2,command=weigthtxt)
drop2.config(width=20)
drop2.grid(row=2,column=7,columnspan=3)

label3=Label(root,text= "Weigth").grid(row=3,column=1,columnspan=3)
labelweigth = Label(root,text="")
labelweigth.grid(row=3,column=7,columnspan=3)

root.mainloop()

CodePudding user response:

tkinter module has an internal class _setit which is used by tk.OptionMenu class when setting up the menu actions.

You need to use this internal class when you populate the menu items inside add_option() function:

...
# it is executed when item of second dropdown menu is selected
def weigthtxt(value):
    if value:
        mydamage = int(value.split(".")[1])
        if mydamage == 1:
            labelweigth.config(text="1")
        elif mydamage == 2:
            labelweigth.config(text="2")
        elif mydamage == 3:
            labelweigth.config(text="3")
        elif mydamage == 4:
            labelweigth.config(text="4")
        else:
            labelweigth.config(text="No number")

def add_option(answer):
    om2.set("")
    labelweigth.config(text="")
    options2.clear()
    if answer == "Option 1":
        options2.extend(options21)
    elif answer == "Option 2":
        options2.extend(options22)
    menu = drop2["menu"]
    menu.delete(0, "end")
    for x in options2:
        menu.add_command(label=x, command=tk._setit(om2, x, weigthtxt))
...
  • Related