Python Forum
tkinter checkbutton if checked - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: tkinter checkbutton if checked (/thread-23851.html)



tkinter checkbutton if checked - MC2020 - Jan-20-2020

Hi - I am learning python and need help with this code. I have researched online, but haven't been able to find an answer for what I'm looking for.

My code is below. The 'open' function is what I need help with. I am looking to add the fooditem selected to the newwindow if the checkbox is checked. My code is below but isn't working. Can someone help point me in the right direction?

fooditems = ['pizza','breadsticks','wings','CocaCola','brownie']
fooditems2 = []
price = ['15', '5', '10', '3', '2']
quantity = ['1','2','3','4']

import tkinter as tk
from tkinter import *
from tkinter import ttk

menu = Tk()
menu.geometry('500x300')

combobox = ttk.Combobox(menu, values=quantity)

def Open():
    New_Window = Tk()
    New_Window.geometry('500x300')
    calculateButton = tk.Button(New_Window, text = 'calculate')
    calculateButton.place(x=250,y=250)
    for x in range(len(fooditems)):
        if Checkbutton.get(x) == 1:
            fooditems2.append[x]
    fooditems2.grid(column=0, row=x)
    combobox.grid(column=15, row=x)
    New_Window.mainloop()


for x in range(len(fooditems)):
    a = Checkbutton(menu, text="", variable=fooditems[x], onvalue = 1, offvalue = 0)
    b = Label(menu, text=fooditems[x])
    a.grid(column=0, row=x)
    b.grid(column=5, row=x)
    
    

confirmButton = tk.Button(menu, text = 'Confirm', command=Open)
confirmButton.place(x=250,y=250)

menu.mainloop()



RE: tkinter checkbutton if checked - woooee - Jan-20-2020

Don't use 2 instances of Tk() as the results are unpredictable. Use a Toplevel instead.
Quote:My code is below but isn't working
What does this mean?


RE: tkinter checkbutton if checked - joe_momma - Jan-21-2020

one major issue with your code is it's not a class. here's a checkbox
import tkinter as tk
 
 
class MainFrame(tk.Frame):
 
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.vars=[]
        self.checkbuttons = {'pizza': tk.IntVar(),
                             'salad': tk.IntVar(),
                             'coffee': tk.IntVar()}
         
        for key, value in self.checkbuttons.items():
            ctrl = tk.Checkbutton(self, text=key, variable=value)
            ctrl.pack()
            self.vars.append(value)
         
        self.button = tk.Button(self, text='Checkbutton values',
                                command=self.on_button)
        
        self.button.pack(pady=5,padx=10)
        self.pack()
 
    def on_button(self):
        res= []
        for key, val in self.checkbuttons.items():
           select= val.get()
           if select:
                print(f'you selected {key}')
        
 
 
if __name__ == '__main__':
    app = tk.Tk()
    main_frame = MainFrame()
    app.mainloop()