Python Forum
Python3 tkinter radiobutton problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3 tkinter radiobutton problem
#11
I did it with radio buttons(but I have a little bit other problems)
Reply
#12
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()
Reply
#13
Can anyone help me?
Reply
#14
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.
Reply
#15
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to dynamically change radiobutton text kenwatts275 2 3,283 Mar-05-2021, 02:25 AM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 3,486 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  [Tkinter] RadioButton Maryan 2 2,128 Oct-23-2020, 09:36 PM
Last Post: Maryan
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 4,359 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  [Tkinter] ClockIn/Out tkinter problem Maryan 2 2,165 Oct-12-2020, 03:42 AM
Last Post: joe_momma
  tkinter| listbox.insert problem Maryan 3 3,438 Sep-29-2020, 05:34 PM
Last Post: Yoriz
  Tkinter problem DPaul 6 4,048 May-28-2020, 03:40 PM
Last Post: DPaul
  [Tkinter] Tkinter wouldn't work with python3.8.3 shay_xs 2 2,588 May-24-2020, 11:48 PM
Last Post: Larz60+
  [Tkinter] How to create radiobutton in numpy gui python? luthfidali 2 2,570 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,522 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