Python Forum
Python3 tkinter radiobutton problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3 tkinter radiobutton problem
#6
(Feb-12-2021, 08:21 PM)deanhystad Wrote: A radio button group is not a good choice for this application. You could just use buttons.
import tkinter as tk

root = tk.Tk()
root.title("Super Market")
 
food = {
    "Rice": 0.99,
    "Spaghetti": 0.95,
    "Ice cream": 3.44,
    "Pizza": 7.50,
    "Cheese": 2.10, 
    "Cheddar": 0.45, 
    "Cherry": 0.88, 
    "Banana": 0.40, 
    "Apple": 0.67, 
    "Meat": 7.28, 
    "Fish": 9.72, 
    "Orange": 0.62, 
    "Orange juice": 2.10, 
    "Chocolate": 0.99, 
    "Bread": 0.55}

inventory = tk.Frame(root)
inventory.grid(row=0, column=0)

shopping_cart = {}

def update_cart():
    print(shopping_cart)
    for item in cart.winfo_children():
        item.grid_forget()
        item.destroy()

    for r, item in enumerate(shopping_cart):
        text = f'{shopping_cart[item]} x {item}'
        tk.Label(cart, text=text, width=20) \
            .grid(row=r, column=0)
        tk.Button(cart, text=' + ', command=lambda n=item: add_item(n)) \
            .grid(row=r, column=1)
        tk.Button(cart, text=' - ', command=lambda n=item: remove_item(n)) \
            .grid(row=r, column=2)

def add_item(item):
    shopping_cart[item] = shopping_cart.get(item, 0) + 1
    update_cart()

def remove_item(item):
    if item in shopping_cart:
        shopping_cart[item] -= 1
        if shopping_cart[item] <= 0:
            shopping_cart.pop(item)
        update_cart()

for item in food:
    button = tk.Button(inventory, text=item, width=20, command=lambda n=item: add_item(n))
    button.pack(side=tk.TOP)

cart = tk.Frame(root)
cart.grid(row=0, column=1)
  
root.mainloop()
Is there any way to do it with radio buttons,because I want to do it that way.Thanks
Reply


Messages In This Thread
RE: Python3 tkinter radiobutton problem - by Nick_tkinter - Feb-12-2021, 08:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to dynamically change radiobutton text kenwatts275 2 3,377 Mar-05-2021, 02:25 AM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 3,585 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  [Tkinter] RadioButton Maryan 2 2,189 Oct-23-2020, 09:36 PM
Last Post: Maryan
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 4,476 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  [Tkinter] ClockIn/Out tkinter problem Maryan 2 2,230 Oct-12-2020, 03:42 AM
Last Post: joe_momma
  tkinter| listbox.insert problem Maryan 3 3,544 Sep-29-2020, 05:34 PM
Last Post: Yoriz
  Tkinter problem DPaul 6 4,177 May-28-2020, 03:40 PM
Last Post: DPaul
  [Tkinter] Tkinter wouldn't work with python3.8.3 shay_xs 2 2,636 May-24-2020, 11:48 PM
Last Post: Larz60+
  [Tkinter] How to create radiobutton in numpy gui python? luthfidali 2 2,624 May-23-2020, 10:35 AM
Last Post: timo
  [Tkinter] Tkinter - I have problem after import varaible or function from aGUI to script johnjh 2 2,611 Apr-17-2020, 08:12 PM
Last Post: johnjh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020