92 lines
3.1 KiB
Python
92 lines
3.1 KiB
Python
import tkinter as tk
|
|
from PIL import Image, ImageTk
|
|
from database import connect
|
|
from tkinter import messagebox
|
|
|
|
class PembeliMenu:
|
|
def __init__(self, parent):
|
|
self.parent = parent
|
|
self.frame = tk.Frame(parent)
|
|
self.frame.pack(fill="both", expand=True)
|
|
|
|
|
|
self.cart = [] # Simpan tuple (nama, harga)
|
|
self.images = [] # Simpan reference gambar agar tidak dihapus GC
|
|
|
|
# --- Judul Halaman ---
|
|
tk.Label(self.frame, text="MENU PEMBELI", font=("Arial", 18, "bold")).pack(pady=10)
|
|
|
|
# --- Tombol Logout ---
|
|
tk.Button(self.frame, text="Logout", bg="#f9e79f", command=self.logout).pack(pady=5)
|
|
|
|
# --- Frame Menu ---
|
|
self.menu_frame = tk.Frame(self.frame)
|
|
self.menu_frame.pack(pady=10)
|
|
|
|
self.load_menu() # Load menu dari database
|
|
|
|
# --- Daftar Pesanan ---
|
|
tk.Label(self.frame, text="Daftar Pesanan:", font=("Arial", 12, "bold")).pack(pady=5)
|
|
self.listbox = tk.Listbox(self.frame, width=50, height=6)
|
|
self.listbox.pack()
|
|
|
|
# --- Total ---
|
|
self.total_lbl = tk.Label(self.frame, text="Total: Rp 0", font=("Arial", 12, "bold"))
|
|
self.total_lbl.pack(pady=5)
|
|
|
|
# --- Tombol Checkout ---
|
|
tk.Button(self.frame, text="Checkout", bg="#d1e7dd", command=self.checkout).pack(pady=5)
|
|
|
|
def load_menu(self):
|
|
db = connect()
|
|
cur = db.cursor()
|
|
cur.execute("SELECT nama, harga, gambar FROM menu")
|
|
data = cur.fetchall()
|
|
db.close()
|
|
|
|
for i, (nama, harga, gambar) in enumerate(data):
|
|
f = tk.Frame(self.menu_frame, bd=2, relief="ridge")
|
|
f.grid(row=i//3, column=i%3, padx=10, pady=10)
|
|
|
|
try:
|
|
img = Image.open(gambar).resize((120, 90))
|
|
photo = ImageTk.PhotoImage(img)
|
|
self.images.append(photo)
|
|
tk.Label(f, image=photo).pack()
|
|
except FileNotFoundError:
|
|
tk.Label(f, text="No Image").pack()
|
|
|
|
tk.Label(f, text=nama).pack()
|
|
tk.Label(f, text=f"Rp {harga:,}").pack()
|
|
tk.Button(f, text="Pesan", bg="#cfe2ff",
|
|
command=lambda n=nama, h=harga: self.add(n, h)).pack(pady=3)
|
|
|
|
def add(self, nama, harga):
|
|
self.cart.append((nama, harga))
|
|
self.listbox.insert(tk.END, f"{nama} - Rp {harga:,}")
|
|
total = sum(h for _, h in self.cart)
|
|
self.total_lbl.config(text=f"Total: Rp {total:,}")
|
|
|
|
def checkout(self):
|
|
if not self.cart:
|
|
messagebox.showwarning("Pesan Kosong", "Belum ada pesanan!")
|
|
return
|
|
|
|
db = connect()
|
|
cur = db.cursor()
|
|
for nama, harga in self.cart:
|
|
cur.execute("INSERT INTO orders VALUES (NULL, ?, ?)", (nama, harga))
|
|
db.commit()
|
|
db.close()
|
|
|
|
messagebox.showinfo("Sukses", "Pesanan dikirim ke kasir")
|
|
self.cart.clear()
|
|
self.listbox.delete(0, tk.END)
|
|
self.total_lbl.config(text="Total: Rp 0")
|
|
|
|
def logout(self):
|
|
self.frame.destroy()
|
|
from main import LoginScreen
|
|
LoginScreen(self.parent)
|
|
|