I am a teacher. Using Tkinter and Openpyxl I need to code a graphical user interface so I could click on a student's name “linked” with a checkbutton square and display his/her marks (saved on a “.xlsx” file) whenever his/her checkbutton is active. How can I create a set of widgets/checkbuttons that I can refer individually later on? My code fails.
![[Image: 9lWrw.png]](https://i.stack.imgur.com/9lWrw.png)
![[Image: iDH9e.png]](https://i.stack.imgur.com/iDH9e.png)
![[Image: 9lWrw.png]](https://i.stack.imgur.com/9lWrw.png)
![[Image: iDH9e.png]](https://i.stack.imgur.com/iDH9e.png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# -*- coding: utf-8 -*- from tkinter import * import openpyxl wb = openpyxl.load_workbook( 'marks.xlsx' , data_only = True ) sheet = wb.active names_and_rows = {} for i in range ( 2 , sheet.max_row + 1 ): name = sheet.cell(row = i, column = 1 ).value names_and_rows[name] = i root = Tk() root.title( "Student's marks" ) students_names = Frame(root, bd = 1 , relief = "solid" ) students_names.pack(side = "left" ) student_marks = Frame(root, bd = 1 , relief = "solid" ) student_marks.pack(side = "right" ) message = Label(student_marks, text = "You still haven't checked on a ny student's name") message.pack() def get_marks(v): marks = "" for i in range ( 2 , sheet.max_column + 1 ): information = str (sheet.cell(row = 1 , column = i).value) + ": " + str (sheet.cell(row = v, column = i).value) + "\n" marks = marks + information if (v.get() = = 1 ): message.config(text = marks) else : message.config(text = "You still haven't checked on any student's name") list_of_widgets = [] for k, v in names_and_rows.items(): square = Checkbutton(students_names, variable = v, onvalue = 1 , offvalue = 0 , text = k, command = lambda : get_marks(v)) list_of_widgets.append(square) square.pack() root.mainloop() |