Using Combobox for item selection in Restaurant Management System
Using Combobox to select item and Entry for quantity in Restaurant Management script in Tkinter
Using Combobox for selection of items
We are using one Combobox and showing all items as options. One Entry widget is placed by the side of this combobox so user can enter the quantity of the selected item.
Data is taken from MySQL database product table based on the selection of the radio button ( product category) . This radio button is used to filter any particular category of product from the large number of products in the plus2_products table.
We kept the function to display all items inside one function show_item(). This function receives the category as parameter and filters the product items ( p_cat ) based on the query.
Add button is placed to trigger the function my_add() to insert the selected item to bill.
def show_items(cat): # Populating the Combobox
global my_menu,my_menu2
my_menu.clear() # remove all items
cb1.set('')
e1.delete(0,END)
r_set=my_conn.execute("SELECT * FROM plus2_products WHERE \
available=1 and p_cat="+str(cat))
for item in r_set:
my_menu.update({item[0]:item[1]})
my_menu2.update({item[1]:[item[0],item[1],item[3]]})
options=list(my_menu.values())
cb1.config(values=options)
b1=tk.Button(frame_m_left,text='Add',font=font2,
command=lambda:my_add())
b1.grid(row=0,column=2,padx=10)
show_items(1)
Adding selected item and quanity to bill
Here try and except code block is used for checking the valid inputs and then adding them to Treeview ( trv ) .
The object trv ( Treeview ) is declared outside at the main script so it is available to all functions.
The function my_sum() is called to calculate the total including the tax part.
After inserting the data to treeview the Combobox and entry widget is reset to accept fresh selection.
def my_add(): #adding item to bill
try:
p_id=my_menu2[sel.get()][0]
p_name=sel.get()
price=e1.get()
quantity=my_menu2[sel.get()][2]
except:
return None
if(int(quantity)>0 and len(p_name)>0 and p_id>0):
sub_total=round(float(price)*int(quantity),2)
trv.insert("",'end',text=p_id,values=[p_name,price,quantity,sub_total])
my_sum() # re-calculate the total
cb1.set('') # reset the combobox
e1.delete(0,END)# reset the entry widget
Calculating the total and bill
To calculate the total value, tax and to show the final price, we kept the code in a separate function my_sum(). This function is called when we add any item or remove some item ( next part ) or finalize the bill.
The for loop inside the function collects the total value of each node or child ( line ) and add them.
After calculating the tax, total value by looping , we used config() to change the text on Labels to display the values.
def my_sum(): # Calculate total and tax part
total=0
for line in trv.get_children(): # Loop through all items
total=total+float(trv.item(line)['values'][3])
tax=round(0.1*total,2) # change the tax rate here
final=round(total+tax,2) # final price
lr2.config(text=str(total)) # show it at Label
lr22.config(text=str(tax)) # show it at Label
lr32.config(text=str(final))# show it at Label
Reset the bill
This function is triggered by the Button saying Confirm ( Reset). This will prepare for next bill and we can include the script to insert the data to our table at this stage.
def my_reset():
for item in trv.get_children(): # loop all child items
trv.delete(item) # delete them
my_sum() # call the re-calculate and update the labels text