Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

enter image description here

I created this tkinter window and created these drop down boxes.

        while number != (max+1):
            Subject1_Label = Label(master, text=("Subject", number))
            Subject1_Label.grid(row=number, column=0, pady=6)
            Subject1 = All_Subjects.get(number)
            variable = StringVar(master)
            variable.set(Subject1[0])
            w = OptionMenu(master, variable, *Subject1)
            w.grid(row=number, column=1, pady=6)
            subject_amount = subject_amount + 1
            number = number + 1

Usually to determine the input from the dropdown boxes I could simply use (variable.get) but in this case variable is being used 5 times to create 5 different boxes. Is there a way I can gather the input from these dropdown boxes using their position on the tkinter window. Such as (variable.get, row=1, column=1)? Or any other way?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
218 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use list or dictionary to store the variables. Below is an example using dictionary:

variables = {}
while number != (max+1):
    subject = f"Subject {number}"
    Subject1_Label = Label(master, text=subject)
    Subject1_Label.grid(row=number, column=0, pady=6)
    Subject1 = All_Subjects.get(number)
    variable = StringVar(master)
    variable.set(Subject1[0])
    variables[subject] = variable  # save the variable
    w = OptionMenu(master, variable, *Subject1)
    w.grid(row=number, column=1, pady=6)
    subject_amount = subject_amount + 1
    number = number + 1

Then you can use something like variables["Subject 1"].get() to get the selected item of "Subject 1".


Update with an example:

from tkinter import *

# just for providing "All_Subjects.get(number)" in the while loop
class All_Subjects:
    subjects = ("math stand", "english standard", "chem", "timber", "child studies")
    def get(n):
        return All_Subjects.subjects

master = Tk()

max = 5
variables = {}
number = 1
subject_amount = 0
while number != (max+1):
    subject = f"Subject {number}"
    Subject1_Label = Label(master, text=subject)
    Subject1_Label.grid(row=number, column=0, pady=6)
    Subject1 = All_Subjects.get(number)
    variable = StringVar(master)
    variable.set(Subject1[0])
    variables[subject] = variable  # save the variable
    w = OptionMenu(master, variable, *Subject1)
    w.grid(row=number, column=1, pady=6)
    subject_amount = subject_amount + 1
    number = number + 1

def show_subjects():
    # use "Subject X" as key to get the selected subjects 
    for i in range(1, max+1):
        subject = f"Subject {i}"
        print(f"{subject}: {variables[subject].get()}")
    # or simply
    #print([var.get() for var in variables.values()])

Button(master, text="Next", command=show_subjects).grid(column=2, padx=10, pady=10)

master.mainloop()

Output:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...