[英]Issue in accessing elements in array of option menu created in python tkinter
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. 这里的选项菜单是动态创建的。但是,当我更改选项菜单时,由于我不知道如何找到被单击的选项菜单,因此不会附加新值。
OptionMenu
executes function with one agument - selected value - so you have to use lambda
with two arguments OptionMenu
以一个参数(选定的值)执行函数-因此您必须使用带有两个参数的lambda
command=lambda val, n=num:new_number_of_pds(val, n))
First is selected value, second will be optionmenu's number "num". 第一个是选择的值,第二个是选项菜单的数字“ 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. 我不知道您要如何处理这些元素,但是如果您要替换列表中的项目,那么也许您应该创建包含5个空元素的列表,您可以用值替换它们。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.