mercredi 16 février 2022

Apply multiple selection functions to checkboxes (they are saved and loaded from the database)

I have an executable code that saves and loads checked or unchecked checkboxes in a database. This works correctly.

Each checkbox has functions that I talked about in this question by kind user acw1668 (read the solution). I would like to apply the solution to my code of this question. You can take a cue from the response code of the user who solved the problem of multiple selection of checkboxes based on True or False. I would like to apply the solution of the other question of the link, to this complete code of mine that I report in this new question, because in addition the code of this new question has the saving and loading of checkboxes.

What I want? I would like that when you click on the Print button, "Ok" is printed in the textbox based on the multiple selection of the checkboxes and their function. If a checkbox returns False based on the function, then it does not print "ok". If a checkbox returns True based on the function, then it prints "ok" in the textbox. If all 3 checkboxes are selected, nothing is printed because among them there is checkbox2 which resist False. If, for example, checkboxes 1 and 3 are selected which are True, then "ok" is printed.

The code I want to add (that of the other question of link) whose purpose is to recognize the plurality of the multiple checkboxes selected is:

def clicked(flag, func):
    if flag:
        funclist.add(func)
    else:
        funclist.remove(func)

funclist = set()

def aaa():
    # if need to clear the text box, uncomment below line
    #textbox.delete("1.0", "end")
    if funclist and all(func() for func in funclist):
        textbox.insert("end", "Ok")

My code that saves and loads checkboxes, including GUI, is:

import sqlite3
from tkinter import *
from tkinter import ttk
import tkinter as tk
import tkinter.messagebox
from tkinter import messagebox

root = tk.Tk()
root.geometry("400x500")
root.configure(bg='white')

chk_lst = []

#CHECKBOX
Checkbutton1 = IntVar()
Checkbutton2 = IntVar()
Checkbutton3 = IntVar() 

#CHECKBOX FUNCTION

def Button1_func():
    if 5 + 3 == 8:
        return True
    else:
        return False

def Button2_func():
    if 5 + 3 == 7:
        return True
    else:
        return False

def Button3_func():
    if 5 + 3 == 8:
        return True
    else:
        return False
    
Button1 = Checkbutton(root, text = "Checkbox 1", variable = Checkbutton1, onvalue = 1, offvalue = 0, height = 1,
                      bg="white", foreground='black', activebackground="white", command=Button1_func)
Button1.place(x=10, y=60)

Button2 = Checkbutton(root, text = "Checkbox 2", variable = Checkbutton2, onvalue = 1, offvalue = 0, height = 1,
                      bg="white", foreground='black', activebackground="white", command=Button2_func)
Button2.place(x=10, y=100)

Button3 = Checkbutton(root, text = "Checkbox 2", variable = Checkbutton3, onvalue = 1, offvalue = 0, height = 1,
                      bg="white", foreground='black', activebackground="white", command=Button3_func)
Button3.place(x=10, y=140)

chk_lst.extend([Checkbutton1,Checkbutton2,Checkbutton3])


#SAVE IN DATABASE
def save():
    conn = sqlite3.connect("value.db")
    c = conn.cursor()
    
    for idx,chk_btn in enumerate(chk_lst,start=1):
        c.execute(f'SELECT button1 FROM table1 WHERE id=?',(idx,))
        rec = c.fetchall()

        if rec:
            c.execute("UPDATE table1 SET Button1=? WHERE id=?;", (chk_btn.get(),idx))
        else:
            c.execute("INSERT INTO table1 VALUES (?,?,?);", (idx,chk_btn.get()))
        
    conn.commit()
    conn.close()

    messagebox.showinfo("Saved successfully","Saved successfully")


#LOAD WHEN OPEN WINDOWS
def load():
    conn = sqlite3.connect("value.db")
    c = conn.cursor()
    c.execute("SELECT * FROM table1")
    vals = c.fetchall()
    
    for val,chk_btn in zip(vals,chk_lst):
        chk_btn.set(val[1])
    
    conn.close()


#SAVE BUTTON
save = Button(root, text="Save", bg='#b40909', foreground='white', command= save)
save.place(x=10, y=10)

#PRINT BUTTON
button = tk.Button(root, text="Print", command= lambda: [aaa()])
button.place(x=100, y=10)

#TEXTOBOX
textbox = tk.Text(root, width=33, height=10, font=('helvetic', 12))
textbox.place(x=10, y=220)

load()

root.mainloop()

Simple database:

CREATE TABLE "table1" (
    "id"    INTEGER,
    "Button1"   TEXT,
    PRIMARY KEY("id" AUTOINCREMENT)
);



Aucun commentaire:

Enregistrer un commentaire