0

I created options menu in run time based on user inputs.

import tkinter
import tkinter.messagebox
from tkinter import *

top = tkinter.Tk()
number_of_pd = Label(top, text="Number of Products")
list2 = ['1','2','3','4','5']
E_Name=['','','','','','']
a=[]
b=[StringVar(),StringVar(),StringVar(),StringVar(),StringVar()]
def New_number_of_pds(b):
    a.append(b)
    print(a)
    print(b)
def New_number_of_pd(oE_number_of_pd):
 y_axis=300
 num=1
 for num in range(int(oE_number_of_pd)):
    y_axis=y_axis+75
    E_Name[num]= OptionMenu(top,b[num],*list2,command=New_number_of_pds)
    E_Name[num].place(x=y_axis,y=150)
oE_number_of_pd=StringVar()
E_number_of_pd= OptionMenu(top, oE_number_of_pd, *list2,command=New_number_of_pd)
number_of_pd.place(x=150,y=75)
E_number_of_pd.place(x=300,y=75)
top.title('Sri Sai')
top.geometry('2000x1000') # Size 200, 200
top.mainloop()

here options menu is created on the fly.However when i change the options menu the new value is appended not updated since i dont know how to find which option menu is clicked.

enter image description here

enter image description here

1 Answer 1

0

OptionMenu executes function with one agument - selected value - so you have to use lambda with two arguments

command=lambda val, n=num:new_number_of_pds(val, n))

First is selected value, second will be optionmenu's number "num".

And then function has to get two arguments

def new_number_of_pds(value, optionmenu_number):

I don't know what you try to do with those elements but if you want to replace item in list then maybe you should create list with 5 empty elements which you can replace with values.

optionmenu number: 0
value: 3
a: ['3', None, None, None, None]
-----
optionmenu number: 2
value: 5
a: ['3', None, '5', None, None]
-----

Code

import tkinter as tk

# --- functions ---

def new_number_of_pds(value, optionmenu_number):
    print('optionmenu number:', optionmenu_number)
    print('value:', value)

    a[optionmenu_number] = value
    print('a:', a)
    print('-----')

def new_number_of_pd(oE_number_of_pd):
    global a

    # delete previous values 
    a = [None, None, None, None, None]

    for num in range(int(oE_number_of_pd)):
        E_Name[num] = tk.OptionMenu(top, b[num], *list2, command=lambda val, n=num:new_number_of_pds(val, n))
        E_Name[num].pack(side="left")

# --- main ---

list2 = ['1', '2', '3', '4', '5']

E_Name=[None, None, None, None, None]

a = [None, None, None, None, None]

top = tk.Tk()
b = [tk.StringVar(), tk.StringVar(), tk.StringVar(), tk.StringVar(), tk.StringVar()]

number_of_pd = tk.Label(top, text="Number of Products")
number_of_pd.pack(side="left")

oE_number_of_pd = tk.StringVar()

E_number_of_pd = tk.OptionMenu(top, oE_number_of_pd, *list2, command=new_number_of_pd)
E_number_of_pd.pack(side="left")

top.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.