Home > Back-end >  Simple problem in multiple checkbox selection with executable example. Is there automatic way to rec
Simple problem in multiple checkbox selection with executable example. Is there automatic way to rec

Time:02-17

Is there an automatic way to recognize the plurality of multiple selected checkboxes? I don't want to manually create multiple selection combinations. I would like to get that:

  • 1/3 checkbox: if I select the first or third box individually, I get the print "ok" in the text box
  • 2/3 checkbox: if I select the first and third at the same time, I get the print "ok" in the text box. But only one "ok" printed, and not two ok as "ok ok" (so two checkboxes correspond to one ok)
  • I don't want to print the second checkbox in the textbox, because I intentionally wrote an error on it.

The problem is this function. Probably AND (or OR) is not what I need, I guess, I don't know:

def aaa():
    if Checkbutton1.get() and Checkbutton2.get() and Checkbutton3.get():
        textbox.insert("end", "Ok")

#I also tried to write: if Button1_func() and Button2_func() and Button3_func():

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.

Can anyone possibly help me by showing me the correct code? Thank you

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 Checkbutton1.get() and Checkbutton2.get() and Checkbutton3.get():
        textbox.insert("end", "Ok")

    #I also tried to write: if Button1_func () and Button2_func () and Button3_func ():

#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()

UPDATE In the code there is also this with which I save and load the checkboxes:

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

CodePudding user response:

You can use a set to store the function references based on the state of those checkbuttons. Then inside aaa() executes all the functions in the set and determine whether to put OK into the text box or not:

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

funclist = set()

#CHECKBOX
Button1 = tk.Checkbutton(root, text = "Checkbox 1", variable = Checkbutton1, onvalue = 1, offvalue = 0, height = 1,
                         bg="white", foreground='black', activebackground="white",
                         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="white", foreground='black', activebackground="white",
                         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="white", foreground='black', activebackground="white",
                         command=lambda: clicked(Checkbutton3.get(), Button3_func))
Button3.place(x=10, y=146)

#BUTTON FUNCTION
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")

CodePudding user response:

Edited for clarified question

Because each Checkbutton has a unique function associated with it, you will need to define these all manually. For now, I've substituted functions to stand in.

You don't actually need to pass the functions to your checkbuttons, because they are only evaluated when you click your Button. Instead, you can change aaa so that it creates a list of the function results for the ticked Checkbuttons. If any of these return False, don't print "OK". But if they all return True, print "OK"

import tkinter as tk

root = tk.Tk()
root.geometry("600x600")

# How many checkboxes you want
buttons = 5
button_eval = [0 for i in range(buttons)]

# If your functions are unique for each checkbox, you will need to define them here
def foo():
    return True
def bar():
    return False
my_functions = [foo, bar, foo, bar, foo] # You need to have 5 functions in this case for this to work

# Creating variables, buttons etc
x = [10, 10, 10, 10, 10]
y = [36, 66, 96, 126, 156]
checkbuttons_vars = [tk.IntVar() for _ in range(buttons)]
checkbuttons = [tk.Checkbutton(root, text=f"Checkbox {i 1}", variable=checkbuttons_vars[i], onvalue=1, offvalue=0) for i in range(buttons)]
for i, box in enumerate(checkbuttons):
    box.place(x=x[i],y=y[i])

#BUTTON FUNCTION
def aaa():
    function_results = [function() for i, function in enumerate(my_functions) if checkbuttons_vars[i].get()]
    textbox.delete(1.0, "end")
    if False not in function_results:
        textbox.insert(1.0, "Ok")

#TEXTOBOX
textbox = tk.Text(root, width=33, height=10, font=('helvetic', 12))
textbox.pack()

#BUTTON
button = tk.Button(root, text="Ok", command=aaa)
button.pack()

root.mainloop()
  • Related