title: Python | Tkinter | Assign different number of Option Menu's to different numbers of labels and extract the data in a pythonic way
body: <p>I have a situation where I have two lists of different lengths. These lists may not be consistent every time this script is run, but for the purposes of this example, I'm going to use students and grades.</p>
  <p>In this example, I have 4 students and 6 grades. I want to be able to generate a GUI where I have a label for each student in the columns and then assign a letter grade (A-F) to each student. I've managed to create the labels in a dictionary format using dictionary comprehension, as the number of students may change.</p>
  <p>The problem arises when I try to get an equal amount of option menus to assign the letter grade. Rather than equaling the number of students, it will equal the number of grades (6 in this example).</p>
  <p>1). How do I get the number of option menus to equal the number of students (but retain the 6 grades in the grade list)?</p>
  <p>2). How can I dynamically extract (return?) the assigned student grade to the debbger/main memory to be used in later calculations outside of the class?</p>
  <p>Here's my code:</p>
  <pre><code>import tkinter as tk
  from tkinter import Tk, filedialog, ttk, StringVar, OptionMenu, Button
  
  root = tk.Tk()
  root.geometry(&quot;1200x600&quot;)
  root.resizable(True, True)
  root.title(&quot;Define Grades&quot;)
  
  
  class DefineGrades:
      def __init__(self, master):
          &quot;&quot;&quot;Creates a GUI to assign letter grades (n=6) to a limited number of students (n=4)&quot;&quot;&quot;
          self.students = ['Student_1', 'Student_2', 'Student_3', 'Student_4']
          self.grades = ['A', 'B', 'C', 'D', 'E', 'F']
  
          # Create labels for the students in the GUI
          self.student_labels = {student: ttk.Label(root, text=f'Select the grade for {student}')
                                 for student in self.students}
          # Place the student label on the GUI
          _ = {student: self.student_labels[student].grid(column=0, row=student_num, pady=5)
               for student_num, student in enumerate(self.students)}
  
          # Create the option menu for the grades to be assigned
          self.grade_selection_menu = {grade: StringVar(root) for grade in self.grades}
          _ = {grade: self.grade_selection_menu[grade].set('Set Grade')
               for grade in self.grades}
          self.grade_selection = {grade: OptionMenu(root, self.grade_selection_menu[grade], *self.grades)
                                for grade in self.grades}
  
          # place the option menus for the grades on the GUI
          _ = {grade: self.grade_selection[grade].grid(column=1, row=grade_num)
               for grade_num, grade in enumerate(self.grades)}
  
          # Create a button to submit the data to the main memory
          self.submit_button = ttk.Button(master, text=&quot;Submit Grades&quot;, command=self.submit_grades)
          self.submit_button.grid(column=2, row=0)
  
      def submit_grades(self):
          global submitted_grades
  
          submitted_grades = {grade: self.grade_selection[grade].get()
                              for grade in self.grades}
          return submitted_grades
  
  
  _ = DefineGrades(root)
  
  root.mainloop()
  
  print(submitted_grades)
  </code></pre>