0

I am trying to create options menu runtime by getting how many options menu they want in runtime.

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=['','','','','','']
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,a[num],*list2)
    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()

In number of products text box enter the number of options menu that is required.The buttons are getting created.However I am getting error as below

enter image description here

3
  • Hello,below code works Commented Dec 24, 2017 at 7:16
  • create empty list and use append() to add new element to this list - this way you will not have problem with index. Commented Dec 24, 2017 at 7:52
  • Hello,I have a diubt.I used append to add in list.I am facing other issues which i posted in below link.stackoverflow.com/questions/47966301/… can you please let me know your suggestion. Commented Dec 25, 2017 at 9:24

2 Answers 2

2

You can create empty lists and use append() then there is no problem with index.

BTW: because you have numbers as options so you can use list with numbers [1, 2, 3, 4, 5] and IntVar

import tkinter as tk

# --- functions ---

def add_optins_menus(number):
    global all_optins_menus
    global all_vars

    # remove previous options menus
    for widget in all_optins_menus:
        widget.destroy()

    all_optins_menus = []
    all_vars = []

    # add new options menu
    for num in range(number_var.get()):
        var = tk.IntVar()

        widget = tk.OptionMenu(top, var, options)
        widget.pack(side="left")

        all_vars.append(var)
        all_optins_menus.append(widget)

# --- main ---

options = [1, 2, 3, 4, 5]

all_optins_menus = []
all_vars = []

# ---

top = tk.Tk()

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

number_var = tk.IntVar()

options_menu = tk.OptionMenu(top, number_var, *options, command=add_optins_menus)
options_menu.pack(side="left")

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

Comments

0

Hello Below code works.

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):
    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()

1 Comment

You should explain the modifications a bit for it to be good answer.

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.