mardi 15 février 2022

Is there an automatic way to recognize the plurality of multiple selected checkboxes?

I would like to obtain that if I select the first or the third checkbox individually I get the print in the textbox, or that if I select the first and the third at the same time I get the print in the textbox. Is there an automatic way to recognize the plurality of multiple selected checkboxes? The problem is this function:

def aaa():
    if Button1_func() and Button2_func() and Button3_func():
        textbox.insert("end", "Ok")

Currently I don't get this because in Button2_func I voluntarily wrote 5 + 3 = 7, so that's right, that's fine. However, I would also like that if I do NOT select checkbox2, but select the others 1 or 3 individually, or at the same time 1 and 3, I get the print of "ok" in the textotox

I guess the solution is to create other conditions for each type of multiple selection case, but I think there is a more automatic and better way without creating selection cases with conditions, because I will have to add dozens of other checkboxes and the situation would get complicated.

IMPORTANT: Please do not make sarcasm about True and False, because 5 + 3 = 8 is just a simple example to understand the logic of the checkboxes to which I will have to apply other different code. Leave the return True and False as is, without changing the functions.

    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("600x600")
    root.configure(bg='white')
    
    Checkbutton1 = IntVar()
    Checkbutton2 = IntVar()
    Checkbutton3 = IntVar()
    
    #CHECKBOX'S FUNCTIONS
    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
       
    #CHECKBOX 
    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=36)
    
    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=66)
    
    Button3 = Checkbutton(root, text = "Checkbox 3", variable = Checkbutton3, onvalue = 1, offvalue = 0, height = 1,
                          bg="white", foreground='black', activebackground="white", command=Button3_func)
    Button3.place(x=10, y=146)
    
    #BUTTON FUNCTION
    def aaa():
        if Button1_func() and Button2_func() and Button3_func():
            textbox.insert("end", "Ok")
    
    #TEXTOBOX
    textbox = tk.Text(root, width=33, height=10, font=('helvetic', 12))
    textbox.place(x=30, y=220)
    
    #BUTTON
    button = tk.Button(root, text="Ok", command= lambda: [aaa()])
    button.pack()


root.mainloop()



Aucun commentaire:

Enregistrer un commentaire