Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- import sqlite3
- with open("login.txt", "w") as f:
- f.write("john:password123\n")
- f.write("jane:letmein\n")
- f.write("admin:adminpassword\n")
- class MenuSystem:
- def __init__(self, master):
- self.master = master
- master.title("Menu System")
- master.geometry("700x700") # set window size
- # Create buttons
- self.button1 = tk.Button(master, text="Login", command=self.login, width=20, height=5)
- self.button2 = tk.Button(master, text="Start Checkout", command=self.option1, width=20, height=5, state="disabled")
- self.button3 = tk.Button(master, text="Settings", command=self.open_new_window, width=20, height=5, state="disabled")
- # Pack buttons
- self.button1.pack(pady=20)
- self.button2.pack(pady=20)
- self.button3.pack(pady=20)
- # Create login entry widgets
- self.username_label = tk.Label(master, text="Username:")
- self.username_label.pack()
- self.username_entry = tk.Entry(master)
- self.username_entry.pack()
- self.password_label = tk.Label(master, text="Password:")
- self.password_label.pack()
- self.password_entry = tk.Entry(master, show="*")
- self.password_entry.pack()
- self.error_label = tk.Label(master, fg="red")
- self.error_label.pack()
- # Button functions
- def login(self):
- # Open login file and check credentials
- with open("login.txt", "r") as f:
- lines = f.readlines()
- credentials = [line.strip().split(":") for line in lines]
- for username, password in credentials:
- if self.username_entry.get() == username and self.password_entry.get() == password:
- self.button1.pack_forget() # Remove the login button
- self.button2.config(state="normal")
- self.button3.config(state="normal")
- self.username_entry.delete(0, "end")
- self.password_entry.delete(0, "end")
- return
- # Show error message if login fails
- self.error_label.config(text="Invalid username or password")
- def option1(self):
- # Close the current window
- self.master.destroy()
- # Create a new window
- new_window = tk.Toplevel()
- new_window.title("Checkout")
- new_window.geometry("800x800")
- # Add widgets to the new window
- label = tk.Label(new_window, text="Checkout selected")
- label.pack(pady=20)
- # Add buttons to the new window
- self.add_to_Stock(new_window)
- def add_to_Stock(self, window):
- # Open the database connection
- conn = sqlite3.connect("Stock.db")
- c = conn.cursor()
- # Create the Stock table if it does not exist
- c.execute("CREATE TABLE IF NOT EXISTS Stock (item_name TEXT, item_price REAL)")
- # Fetch items from the database
- c.execute("SELECT * FROM Stock")
- items = c.fetchall()
- # Create a frame to hold the buttons
- frame = tk.Frame(window)
- frame.pack()
- # Add a label to display the total
- total_label = tk.Label(window, text="Total: $0.00")
- total_label.pack(pady=10)
- # Create a button for each item and update the total when clicked
- for item in items:
- item_name = item[0]
- item_price = item[1]
- button = tk.Button(frame, text=f"{item_name} (${item_price:.2f})", command=lambda name=item_name, price=item_price: self.add_item_to_Stock(name, price, total_label))
- button.pack(side="left", padx=20, pady=10)
- # Add entry widgets for new Stock
- name_label = tk.Label(window, text="Item Name:")
- name_label.pack(pady=10)
- self.name_entry = tk.Entry(window)
- self.name_entry.pack(pady=5)
- price_label = tk.Label(window, text="Item Price:")
- price_label.pack(pady=10)
- self.price_entry = tk.Entry(window)
- self.price_entry.pack(pady=5)
- # Add a confirmation button to add new Stock
- confirm_button = tk.Button(frame, text="Confirm", command=self.confirm_add_item)
- confirm_button.pack(pady=10)
- # Close the database connection
- conn.close()
- def add_item_price_to_total(price, total_label,self):
- total_text = total_label['text']
- total = float(total_text.replace('Total: $', ''))
- total += price
- total_label.config(text=f'Total: ${total:.2f}')
- # Add the item price to the total and update the label
- self.add_item_price_to_total(price, total_label)
- self.add_item_price_to_total(price)
- def add_item_price_to_total(self, price):
- # Add the item to the database
- conn = sqlite3.connect("Stock.db")
- c = conn.cursor()
- c.execute("INSERT INTO Stock (item_name, item_price) VALUES (?, ?)", (name, price))
- conn.commit()
- conn.close()
- # Clear the entry fields
- self.name_entry.delete(0, tk.END)
- self.price_entry.delete(0, tk.END)
- def option2(self):
- print("Option 2 selected")
- def open_new_window(self):
- # Create a new window
- new_window = tk.Toplevel(self.master)
- new_window.title("Settings")
- new_window.geometry("400x400")
- # Add widgets to the new window
- label = tk.Label(new_window, text="Settings")
- label.pack(pady=20)
- close_button = tk.Button(new_window, text="Done", command=new_window.destroy)
- close_button.pack(pady=20)
- root = tk.Tk()
- menu_system = MenuSystem(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement