Python Forum
[Tkinter] Create a set of checkbutton widgets and refer to every one of them individually?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Create a set of checkbutton widgets and refer to every one of them individually?
#1
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()
Reply
#2
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Checkbutton writing selection to file blakefindlay 1 2,079 Jan-28-2021, 01:56 PM
Last Post: deanhystad
Question [Tkinter] Checkbutton clicks events does not update visually. nicolaask 1 2,920 Dec-20-2020, 06:11 PM
Last Post: nicolaask
  [Tkinter] How to insert 'Checkbutton' to 'Treeview' and associate each item? water 2 13,000 Dec-19-2020, 05:24 PM
Last Post: water
  Find Checkbutton State PEGylated_User 3 3,051 Oct-23-2020, 12:43 AM
Last Post: Larz60+
  [tkinter] not getting checkbutton value when clicked OogieM 5 5,893 Sep-20-2020, 04:49 PM
Last Post: deanhystad
  [Tkinter] ttk.Checkbutton set on/off ifigazsi 5 10,310 Apr-04-2020, 07:34 PM
Last Post: deanhystad
  tkinter checkbutton if checked MC2020 2 5,925 Jan-21-2020, 07:08 PM
Last Post: joe_momma
  [Tkinter] Can't seem to get the current state of a checkbutton. p_hobbs 6 3,268 Nov-07-2019, 11:38 PM
Last Post: p_hobbs
  doing something after been checked a checkbutton gray 1 4,236 Feb-18-2017, 05:11 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020