0

I'm trying to build a python app where we can make our working schedules. I want to be able to press 2 checkbuttons per employee and to export the ones that are checked into a CSV file. The way i have set up the code right now i'm generating the Checkbuttons in for loops, so i can't give them individual names/values. is there still a way i can see which ones are pressed?

(the variable "times" contains a list of all the available times, starting at 14:00 and ending at 23:00)

#building UI
master = Tk()
emplindex = 1
timeindex = 1
for i in times:
    timelabel = Label(master, text=" " + i + " ").grid(row=0, column=timeindex)
    timeindex +=1
for i in employees:
    namelabel = Label(master, text=i[1]).grid(row=emplindex, column=0)
    timeindex = 1
    for i in times:
        CB =  Checkbutton(master).grid(row=emplindex,column=timeindex)
        timeindex +=1
    emplindex +=1
buildbutton = ttk.Button(master, text = "BUILD SCHEDULE", command=lambda: buttonclicked()).grid(row=100)
def buttonclicked():
    selected = CB
master.mainloop()

This is what the ui outputs

3 Answers 3

2

You need to associate a variable with the checkbuttons, and save references to the buttons in a list or dictionary. You can then iterate over the variables to determine which ones were checked.

Here's a simple example to illustrate the point:

import tkinter as tk

def submit():
    for i, var in enumerate(cbvars):
        print(f"{i}: {var.get()}")

root = tk.Tk()

cbvars = []
for i in range(10):
    var = tk.IntVar(root, value=0)
    cbvars.append(var)
    cb = tk.Checkbutton(root, text=f"Item #{i}", variable=var)
    cb.pack(side="top", anchor="w")

button = tk.Button(root, text="Submit", command=submit)
button.pack()

root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

0

You can:

1: Store your Checkbuttons inside an array and get the status

Or

2: pass a function as command to checkbutton and get in which emplindex and timeindex checkbuttonis ticked

CB=Checkbutton(master,command=lambda:checked(emplindex,timeindex)
def checked(emplindex,timeindex):
    #whatever...

2 Comments

How do you expect them to get the status in the first part of your answer?
@Bryan Oakley If using ttk than by checkbutton.state() get status If not then your answer is the way to do
0

You can use a dictionary with employee name as the key to store all the variables associated with the checkbuttons, then you can use this dictionary of variable list to generate the CSV file you want.

Also you can use command option of Checkbutton to limit the number of checked buttons to be at most two inside the command callback.

import tkinter as tk
from tkinter import ttk
import csv

def on_check(varlist, var):
    count = sum(v.get() for v in varlist)
    if count > 2:
        # at most two check buttons can be checked
        var.set(0)

times = [
    '14:00', '14:30',
    '15:00', '15:30',
    '16:00', '16:30',
    '17:00', '17:30',
    '18:00', '18:30',
    '19:00', '19:30',
    '20:00', '20:30',
    '21:00', '21:30',
    '22:00', '22:30',
    '23:00',
]
employees = ['niek', 'jeroen', 'veronica']

master = tk.Tk()

# time slot headings
for col, slot in enumerate(times, 1):
    tk.Label(master, text=slot).grid(row=0, column=col)

cblist = {}  # dictionary to store variable list with employee name as key
for row, name in enumerate(employees, 1):
    tk.Label(master, text=name).grid(row=row, column=0)
    varlist = []
    for col, slot in enumerate(times, 1):
        var = tk.IntVar(value=0)
        cb = tk.Checkbutton(master, variable=var, command=lambda l=varlist, v=var: on_check(l, v))
        cb.grid(row=row, column=col)
        varlist.append(var)
    cblist[name] = varlist

def button_clicked():
    with open('schedule.csv', 'w', newline='') as f:
        writer = csv.writer(f)
        for name, varlist in cblist.items():
            row = name, *[slot for slot,var in zip(times,varlist) if var.get() == 1]
            writer.writerow(row)

ttk.Button(master, text='BUILD SCHEDULE', command=button_clicked).grid(row=100)

master.mainloop()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.