jeudi 22 avril 2021

How do you precheck checkboxes in a loop?

I'm attempting to have it that when the table is loaded any completed tasks will have the checkbox prechecked and when the user checks it it updates the array so that I can store it in a file so that it will be checked next time it is loaded. I'm having trouble setting the checkbox to on and retrieving the value when it changes so the array can be updated.

from tkinter import *
Colours=['red','orange','yellow','green']
Priority=[1,0,0]
Completion=[0,1,0]
Date = ['D1','D2','D3']
DateCreated=['D1','D2','D3']
DateModified=['D1','D2','D3']
Action = ['4','3','3']
Project =['Project1','Project2','Project3']
Author = ['Person1','Person2','Person3']
ReportNo = ['124','654','2344']
Vessel=['Vessel1','Vessel2','Vessel3']
Tag = ['Tag1','Tag2','Tag3']
Comments = [['Comment1','Comment2','Comment3'],['Comment1','Comment2','Comment3'],['Comment1','Comment2','Comment3']]

class Table: 
      
    def __init__(self,root): 
        headers=[' ','Date Added', ' ','Created','Modified', 'Project','Author','Report no','Vessel','Tag','Comments']
        Checks={}
        # code for creating table 
        for i in range(total_rows): 
            for j in range(total_columns): 
                if j ==0 :
                    if i==0:
                        self.e = Entry(root, width=5, fg='blue', font=('Arial',10,'bold'))
                    else:
                        Checks[i]=IntVar(value=0)
                        self.e=Checkbutton(root, variable = Checks[i], onvalue = 1, offvalue = 0)

                elif j ==2 :  
                    self.e = Entry(root, width=5, fg='blue', font=('Arial',10,'bold'))
                elif j ==10 :  
                    self.e = Entry(root, width=100, fg='blue', font=('Arial',10,'bold'))
                else: 
                    self.e = Entry(root, width=10, fg='blue', font=('Arial',10,'bold'))
                  
                self.e.grid(row=i+1, column=j)
                if i == 0:
                    if j==0:
                        pass
                    else:
                        self.e.insert(END,headers[j])
                elif j==1:
                    self.e.insert(END, Date[i-1])
                elif j ==2 :
                    self.e.insert(END,Action[i-1])
                    if Action[i-1]==4 or Action[i-1]=='4':
                        self.e.configure(bg=Colours[0])
                    elif Action[i-1]==3 or Action[i-1]=='3':
                        self.e.configure(bg=Colours[1])
                    elif Action[i-1]==2 or Action[i-1]=='2':
                        self.e.configure(bg=Colours[2])
                    else:
                        self.e.configure(bg=Colours[3])
                    if Completion[i-1]==1 or Completion[i-1]=='1':
                        self.e.configure(bg='white')
                elif j==3:
                    self.e.insert(END, DateCreated[i-1])
                elif j==4:
                    self.e.insert(END, DateModified[i-1])
                elif j==5:
                    self.e.insert(END, Project[i-1])
                elif j==6:
                    self.e.insert(END, Author[i-1])
                elif j==7:
                    self.e.insert(END, ReportNo[i-1])
                elif j==8:
                    self.e.insert(END, Vessel[i-1])
                elif j==9:
                    self.e.insert(END, Tag[i-1])
                elif j==10:
                    self.e.insert(END, Comments[i-1])
                    
total_rows = len(Date)+1
total_columns = 11
root=Tk()
t = Table(root) 
root.mainloop()

Above is the code creating the table.




Aucun commentaire:

Enregistrer un commentaire