Feb-12-2021, 05:03 PM
(This post was last modified: Feb-12-2021, 05:03 PM by Nick_tkinter.)
Hello again. I have some problems with my app. I want it to look something like this
Also ,I have a frame as you can see on the right side.So, when I click an item, I want to add it to the basket(Done),but the problem is that the item doesn't place down the previous one.( See here )
I also want to put "remove" buttons right from added items to remove items from list ,but this doesn't seem to work with my code below.
I put these "remove" buttons ,cause when user add an item many times, a counter increases ,so when he uses this button this counter discreses.
Code:
Also ,I have a frame as you can see on the right side.So, when I click an item, I want to add it to the basket(Done),but the problem is that the item doesn't place down the previous one.( See here )
I also want to put "remove" buttons right from added items to remove items from list ,but this doesn't seem to work with my code below.
I put these "remove" buttons ,cause when user add an item many times, a counter increases ,so when he uses this button this counter discreses.
Code:
from tkinter import * from PIL import Image , ImageTk root = Tk() root.title("Super Market") root.geometry("1500x1300") food = [ ("Rice" , 0.99) , # value for radio_button -> 1 ("Spaghetti" , 0.95) , # value for radio_button -> 2 ("Ice cream" , 3.44) , # value for radio_button -> 3 ("Pizza" , 7.50) , # value for radio_button -> 4 ("Cheese" , 2.10) , # value for radio_button -> 5 ("Cheddar" , 0.45) , # value for radio_button -> 6 ("Cherry" , 0.88) , # value for radio_button -> 7 ("Banana" , 0.40) , # value for radio_button -> 8 ("Apple" , 0.67) , # value for radio_button -> 9 ("Meat" , 7.28) , # value for radio_button -> 10 ("Fish" , 9.72) , # value for radio_button -> 11 ("Orange" , 0.62) , # value for radio_button -> 12 ("Orange juice" , 2.10) , # value for radio_button -> 13 ("Chocolate" , 0.99) , # value for radio_button -> 14 ("Bread" , 0.55) , # value for radio_button -> 15 ("Milk" , 1.00) # value for radio_button -> 16 ] NUMBER_OF_FOODS = 16 food_name_var = StringVar() food_name_var.set(-200) # δεν δειχνει σε κανενα radio button αρχικα , αν εβαζα 1 θα εδειχνε στο 1ο προιον κοκ... frame = LabelFrame(root , relief = SUNKEN , bd = 1) frame.grid(row = 0 , column = 0 , sticky = W) label_frame_food_info = LabelFrame(root , height = 600, width = 300 , relief = SUNKEN , bd = 2) label_frame_food_info.place(x = 1200 , y = 50) food_quantity = [0 for i in range(NUMBER_OF_FOODS)] r = 0 y_lim_list = 0 for food_name , food_price in food: Radiobutton(frame , text = food_name , variable = food_name_var , value = r+1 , padx = 10 , pady = 5).grid(row = r , column = 0 , sticky = W) label_price = LabelFrame(root , relief = SUNKEN , bd = 1 , text = "Price") label_price.grid(row = r , column = 1 , sticky = W) r += 1 def select_food(food_name): global y_lim_list label_frame_food_info = LabelFrame(root , height = 600, width = 300 , relief = SUNKEN , bd = 2) # update shopping list label_frame_food_info.place(x = 1200 , y = 50) label_print_food = Label(label_frame_food_info , text = food_name + " (" + str(food_quantity[int(food_name_var.get() ) - 1] + 1) + ")") food_quantity[int(food_name_var.get() ) - 1] += 1 if(food_quantity[int(food_name_var.get() ) - 1] >= 1): button_remove_food = Button(label_frame_food_info , text = "Remove" , command = lambda: remove_selected_food(food[ int(food_name_var.get() ) - 1][0])) button_remove_food.grid(row = y_lim_list , column = 1) label_print_food.grid(row = y_lim_list , column = 0) y_lim_list += 1 def remove_selected_food(item): return lock_logo = Image.open("shopping_basket.jpg") lock_logo = lock_logo.resize( (60 , 60) , resample = 0) lock_image = ImageTk.PhotoImage(lock_logo) button = Button(root , image = lock_image , command = lambda: select_food(food[ int(food_name_var.get() ) - 1][0])) button.grid(row = 0 , column = 3 , sticky = S) label_info = Label(root , text = "Shopping basket") label_info.place(x = 1294 , y = 20) root.mainloop()