0

Im trying to write a tkinter program that creates lots of new drop down lists and buttons depending on the user's choice. For example, a user chooses some option from a drop down list and another drop down list appears which depends on the the option that the user have chosen. Im trying to use the users choice but as soon as the optionmenu drop down list appears, the code keeps running and doesn't wait for the user's choice, I would like to know how to make the program to wait for the user's choice. Furthermore, if a user chooses some option from the drop down list after he already chosen something, I would like to interact with his decision again. How can I do it? I thought about using loops but then I wouldn't be able to proceed to the next operations.

  • `tkinter` programs are user event-driven, so the flow of control is not the same as it is under the procedural programming paradigm you are used to using. Unfortunately is is not something that can be taught via an answer to a single stackoverflow question. – martineau Aug 03 '22 at 07:58
  • needed some clarity is dropdown menus are countable like 10, 15, like or below 5 means can manage easily – ClownBunny Aug 03 '22 at 09:28

1 Answers1

0

I just created a sample work, may be use full to you

# It will be main list
main_list = ['One', 'two', 'three', 'four']

# it will be second list
sub_list = {'One': [11, 12, 13, 14], 'two': [21, 22, 23, 24], 'three': [31, 32, 33, 34], 'four': [41, 42, 43, 44]}

# importing module
from tkinter import *

# creating a window
root = Tk()
# just creating a fixed window
root.geometry("500x500")

# variable for option menu one
Value1 = StringVar()

# just a default value
Value1.set('Select Options')

# variable for second menu
Value2 = StringVar()

# creating a second option ment but not placing
secondmenu = OptionMenu(root, Value2, 'select')


# this method is called for the first option menu
def get2ndOptions(event):
    # get the 2nd list for options menu
    select_values = sub_list[Value1.get()]
    
    # setting the variable for the default value
    Value2.set(select_values[0])
    
    # pack is called for placement
    secondmenu.pack(pady=20)

    # updating the 2nd menu list 
    secondmenu["menu"].delete(0, "end")
    for item in select_values:
        secondmenu["menu"].add_command(
            label=item,
            command=lambda value=item: Value2.set(value))

# 1st option menu is created
OptionMenu(root, Value1, *main_list, command=get2ndOptions).pack(pady=20)

root.mainloop()
ClownBunny
  • 181
  • 9