I'm currently working on a retail management system project using Tkinter as the GUI framework. The system's interface is designed to use an image as the background, and buttons are placed over the image in specified areas.
I've managed to display the buttons and entry boxes, but I'm having trouble getting the combobox to display on the interface.
This is the output of my main program:Click link
My code:
#BBCVZJS0
from tkinter import *
from tkinter.ttk import Combobox
import tkinter as tk
import sqlite3
import tkinter.messagebox as messagebox
def main_window():
# create a new window
invoice = Toplevel()
# load the image file
billimage1 = PhotoImage(file="C:/Users/hasee/OneDrive/Documents/CODE/billimage.PNG")
# create a label widget and set it as the background of the window
label2 = Label(invoice, image = billimage1)
label2.pack()
def search_bill():
# Connect to the database
conn = sqlite3.connect('store.db')
c = conn.cursor()
# Get the value of the bill entry box
bill_no = bill_entry.get()
# Execute a query to fetch the bill number from the database
c.execute("SELECT bill_no FROM bill WHERE bill_no = ?", (bill_no,))
result = c.fetchone()
# Check if the result is not None
if result is not None:
# Execute a query to fetch the product information from the inventory table
c.execute("SELECT product_name, price, quantity FROM raw_inventory WHERE product_name = 'Big G Cereals Cinnamon Toast Crunch' AND price = 12 AND quantity = 10")
product_info = c.fetchone()
# Execute a query to fetch the customer information from the bill table
c.execute("SELECT customer_name, customer_no FROM bill WHERE customer_name = 'Adam Fawn' AND customer_no = '02086132851'")
customer_info = c.fetchone()
# Close the database connection
conn.close()
# Check if both the product and customer information are not None
if product_info is not None and customer_info is not None:
# Create labels to display the product and customer information
product_label = tk.Label(invoice, text=f"{product_info[0]}{' ' * 45}{product_info[1]}{' ' * 83}£{product_info[2]}", font=("Arial", 13), bg="white")
customer_label = tk.Label(invoice, text=f"{customer_info[0]}{' ' * 109}Phone Number: {customer_info[1]}", font=("Arial", 13), bg="white")
bill_label = tk.Label(invoice, text=f"{bill_no}", font=("Arial", 13), bg="white")
total_label = tk.Label(invoice, text=f"Total: £{product_info[2]}", font=("Arial", 15), bg="white", fg = "red")
# Place the labels on the GUI
product_label.place(relx=0.44, rely=0.6)
customer_label.place(relx=0.52, rely=0.458)
bill_label.place(relx=0.5, rely=0.483)
total_label.place(relx = 0.44, rely =0.65)
else:
# Close the database connection
conn.close()
# Show a message box to indicate that the search was unsuccessful
messagebox.showwarning("Search Result", "Bill number does not exist in the database")
def generate_bill():
conn = sqlite3.connect('store.db')
c = conn.cursor()
# Get the values from the entry boxes
cust_name2 = cust_name.get()
contact_num2 = contact_num.get()
c.execute("SELECT customer_name FROM bill WHERE customer_name = 'Adam Fawn'")
customer_check = c.fetchone()
c.execute("SELECT customer_no FROM bill WHERE customer_no = '02086132851'")
phone_check = c.fetchone()
# Check if the customer name and contact number are not empty
if cust_name2 and contact_num2:
# Check if the customer name and contact number match the values in the database
if cust_name2 == customer_check[0] and contact_num2 == phone_check[0]:
# Get the bill from the search_bill function
conn = sqlite3.connect('store.db')
c = conn.cursor()
bill_no = bill_entry.get()
# Execute a query to fetch the bill number from the database
c.execute("SELECT bill_no FROM bill WHERE bill_no = ?", (bill_no,))
result = c.fetchone()
c.execute("SELECT product_name, price, quantity FROM raw_inventory WHERE product_name = 'Big G Cereals Cinnamon Toast Crunch' AND price = 12 AND quantity = 10")
product_info = c.fetchone()
# Execute a query to fetch the customer information from the bill table
c.execute("SELECT customer_name, customer_no FROM bill WHERE customer_name = 'Adam Fawn' AND customer_no = '02086132851'")
customer_info = c.fetchone()
# Close the database connection
conn.close()
product_label = tk.Label(invoice, text=f"{product_info[0]}{' ' * 45}{product_info[1]}{' ' * 83}£{product_info[2]}", font=("Arial", 13), bg="white")
customer_label = tk.Label(invoice, text=f"{customer_info[0]}{' ' * 109}Phone Number: {customer_info[1]}", font=("Arial", 13), bg="white")
bill_label = tk.Label(invoice, text=f"{bill_no}", font=("Arial", 13), bg="white")
total_label = tk.Label(invoice, text=f"Total: £{product_info[2]}", font=("Arial", 15), bg="white", fg = "red")
# Place the labels on the GUI
product_label.place(relx=0.44, rely=0.6)
customer_label.place(relx=0.52, rely=0.458)
bill_label.place(relx=0.5, rely=0.483)
total_label.place(relx = 0.44, rely =0.65)
# Display a message to the user that the bill is generated
messagebox.showinfo("Bill Generated", "The bill has been generated.")
else:
# Display an error message to the user that the customer name and contact number do not match
messagebox.showerror("Error", "The customer name or contact number is incorrect.")
else:
# Display an error message to the user that they need to enter values
messagebox.showerror("Error", "Please enter a customer name and contact number.")
def clear_bill():
invoice.destroy()
main_window()
def dropdown():
# create a Combobox widget with items from the 'items' list
combo = Combobox(invoice, values=items)
# set the default value to the first item in the list
combo.current(0)
items = ['Item 1', 'Item 2', 'Item 3']
combo.pack()
# set up the variables
logout_img = tk.PhotoImage(file="C:/Users/hasee/OneDrive/Documents/CODE/logout.PNG")
logout_button = tk.Button(invoice, image=logout_img, command="", borderwidth=0, bg='white')
logout_button.place(relx=0.025, rely=0.099)
search_img = tk.PhotoImage(file="C:/Users/hasee/OneDrive/Documents/CODE/search.PNG")
search_button = tk.Button(invoice, image=search_img, command = search_bill, borderwidth=0, bg='white')
search_button.place(relx=0.3, rely=0.22)
cart_img = tk.PhotoImage(file="C:/Users/hasee/OneDrive/Documents/CODE/cart.PNG")
cart_button = tk.Button(invoice, image=cart_img, command="", borderwidth=0, bg='white')
cart_button.place(relx=0.085, rely=0.725)
remove_img = tk.PhotoImage(file="C:/Users/hasee/OneDrive/Documents/CODE/remove.PNG")
remove_button = tk.Button(invoice, image=remove_img, command="", borderwidth=0, bg='white')
remove_button.place(relx=0.176, rely=0.722)
clear_img = tk.PhotoImage(file="C:/Users/hasee/OneDrive/Documents/CODE/clear.PNG")
clear_button = tk.Button(invoice, image=clear_img, command=clear_bill, borderwidth=0, bg='white')
clear_button.place(relx=0.265, rely=0.72)
clear_img2 = tk.PhotoImage(file="C:/Users/hasee/OneDrive/Documents/CODE/clear.PNG")
clear_button2 = tk.Button(invoice, image=clear_img2, command="", borderwidth=0, bg='white')
clear_button2.place(relx=0.22, rely=0.871)
total_img = tk.PhotoImage(file="C:/Users/hasee/OneDrive/Documents/CODE/total.PNG")
total_button = tk.Button(invoice, image=total_img, command="", borderwidth=0, bg='white')
total_button.place(relx=0.04, rely=0.878)
generate_img = tk.PhotoImage(file="C:/Users/hasee/OneDrive/Documents/CODE/generate.PNG")
generate_button = tk.Button(invoice, image=generate_img, command=generate_bill, borderwidth=0, bg='white')
generate_button.place(relx=0.13, rely=0.875)
exit_img = tk.PhotoImage(file="C:/Users/hasee/OneDrive/Documents/CODE/exit.PNG")
exit_button = tk.Button(invoice, image=exit_img, command="", borderwidth=0, bg='white')
exit_button.place(relx=0.31, rely=0.8715)
bill_entry = tk.Entry(invoice, width=20, font=("Arial", 20), bd = 0) # create an textbox for the phone number
bill_entry.place(relx=0.105, rely=0.235)
cust_name = tk.Entry(invoice, width=20, font=("Arial", 20), bd = 0) # create an textbox for the phone number
cust_name.place(relx=0.515, rely=0.235)
contact_num = tk.Entry(invoice, width=20, font=("Arial", 20), bd = 0) # create an textbox for the phone number
contact_num.place(relx=0.795, rely=0.235)
invoice.mainloop()
main_window()
The code for writing up the combobox:
def dropdown():
# create a Combobox widget with items from the 'items' list
combo = Combobox(invoice, values=items)
# set the default value to the first item in the list
combo.current(0)
items = ['Item 1', 'Item 2', 'Item 3']
combo.pack()
At the moment, my dropdown() function is quite simple in terms of the items on the list that I'm working with. However, I'm facing an issue where the combobox is not being displayed on the GUI. I've tried using the place() method to see if it can fix the problem, but unfortunately it didn't.
I've also attempted to run the combobox function on its own, and it was successfully displayed. However, when I integrate it with the rest of my code, the combobox is not showing up. I'm starting to suspect that there might be an issue with the image that I'm using as my background. Do you have any ideas or suggestions on how I can resolve this issue?
You need to use a StringVar or IntVar or even a FloatVar to get the values from the Combobox back uesing callback functio. The Varaible should be traced with "varaible".trace('w', "callback_function_name"
Try this:
combo_output_var = StringVar() # output variabel for the combobox -> callback function with trace should be used!
combobox_list = Combobox(invoice, textvariable= combo_output_var) # Create the combobox
combobox_list['values'] = items # Give the Combobox all the selectable values
combobox_list['state'] = 'readonly # The state from the combobox could be set... ('readonly' is mostly used)
combobox_list.current(index_items) # Set the default visible value
combobox.pack() # Pack the combobox