samedi 15 août 2020

Resetting Date Entry box and re-selection all check buttons in tkinter after button is pressed

I have created multiple check boxes using for loop. Here for simplicity, these are just 5 but actually they are up to 100. Now what I want is when "submit" button is pressed, then along with the execution of the function associated with button, the Date Entry box should again show the initial date i.e system's date and also all the unchecked check boxes should become checked again, I mean everything should become as it was before clicking the button. I tried to append all the check boxes in a list and mapped a function lambda x : x.select() on the list but it didn't work. So, now what can be the best solution for that?

from tkinter import *
from tkcalendar import DateEntry
import tkinter.messagebox as tkm

def submit_attendance():
    tkm.showinfo('Success', 'Attendance is taken.')

root = Tk()

frame = Frame(root)
frame.pack()

enterDate_Label = Label(frame, text = 'Select Date : ').pack()
    
Date_Entry = DateEntry(frame, date_pattern = 'dd/mm/yyyy').pack()

note_Label = Label(frame, text = 'Note: Uncheck the boxes for absentees').pack()

text = Text(frame, cursor = 'arrow')
text.pack()

students = [{'Reg': '2018-MC-01', 'Name': 'Hussain Ahmed'},
            {'Reg': '2018-MC-02', 'Name': 'Kamran Akmal'},
            {'Reg': '2018-MC-03', 'Name': 'Virat Kohli'},
            {'Reg': '2018-MC-04', 'Name': 'Abdul Ghani'},
            {'Reg': '2018-MC-05', 'Name': 'Hafiz Faizan Shahid'}]

varis = []
for std in students:
    reg = std['Reg']
    name = std['Name']
    var = StringVar()
    cb = Checkbutton(text, text = f"{reg}\t{name}", variable = var, offvalue = f"{reg}\t{name}")
    cb.select()
    text.window_create('end', window=cb)
    text.insert('end', '\n')
    varis.append(var)

Button(frame, text='Submit'.upper(), command = submit_attendance).pack()

mainloop()



Aucun commentaire:

Enregistrer un commentaire