Feb-12-2021, 02:38 PM
(This post was last modified: Feb-12-2021, 02:38 PM by deanhystad.)
You need an IntVar(), not a StringVar(). You can use either IntVar() or StringVar(), but the variable type must match the value type.
This code uses a StringVar() and sets the RadioButton value to the food name. Since the button values are food names it makes more sens for food to be a dictionary with the name as the key.
This code uses a StringVar() and sets the RadioButton value to the food name. Since the button values are food names it makes more sens for food to be a dictionary with the name as the key.
root.geometry("1500x1300") 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} frame = tk.LabelFrame(root, relief=tk.SUNKEN, bd=1) frame.grid(row=0 ,column=0, sticky=tk.W) food_var = tk.StringVar() food_var.set(list(food)[0]) for r, name in enumerate(food): tk.Radiobutton(frame , text=name, variable=food_var, value=name, padx=10, pady=5) \ .grid(row=r+1, column=0 , sticky=tk.W) label_price = tk.LabelFrame(root, relief=tk.SUNKEN, bd=1, text="Price") label_price.grid(row=r+1, column=1, sticky=tk.W) def select_food(food_name): tk.Label(grocery_list, text=food_name).pack(side=tk.TOP) button = tk.Button(root, text="Basket", command=lambda: select_food(food_var.get())) button.grid(row=0, column=3) grocery_list = tk.Frame(root) grocery_list.grid(row=0, column=4) root.mainloop()