Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2023
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | Source Code | 0 0
  1. import tkinter as tk
  2. import sqlite3
  3.  
  4. with open("login.txt", "w") as f:
  5. f.write("john:password123\n")
  6. f.write("jane:letmein\n")
  7. f.write("admin:adminpassword\n")
  8.  
  9. class MenuSystem:
  10. def __init__(self, master):
  11. self.master = master
  12. master.title("Menu System")
  13. master.geometry("700x700") # set window size
  14.  
  15. # Create buttons
  16. self.button1 = tk.Button(master, text="Login", command=self.login, width=20, height=5)
  17. self.button2 = tk.Button(master, text="Start Checkout", command=self.option1, width=20, height=5, state="disabled")
  18. self.button3 = tk.Button(master, text="Settings", command=self.open_new_window, width=20, height=5, state="disabled")
  19.  
  20. # Pack buttons
  21. self.button1.pack(pady=20)
  22. self.button2.pack(pady=20)
  23. self.button3.pack(pady=20)
  24.  
  25. # Create login entry widgets
  26. self.username_label = tk.Label(master, text="Username:")
  27. self.username_label.pack()
  28. self.username_entry = tk.Entry(master)
  29. self.username_entry.pack()
  30.  
  31. self.password_label = tk.Label(master, text="Password:")
  32. self.password_label.pack()
  33. self.password_entry = tk.Entry(master, show="*")
  34. self.password_entry.pack()
  35.  
  36. self.error_label = tk.Label(master, fg="red")
  37. self.error_label.pack()
  38.  
  39. # Button functions
  40. def login(self):
  41. # Open login file and check credentials
  42. with open("login.txt", "r") as f:
  43. lines = f.readlines()
  44. credentials = [line.strip().split(":") for line in lines]
  45. for username, password in credentials:
  46. if self.username_entry.get() == username and self.password_entry.get() == password:
  47. self.button1.pack_forget() # Remove the login button
  48. self.button2.config(state="normal")
  49. self.button3.config(state="normal")
  50. self.username_entry.delete(0, "end")
  51. self.password_entry.delete(0, "end")
  52. return
  53. # Show error message if login fails
  54. self.error_label.config(text="Invalid username or password")
  55.  
  56. def option1(self):
  57. # Close the current window
  58. self.master.destroy()
  59.  
  60. # Create a new window
  61. new_window = tk.Toplevel()
  62. new_window.title("Checkout")
  63. new_window.geometry("800x800")
  64.  
  65. # Add widgets to the new window
  66. label = tk.Label(new_window, text="Checkout selected")
  67. label.pack(pady=20)
  68.  
  69. # Add buttons to the new window
  70. self.add_to_Stock(new_window)
  71.  
  72. def add_to_Stock(self, window):
  73. # Open the database connection
  74. conn = sqlite3.connect("Stock.db")
  75. c = conn.cursor()
  76.  
  77. # Create the Stock table if it does not exist
  78. c.execute("CREATE TABLE IF NOT EXISTS Stock (item_name TEXT, item_price REAL)")
  79.  
  80. # Fetch items from the database
  81. c.execute("SELECT * FROM Stock")
  82. items = c.fetchall()
  83.  
  84. # Create a frame to hold the buttons
  85. frame = tk.Frame(window)
  86. frame.pack()
  87.  
  88. # Add a label to display the total
  89. total_label = tk.Label(window, text="Total: $0.00")
  90. total_label.pack(pady=10)
  91.  
  92. # Create a button for each item and update the total when clicked
  93. for item in items:
  94. item_name = item[0]
  95. item_price = item[1]
  96. 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))
  97. button.pack(side="left", padx=20, pady=10)
  98.  
  99. # Add entry widgets for new Stock
  100. name_label = tk.Label(window, text="Item Name:")
  101. name_label.pack(pady=10)
  102. self.name_entry = tk.Entry(window)
  103. self.name_entry.pack(pady=5)
  104.  
  105. price_label = tk.Label(window, text="Item Price:")
  106. price_label.pack(pady=10)
  107. self.price_entry = tk.Entry(window)
  108. self.price_entry.pack(pady=5)
  109.  
  110. # Add a confirmation button to add new Stock
  111. confirm_button = tk.Button(frame, text="Confirm", command=self.confirm_add_item)
  112. confirm_button.pack(pady=10)
  113.  
  114.  
  115.  
  116.  
  117. # Close the database connection
  118. conn.close()
  119.  
  120. def add_item_price_to_total(price, total_label,self):
  121. total_text = total_label['text']
  122. total = float(total_text.replace('Total: $', ''))
  123. total += price
  124. total_label.config(text=f'Total: ${total:.2f}')
  125. # Add the item price to the total and update the label
  126. self.add_item_price_to_total(price, total_label)
  127. self.add_item_price_to_total(price)
  128.  
  129.  
  130.  
  131. def add_item_price_to_total(self, price):
  132. # Add the item to the database
  133. conn = sqlite3.connect("Stock.db")
  134. c = conn.cursor()
  135. c.execute("INSERT INTO Stock (item_name, item_price) VALUES (?, ?)", (name, price))
  136. conn.commit()
  137. conn.close()
  138.  
  139. # Clear the entry fields
  140. self.name_entry.delete(0, tk.END)
  141. self.price_entry.delete(0, tk.END)
  142.  
  143. def option2(self):
  144. print("Option 2 selected")
  145.  
  146. def open_new_window(self):
  147. # Create a new window
  148. new_window = tk.Toplevel(self.master)
  149. new_window.title("Settings")
  150. new_window.geometry("400x400")
  151.  
  152. # Add widgets to the new window
  153. label = tk.Label(new_window, text="Settings")
  154. label.pack(pady=20)
  155. close_button = tk.Button(new_window, text="Done", command=new_window.destroy)
  156. close_button.pack(pady=20)
  157.  
  158. root = tk.Tk()
  159. menu_system = MenuSystem(root)
  160. root.mainloop()
  161.  
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement