How to get values from Menubutton the element as a checkbutton

Question:

I am attempting to create a checkbox list using Tkinter in python.
I added all components and button
but i dint know how to retrieve the values from the components. I want to retrieve the values such as: Sun, Mon… that will be checked and save it in the list after enter the button.
the code below reflects what I am attempting to describe:

from tkinter import *
top = Tk()
mb=  Menubutton ( top, text="CheckComboBox", relief=RAISED )
mb.grid()
mb.menu  =  Menu ( mb, tearoff = 0 )
mb["menu"]  =  mb.menu
Item1 = StringVar()
Item2 = StringVar()
Item3 = StringVar()
Item4 = StringVar()
Item5 = StringVar()

lst = []

def get_checked(inputuser):
    for item in [Item1, Item2, Item3, Item4, Item5]:
        if item.get()== '1':
            lst.append(inputuser)
    # do something with lst

mb.menu.add_checkbutton ( label="Sun", variable=Item1, command=get_checked("Sun"))
mb.menu.add_checkbutton ( label="Mon", variable=Item2, command=get_checked("Mon"))
mb.menu.add_checkbutton ( label="Teu", variable=Item3, command=get_checked("Teu"))
mb.menu.add_checkbutton ( label="Wed", variable=Item4, command=get_checked("Wed"))
mb.menu.add_checkbutton ( label="Fri", variable=Item5, command=get_checked("Fri"))


def getresposees():   
    print(lst)
    
Button22= Button(top, text='Ok',  fg= 'white', command=getresposees , bg='gray', width=15) 
Button22.pack()

top.mainloop() 
Asked By: AB1506

||

Answers:

You can add a command parameter to the add_checkbutton calls that points to a callback function that gets the state of each of the StringVar() items. The callback will then be executed each time one of the menu options is selected or deselected.

from tkinter import *
top = Tk()
mb=  Menubutton ( top, text="CheckComboBox", relief=RAISED )
mb.pack()
mb.menu  =  Menu ( mb, tearoff = 0 )
mb["menu"]  =  mb.menu
Item1 = StringVar()
Item2 = StringVar()
Item3 = StringVar()
Item4 = StringVar()
Item5 = StringVar()

def get_checked():
    lst = []
    for item in [Item1, Item2, Item3, Item4, Item5]:
        lst.append(item.get())
    print(lst)
    # do something with lst

mb.menu.add_checkbutton ( label="Sun", variable=Item1, command=get_checked)
mb.menu.add_checkbutton ( label="Mon", variable=Item2, command=get_checked)
mb.menu.add_checkbutton ( label="Teu", variable=Item3, command=get_checked)
mb.menu.add_checkbutton ( label="Wed", variable=Item4, command=get_checked)
mb.menu.add_checkbutton ( label="Fri", variable=Item5, command=get_checked)

    
Button22= Button(top, text='Ok',  fg= 'white', command=get_checked , bg='gray', width=15) 
Button22.pack()

top.mainloop()

Below is and example that makes the callback work with the button only and only print the menu items that are selected.

from tkinter import *
top = Tk()
mb=  Menubutton ( top, text="CheckComboBox", relief=RAISED )
mb.pack()
mb.menu  =  Menu ( mb, tearoff = 0 )
mb["menu"]  =  mb.menu
Item1 = StringVar()
Item2 = StringVar()
Item3 = StringVar()
Item4 = StringVar()
Item5 = StringVar()

def get_checked():
    values = ["Sun", "Mon", "Teu", "Wed", "Fri"]
    lst = []
    for i, item in enumerate([Item1, Item2, Item3, Item4, Item5]):
        if item.get() == "1":
            lst.append(values[i])
    print(lst)
    # do something with lst

mb.menu.add_checkbutton(label="Sun", variable=Item1)
mb.menu.add_checkbutton(label="Mon", variable=Item2)
mb.menu.add_checkbutton(label="Teu", variable=Item3)
mb.menu.add_checkbutton(label="Wed", variable=Item4)
mb.menu.add_checkbutton(label="Fri", variable=Item5)

    
Button22= Button(top, text='Ok',  fg= 'white', command=get_checked , bg='gray', width=15) 
Button22.pack()

top.mainloop()
Answered By: Alexander
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.