Skip to content
Advertisement

How can you find which checkbutton is selected using Tkinter and a function?

Here I am trying to create a list of three fruits , Apple ,Pomegranate and Banana, and asking the user to select their favorite fruit. So I am defining a function print_choice that is called each time any checkbox is selected , and have defined an array fruits that appends the respective fruit to the array when the checkbox is selected. I have defined the array inside the function instead of global declaration since the values should not be added twice . However I am not able to get the choice to be printed inside a label box that supposed to display the list of fruits selected by the user. Could you please help me find out where I’ve made a mistake and how can I display the fruits selected ?

 import tkinter as tk
from tkinter import *

parent = tk.Tk()
parent.title("My favorite fruits")
l= tk.Label(parent, background="yellow", text="empty", width="30")
l.pack()
w = tk.Label(parent , text ="Select your favorite fruits!", bg="pink", fg = "white")

txt = "you love "
def print_choice():
    fruits=list()
    global txt
    flag=0
    while flag==0:
         if checkvar1.get()==1 and checkvar2.get==0 and checkvar3.get()==0:
             fruits.append("Apple")
             flag=1
             break
         elif checkvar2.get()==1 and checkvar1.get()==0 and checkvar3.get()==0:
             fruits.append("Pomegranate")
             flag=1
             break
         elif checkvar3.get()==1 and checkvar1.get()==0 and checkvar2.get()==0:
             fruits.append("Banana")
             flag=1
             break
         elif checkvar3.get()==1 and checkvar1.get()==1 and checkvar2.get()==0:
             fruits.append("Banana")
             fruits.append("Apple")
             flag=1
             break
         elif checkvar3.get()==1 and checkvar1.get()==0 and checkvar2.get()==1:
             fruits.append("Banana")
             fruits.append("Pomegranate")
             flag=1
             break
         elif checkvar3.get()==0 and checkvar1.get()==1 and checkvar2.get()==1:
             fruits.append("Apple")
             fruits.append("Pomegranate")
             flag=1
             break
         elif checkvar3.get()==1 and checkvar1.get()==1 and checkvar2.get()==1:
             fruits.append("Banana")
             fruits.append("Apple")
             fruits.append("Pomegranate")
             flag=1
             break
         else :
             fruits.append(" ")

         for fruit in fruits:
             txt += fruit+" "
         l.config(text=txt)
    
   
    
    
    
    
    
checkvar1= tk.IntVar()
checkvar2 = tk.IntVar()
checkvar3 = tk.IntVar()


c1 = tk.Checkbutton(parent, text ="Apple", variable = checkvar1, onvalue=1, offvalue=0,height = 5, width =20, bg="pink", activebackground="yellow", activeforeground="orange", command=print_choice)
c1.pack()
c2 = tk.Checkbutton(parent, text ="Pomegranate", variable = checkvar2, onvalue=1, offvalue=0,height = 5, width =20, bg="pink", command=print_choice)
c2.pack()
c3 = tk.Checkbutton(parent, text ="Banana", variable = checkvar3, onvalue=1, offvalue=0,height = 5, width =20, bg="pink", command=print_choice)
c3.pack()





parent.mainloop()

This is what I’m getting as the output:

enter image description here

Whenever I try to select a checkbox , Python stops responding. Is it possible that I’m running into an infinite loop somewhere ?

Advertisement

Answer

so here is an example of how You may want to do that:

from tkinter import Tk, Checkbutton, IntVar


root = Tk()

selected = []


class Choice:
    def __init__(self, text):
        self.text = text
        self.state = IntVar()

        self.checkbutton = Checkbutton(root, text=self.text, command=self.check,
                                       variable=self.state, onvalue=1, offvalue=0)
        self.checkbutton.pack()

    def check(self):
        state = self.state.get()
        if state == 1:
            selected.append(self.text)
        if state == 0:
            selected.remove(self.text)
        print(selected)


c1 = Choice('Apple')
c2 = Choice('Orange')
c3 = Choice('Pear')

root.mainloop()

and in this case You can add as many Checkboxes as You pretty much want and the only thing You gotta do in You code to hardcode is to just initiate Choice class like so var_name = Choice('checkbutton name') where var_name is any variable name You want, and checkbutton name is any name You want (just to clarify).

Advertisement