Python Forum

Full Version: Create a set of checkbutton widgets and refer to every one of them individually?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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]
[Image: iDH9e.png]



# -*- 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()
Quote:so I could click on a student's name “linked” with a checkbutton square and display his/her marks
This example that I found on the web long ago, shows how to associate a button with a name in a second list. You could also use a dictionary.
import tkinter as tk     # Python3

def cb_checked():
    # remove text from label
    label['text'] = ''
    for ctr, int_var in enumerate(cb_intvar):
        if int_var.get():     ## IntVar not zero==checked
            label['text'] += '%s is checked' % cb_list[ctr] + '\n'

root = tk.Tk()

cb_list = [
'apple',
'orange',
'banana',
'pear',
'apricot'
]

# list of IntVar for each button
cb_intvar = []
for this_row, text in enumerate(cb_list):
    cb_intvar.append(tk.IntVar())
    tk.Checkbutton(root, text=text,
                            variable=cb_intvar[-1],
                     command=cb_checked).grid(row=this_row, column=0, sticky='w')

label = tk.Label(root, width=20)
label.grid(row=20, column=0, sticky='w')

# you can preset check buttons (1=checked, 0=unchecked)
cb_intvar[3].set(1)
# show what is initially checked
cb_checked()

root.mainloop()