Need a bit of help with my code, only been working with python for just over a week at this point.
I am trying to get a list of discount codes from my discount table, and put it into the combobox for the drop down list.
As of this moment it’s pulling the information all together.
If I run the code in it’s own python there is no issues. I’m sure I shouldn’t be setting it up as a def.
Here is the code I currently have setup for it.
def listdiscounts():
is the code to pull the sql data.
discount_combobox = ttk.Combobox(root, values=listdiscounts)
is the code to put it into the Combobox
Any input is welcome or a better way of going about it is more then welcome as well.
from tkinter import *
from PIL import Image,ImageTk
import tkinter as tk
from tkinter import ttk
import mysql.connector
import connection
import tkinter.messagebox
root = Tk()
root.title('Customers')
root.geometry("800x700")
# Connection to DB... connection.py
cur, conn = connection.get_connection()
cur.execute("CREATE TABLE IF NOT EXISTS customers (f_name VARCHAR(255), \
l_name VARCHAR(255), \
address_1 VARCHAR(255),\
address_2 VARCHAR(255),\
city VARCHAR(50),\
providence VARCHAR(2),\
postal VARCHAR(6), \
phone VARCHAR(255),\
email VARCHAR(255),\
payment_method VARCHAR(50),\
discount_code VARCHAR(255), \
user_id INT AUTO_INCREMENT PRIMARY KEY)")
# Pull list of discount codes
def listdiscounts():
cur.execute("SELECT * FROM discount")
discount = cur.fetchall()
for discounts in discount:
code_name = discounts[0]
code_id = discounts[2]
discount_combobox.insert(tk.END, f"{code_name}: {code_id}")
discount_combobox.pack()
# Clear fields command
def clear_fields():
f_name_box.delete(0, END)
l_name_box.delete(0, END)
address1_box.delete(0, END)
address2_box.delete(0, END)
city_box.delete(0, END)
providence_box.delete(0, END)
postal_box.delete(0, END)
phone_box.delete(0, END)
email_box.delete(0, END)
payment_method_box.delete(0, END)
discount_combobox.delete(0, END)
# Submit Customer To Database
def add_customer():
sql_command = "INSERT INTO customers (f_name, l_name, postal, email, address_1, address_2, city, providence, phone, payment_method, discount_code) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
values = (f_name_box.get(), l_name_box.get(), postal_box.get(), email_box.get(), address1_box.get(), address2_box.get(), city_box.get(), providence_box.get(), phone_box.get(), payment_method_box.get(), discount_combobox.get())
cur.execute(sql_command, values)
# Commit the changes to the database
conn.commit()
# Clear the fields
clear_fields()
#Create Main Form To Enter Customer Data
f_name_label = Label(root, text="First Name:").grid(row=1, column=0, sticky=W, padx=10)
l_name_label = Label(root, text="Last Name:").grid(row=2, column=0, sticky=W, padx=10)
address1_label = Label(root, text="Address 1:").grid(row=3, column=0, sticky=W, padx=10)
address2_label = Label(root, text="Address 2:").grid(row=4, column=0, sticky=W, padx=10)
city_label = Label(root, text="City:").grid(row=5, column=0, sticky=W, padx=10)
providence_label = Label(root, text="Providence:").grid(row=6, column=0, sticky=W, padx=10)
postal_label = Label(root, text="Postal code:").grid(row=7, column=0, sticky=W, padx=10)
phone_label = Label(root, text="Phone Number:").grid(row=8, column=0, sticky=W, padx=10)
email_label = Label(root, text="Email Address:").grid(row=9, column=0, sticky=W, padx=10)
payment_method_label = Label(root, text="Payment Method:").grid(row=10, column=0, sticky=W, padx=10)
discount_code_label = Label(root, text="Discount Code:").grid(row=11, column=0, sticky=W, padx=10)
# Create Entry Boxes
f_name_box = Entry(root)
f_name_box.grid(row=1, column=1)
l_name_box = Entry(root)
l_name_box.grid(row=2, column=1, pady=5)
address1_box = Entry(root)
address1_box.grid(row=3, column=1, pady=5)
address2_box = Entry(root)
address2_box.grid(row=4, column=1, pady=5)
city_box = Entry(root)
city_box.grid(row=5, column=1, pady=5)
providence_box = Entry(root)
providence_box.grid(row=6, column=1, pady=5)
postal_box = Entry(root)
postal_box.grid(row=7, column=1, pady=5)
phone_box = Entry(root)
phone_box.grid(row=8, column=1, pady=5)
email_box = Entry(root)
email_box.grid(row=9, column=1, pady=5)
payment_method_box = Entry(root)
payment_method_box.grid(row=10, column=1, pady=5)
#discount_code_box = Entry(root)
#discount_code_box.grid(row=11, column=1, pady=5)
discount_combobox = ttk.Combobox(root, values=listdiscounts)
discount_combobox.grid(row=11, column=1, pady=5)
# Create Buttons
add_customer_button = Button(root, text="Add Customer To Database", command=add_customer)
add_customer_button.grid(row=14, column=0, padx=10, pady=10)
clear_fields_button = Button(root, text="Clear Fields", command=clear_fields)
clear_fields_button.grid(row=14, column=1)
root.mainloop()