I have a problem with 3 checkboxes intersecting when using list in checkbox functions. I would like to use intersection in relation to the quantity and combination of selected checkboxes.
I tried to insert the checkbox functions in a list and combine them with a condition if the checkboxes are only selected or deselected:
all_test = [(Button1_func()) if Checkbutton1.get() else not Checkbutton1.get(),
(Button2_func()) if Checkbutton2.get() else not Checkbutton2.get(),
(Button3_func()) if Checkbutton3.get() else not Checkbutton3.get()]
Next, I put the list in c = set.intersection(*all_test)
If I try to select one or more checkboxes, I get the error:
TypeError: descriptor 'intersection' for 'set' objects doesn't apply to a 'bool' object
Let me explain better with an example what I would like to achieve. For example if I select only checkbox1 I would like to print a, b, c, d, e. If I select all the checkboxes I would like to print only a, b, because the elements in common in the three lists are only a, b. How can I print what I said above without getting errors?
I'm looking for a solution that allows me to manage even 20 checkboxes if I need to extend the code in the future.
from tkinter import ttk
import tkinter as tk
root = tk.Tk()
root.geometry("300x300")
funclist = set()
Checkbutton1 = tk.IntVar()
Checkbutton2 = tk.IntVar()
Checkbutton3 = tk.IntVar()
#CHECKBOX'S FUNCTIONS
def Button1_func():
test1 = ["a", "b", "c", "d", "e"]
return test1
def Button2_func():
test2 = ["a", "b", "c"]
return test2
def Button3_func():
test3 = ["a", "b"]
return test3
def clicked(flag, func):
if flag:
funclist.add(func)
else:
funclist.remove(func)
#CHECKBOX
Button1 = tk.Checkbutton(root, text = "Checkbox 1", variable = Checkbutton1, onvalue = 1, offvalue = 0, height = 1,
bg="#d9d9d9", foreground='black', activebackground="#d9d9d9",
command=lambda: clicked(Checkbutton1.get(), Button1_func))
Button1.place(x=10, y=36)
Button2 = tk.Checkbutton(root, text = "Checkbox 2", variable = Checkbutton2, onvalue = 1, offvalue = 0, height = 1,
bg="#d9d9d9", foreground='black', activebackground="#d9d9d9",
command=lambda: clicked(Checkbutton2.get(), Button2_func))
Button2.place(x=10, y=66)
Button3 = tk.Checkbutton(root, text = "Checkbox 3", variable = Checkbutton3, onvalue = 1, offvalue = 0, height = 1,
bg="#d9d9d9", foreground='black', activebackground="#d9d9d9",
command=lambda: clicked(Checkbutton3.get(), Button3_func))
Button3.place(x=10, y=96)
all_test = [(Button1_func()) if Checkbutton1.get() else not Checkbutton1.get(),
(Button2_func()) if Checkbutton2.get() else not Checkbutton2.get(),
(Button3_func()) if Checkbutton3.get() else not Checkbutton3.get()]
def try_print():
#if each checkbox is True:
if funclist and all(func() for func in funclist):
c = set.intersection(*all_test)
print(c)
#PRINT BUTTON
button = tk.Button(root, text="Print", command= lambda: [try_print()])
button.place(x=10, y=140)
root.mainloop()
Aucun commentaire:
Enregistrer un commentaire