dimanche 2 août 2020

Creating and Getting values from tkinter check boxes using for loop

This code is a part of my project in which i have to manage the attendance of 50 (or more) students.

The thing I want is that all the check boxes should initially be 'checked' (showing the present state) and when I uncheck random checkboxes (to mark the absent), and click the 'Submit' button (yet to be created at the bottom of the window), I should get a list with 'entered date' as first element and the roll numbers i.e. 2018-MC-XX as other elements. For example : ['01/08/2020', '2018-MC-7', '2018-MC-11', '2018-MC-23', '2018-MC-44']. Actually my plan is when i will get a list i will easily write it to a text file. Also, if there is another way of creating multiple scrollable check boxes without packing them inside a canvas then please do tell!

    from tkinter import *
    from tkcalendar import DateEntry

    root = Tk()
    root.geometry('920x600+270+50')
    root.minsize(920,600)

    Attendance_frame = Frame(root)    ### Consider it a Main Frame
    Attendance_frame.pack()

    attendaceBox = LabelFrame(Attendance_frame, text = 'Take Attendance', bd = 4, relief = GROOVE, labelanchor = 'n',font = 'Arial 10 bold', fg = 'navy blue', width = 850, height = 525)     # A Label Frame inside the main frame

    attendaceBox.pack_propagate(0)
    attendaceBox.pack(pady = 15)

    dateFrame = Frame(attendaceBox)    # A small frame to accommodate date entry label & entry box
    dateFrame.pack(anchor = 'w')

    font = 'TkDefaultFont 10 bold'
    date_label = Label(dateFrame, text = 'Enter Date : ', font = font).grid(row = 0, column =  0, sticky = 'w', padx = 10, pady = 10)

    date_entry = DateEntry(dateFrame, date_pattern = 'dd/mm/yyyy', showweeknumbers = FALSE, showothermonthdays = FALSE)
    date_entry.grid(row = 0, column = 1, sticky = 'w')

    noteLabel = Label(attendaceBox, text = 'Note: Uncheck the boxes for absentees').pack(anchor = 'w', padx = 10, pady = 5)

    canvas = Canvas(attendaceBox, borderwidth=0, background="#ffffff")
    checkFrame = Frame(canvas, width = 100, height = 50)
    vsb = Scrollbar(canvas, orient="vertical", command=canvas.yview)
    canvas.configure(yscrollcommand=vsb.set)

    vsb.pack(side="right", fill="y")
    canvas.pack(side="left", fill="both", expand=True)
    canvas.pack_propagate(0)
    canvas.create_window((4,4), window=checkFrame, anchor="nw")

    def onFrameConfigure(canvas):
        '''Reset the scroll region to encompass the inner frame'''
        canvas.configure(scrollregion=canvas.bbox("all"))

    checkFrame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))

    for i in range(0,51):     # A loop to create Labels of students roll numbers & names
        c = Checkbutton(checkFrame, text = f"{'2018-MC-'+str(i+1)}       Student {i+1}")
        c.grid(row = i, column = 0, padx = 10, sticky = 'w')


    mainloop()



Aucun commentaire:

Enregistrer un commentaire