Feb-12-2021, 11:40 PM
(This post was last modified: Feb-12-2021, 11:41 PM by Nick_tkinter.)
I did it with radio buttons(but I have a little bit other problems)
Python3 tkinter radiobutton problem
|
Feb-12-2021, 11:40 PM
(This post was last modified: Feb-12-2021, 11:41 PM by Nick_tkinter.)
I did it with radio buttons(but I have a little bit other problems)
Feb-13-2021, 11:37 AM
Hello again.Now, I have the following problem.When I click an item(food) ,I put it to the basket.When I click another item ,I want to put it down the previous one,but the item places on top of the other.
Code: from tkinter import * from PIL import Image , ImageTk import tkinter.font as tkFont 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(1) # δεν δειχνει σε κανενα 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 position_in_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 = 0) r += 1 def select_food(food_name): global position_in_list print(position_in_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) if(food_quantity[ int(food_name_var.get()) - 1 ] == 0): food_quantity[ int(food_name_var.get()) - 1 ] += 1 label_print_food = Label(label_frame_food_info , padx = 3 , height = 2 , text = food_name + " (" + str(food_quantity[int(food_name_var.get() ) - 1]) + ")" , width = 17) label_print_food.grid(row = position_in_list , column = 0) #print(food_name_var.get()) button_remove_food = Button(label_frame_food_info , text = "-" , fg = "red" , command = lambda: remove_selected_food( int(food_name_var.get() ) - 1 , food_name) , width = 6) button_remove_food.grid(row = position_in_list , column = 1) button_add_food = Button(label_frame_food_info , text = "+" , fg = "green" , command = lambda: add_selected_food( int(food_name_var.get() ) - 1 , food_name) , width = 6) button_add_food.grid(row = position_in_list , column = 2) position_in_list += 1 def remove_selected_food(item_quantity , food_name): global position_in_list print(food_quantity[item_quantity]) if(food_quantity[item_quantity] > 1): food_quantity[item_quantity] -= 1 label_frame_food_info = LabelFrame(root , height = 600, width = 300 , relief = SUNKEN , bd = 2) # update shopping list label_print_food = Label(label_frame_food_info , padx = 3 , height = 2 , text = food_name + " (" + str(food_quantity[int(food_name_var.get() ) - 1]) + ")" , width = 17) label_frame_food_info.place(x = 1200 , y = 50) label_print_food.grid(row = position_in_list , column = 0) '''for i in range(0 , 16): print(food_quantity[i]) print("-------")''' else: label_frame_food_info = LabelFrame(root , height = 600, width = 300 , relief = SUNKEN , bd = 2) # update shopping list label_print_food = Label(label_frame_food_info , padx = 3 , height = 2 , text = food_name + " (" + str(food_quantity[int(food_name_var.get() ) - 1]) + ")" , width = 17) label_frame_food_info.place(x = 1200 , y = 50) label_print_food.grid(row = position_in_list , column = 0) label_print_food.grid_forget() def add_selected_food(item_quantity , food_name): global position_in_list food_quantity[item_quantity] += 1 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) # update listed food label_print_food = Label(label_frame_food_info , padx = 3 , height = 2 , text = food_name + " (" + str(food_quantity[int(food_name_var.get() ) - 1]) + ")" , width = 17) label_print_food.grid(row = position_in_list , column = 0) '''for i in range(0 , 16): print(food_quantity[i]) print("-------")''' lock_logo = Image.open("shopping_basket.jpg") lock_logo = lock_logo.resize( (60 , 60) , resample = 0) lock_image = ImageTk.PhotoImage(lock_logo) size_letter = tkFont.Font(family = "Helvetica" , size = 28 , weight = "bold") label_title = Label(root , text = "e-market" , padx = 486 , font = size_letter) label_title.grid(row = 0 , column = 4 , sticky = N) 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()
Feb-13-2021, 02:36 PM
Can anyone help me?
Feb-13-2021, 04:57 PM
Why do you make label_frame_food_info every time you select food? My example shows how to display multiple food items. Why don't you look at that example.
Feb-15-2021, 11:01 PM
(This post was last modified: Feb-15-2021, 11:04 PM by Nick_tkinter.)
Hello guys.Yesterday,I finally made the app,but when I add foods to basket or remove or add more quantity,my app blinks(run code to see what I mean).I suspect this is caused by the labels I create every time in add_selected_food() ,
remove_selected_food() and select_food() functions ,so this takes time(I suppose).I have some arrays to save food name ,food price , which food is in the basket and other things ,in order to show all the foods information that I put in the basket (every time). >> I want to tell me what is the cause of this blink in the app and how I can improve it.(I say again, I suspect the labels I create every time,but these labels have some parameters such as variable i that I use in for-loops and I can't find any other ideas to make it better.I don't want you to check the whole code,just a look of my labels.I'm a beginner in tkinter. Also,many times my app crashes and I can not do anything,so I terminate it via my terminal. Any ideas would be appreciated. Thank you very much. # go to email_send and put your email address and your password if you want to send your total price or skip this. from tkinter import * from PIL import Image , ImageTk import tkinter.font as tkFont import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText root = Tk() root.title("Super Market") root.geometry("1500x1400") 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(1) # δεν δειχνει σε κανενα 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 = 400 , relief = SUNKEN , bd = 2) label_frame_food_info.place(x = 1100 , y = 50) food_quantity = [0 for i in range(NUMBER_OF_FOODS)] listed_food = [0 for i in range(NUMBER_OF_FOODS)] # to know what I have inside my list,it helps me to control shopping_button with final price list_food_price = [0 for i in range(NUMBER_OF_FOODS)] print_listed_food = ["" for i in range(NUMBER_OF_FOODS)] print_help_price_list = [0 for i in range(NUMBER_OF_FOODS)] # which button I pressed, update corresponding index of this list ( price of item ) price_item_at_start = [0 for i in range(NUMBER_OF_FOODS)] r = 0 position_in_list = 0 global final_price final_price = 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) r += 1 def select_food(): global position_in_list , final_price label_frame_food_info = LabelFrame(root , height = 600, width = 400 , relief = SUNKEN , bd = 2) # update shopping list label_frame_food_info.place(x = 1100 , y = 50) if(listed_food[ int(food_name_var.get() ) - 1 ] == 0): price_item_at_start[ position_in_list ] = food[ int(food_name_var.get()) - 1 ][1] # price of food in list (at start) list_food_price[ position_in_list ] += food[ int(food_name_var.get()) - 1 ][1] # put price of food in the list final_price += food[ int(food_name_var.get()) - 1 ][1] # final price print_listed_food[position_in_list] = food[ int(food_name_var.get()) - 1 ][0] # put name of food in the list print_help_price_list[position_in_list] = food[ int(food_name_var.get()) - 1 ][1] position_in_list += 1 listed_food[ int(food_name_var.get() ) - 1 ] = 1 # selected food is in the list ( set to 1 ) if(food_quantity[ position_in_list - 1 ] == 0): food_quantity[ position_in_list - 1 ] += 1 if(list_food_price[ position_in_list - 1 ] == 0): # if price == 0 -> set quantity = 0 food_quantity[position_in_list - 1] = 0 label_final_price = Label(root , text = "Final price: " + str( round( final_price , 2) ) + " $ ") label_final_price.place(x = 1100 , y = 660) for i in range(0 , position_in_list): if(list_food_price[i] <= 0): list_food_price[i] = 0 label_price = Label(label_frame_food_info , text = str(round( list_food_price[ i ] , 2 ) ) + str(" $") , padx = 9 , width = 11) label_price.grid(row = i , column = 3) label_print_food = Label(label_frame_food_info , padx = 3 , anchor = W , height = 2 , text = str(print_listed_food[i]) + " (" + str(food_quantity[i]) + ")" , width = 16) label_print_food.grid(row = i , column = 0) button_remove_food = Button(label_frame_food_info , text = "-" , fg = "red" , command = lambda i=i: remove_selected_food( int(food_name_var.get() ) - 1 , i) , width = 6) button_remove_food.grid(row = i , column = 1) button_add_food = Button(label_frame_food_info , text = "+" , fg = "green" , command = lambda i=i: add_selected_food( int(food_name_var.get() ) - 1 , i) , width = 6) button_add_food.grid(row = i , column = 2) def remove_selected_food(item_quantity , which_button): global position_in_list , final_price , label_frame_food_info list_food_price[ which_button ] -= print_help_price_list[which_button] # pop price of food from the list if(food_quantity[ which_button ] != 0): final_price -= print_help_price_list[which_button] if(final_price <= 0): final_price = 0 if(food_quantity[which_button] >= 1): food_quantity[which_button] -= 1 label_frame_food_info = LabelFrame(root , height = 600, width = 400 , relief = SUNKEN , bd = 2) # update shopping list label_frame_food_info.place(x = 1100 , y = 50) else: # quantity of food -> 0 listed_food[which_button] = 0 label_frame_food_info = LabelFrame(root , height = 600, width = 400 , relief = SUNKEN , bd = 2) # update shopping list label_frame_food_info.place(x = 1100 , y = 50) label_frame_food_info.grid_forget() label_final_price = Label(root , text = "Final price: " + str( round( final_price , 2) ) + " $ ") label_final_price.place(x = 1100 , y = 660) for i in range(0 , position_in_list): if(list_food_price[i] <= 0): list_food_price[i] = 0 label_price = Label(label_frame_food_info , text = str(round( list_food_price[ i ] , 2 ) ) + str(" $") , padx = 9 , width = 11) label_price.grid(row = i , column = 3) label_print_food = Label(label_frame_food_info , padx = 3 , anchor = W , height = 2 , text = str(print_listed_food[i]) + " (" + str(food_quantity[i]) + ")" , width = 16) label_print_food.grid(row = i , column = 0) button_remove_food = Button(label_frame_food_info , text = "-" , fg = "red" , command = lambda i=i: remove_selected_food( item_quantity , i) , width = 6) button_remove_food.grid(row = i , column = 1) button_add_food = Button(label_frame_food_info , text = "+" , fg = "green" , command = lambda i=i: add_selected_food( item_quantity , i) , width = 6) button_add_food.grid(row = i , column = 2) def add_selected_food(item_quantity , which_button): global position_in_list , final_price list_food_price[ which_button ] += print_help_price_list[which_button] # put price of food in the list #final_price += food[ item_quantity ][1] final_price += print_help_price_list[which_button] food_quantity[which_button] += 1 label_frame_food_info = LabelFrame(root , height = 600, width = 400 , relief = SUNKEN , bd = 2) # update shopping list label_frame_food_info.place(x = 1100 , y = 50) label_final_price = Label(root , text = "Final price: " + str( round( final_price , 2) ) + " $ ") label_final_price.place(x = 1100 , y = 660) for i in range(0 , position_in_list): label_price = Label(label_frame_food_info , text = str(round( list_food_price[ i ] , 2 ) ) + str(" $") , padx = 9 , width = 11) label_price.grid(row = i , column = 3) label_print_food = Label(label_frame_food_info , padx = 3 , anchor = W , height = 2 , text = str(print_listed_food[i]) + " (" + str(food_quantity[i]) + ")" , width = 16) label_print_food.grid(row = i , column = 0) button_remove_food = Button(label_frame_food_info , text = "-" , fg = "red" , command = lambda i=i: remove_selected_food( item_quantity , i) , width = 6) button_remove_food.grid(row = i , column = 1) button_add_food = Button(label_frame_food_info , text = "+" , fg = "green" , command = lambda i=i: add_selected_food( item_quantity , i) , width = 6) button_add_food.grid(row = i , column = 2) input_email = Entry(root , width = 36) input_email.place(x = 1100 , y = 730) size_letter = tkFont.Font(family = "Helvetica" , size = 28 , weight = "bold") label_title = Label(root , text = "e-market" , padx = 486 , font = size_letter) label_title.grid(row = 0 , column = 4 , sticky = N) button = Button(root , text = "Basket , command = select_food) button.grid(row = 0 , column = 3 , sticky = S) label_info = Label(root , text = "Shopping basket") label_info.place(x = 1250 , y = 20) label_final_price = Label(root , text = "Final price: " + str( round( final_price , 2) ) + " $ ") label_final_price.place(x = 1100 , y = 660) money_customer = Entry(root , width = 4) money_customer.place(x = 1000 , y = 660) Label(root , text = "Customer's money:").place(x = 850 , y = 660) Label(root , text = "$").place(x = 1050 , y = 660) def receipt(): spaces = 19 message = StringVar() print("Food" + " " * (spaces - 4) + "Quantity" + " " * (spaces - 4) + "Food price" + " " * (spaces - 4) + "Total price of item\n") for i in range(0 , position_in_list): if(food_quantity[i] != 0): print(str(print_listed_food[i]) + " " * (spaces - len(print_listed_food[i]) ) + str(food_quantity[i]) , end = "" , flush = True) print(" " * 22 + str( round(price_item_at_start[i] , 3)) + str(" $") , end = "" , flush = True) if(len(str(price_item_at_start[i])) == 3): space = 1 else: space = 0 print(" " * (19 + space) + str( round(list_food_price[i] , 3)) + str(" $")) print("\nTotal price has been sent to your email address.\nThank you for your preference!") def email_send(SUBJECT, message , TO): FROM = "your email" #TO = "send email to..." MESSAGE = MIMEMultipart('alternative') MESSAGE['subject'] = SUBJECT MESSAGE['To'] = TO MESSAGE['From'] = FROM HTML_BODY = MIMEText(message, 'html') MESSAGE.attach(HTML_BODY) server = smtplib.SMTP("smtp.gmail.com:587") password = "your password" server.starttls() server.login(FROM,password) server.sendmail(FROM , TO , MESSAGE.as_string() ) server.quit() return TO def payment(): email_address = StringVar() email_address = input_email.get() customer_money = money_customer.get() if(email_address != "" and customer_money != "" and final_price != 0): if(int(customer_money) < final_price): not_enough_money = Label(root , text = "You have not enough money to complete your order.") not_enough_money.place(x = 1100 , y = 780) return if(final_price != 0): email_send("e-market" , "Total price: " + str(round( final_price , 2 )) + str(" $") , email_address ) receipt() root.quit() payment_button = Button(root , text = "Payment" , height = 1 , bg = "green" , command = payment) payment_button.place(x = 1415 , y = 730) label_text_email = Label(root , text = "Enter your email to send you the receipt.") label_text_email.place(x = 1100 , y = 700) root.mainloop()if I remove label_frame_food_info variable ,then the frame ,is not completed if you can see. |
|