Merge branch 'main' of https://git-eng.ukwms.ac.id/5803025001/Python-Menu
This commit is contained in:
commit
2a9b7a4edc
@ -1,6 +1,6 @@
|
||||
import tkinter as tk
|
||||
from database import connect
|
||||
from tkinter import messagebox
|
||||
from database import connect
|
||||
|
||||
class AdminMenu:
|
||||
def __init__(self, root):
|
||||
@ -18,7 +18,7 @@ class AdminMenu:
|
||||
).pack(pady=15)
|
||||
|
||||
# =====================
|
||||
# FORM INPUT (RAPI)
|
||||
# FORM INPUT
|
||||
# =====================
|
||||
form = tk.Frame(self.frame)
|
||||
form.pack(pady=10)
|
||||
@ -28,15 +28,27 @@ class AdminMenu:
|
||||
self.nama = tk.Entry(form, width=30)
|
||||
self.nama.grid(row=0, column=1)
|
||||
|
||||
tk.Label(form, text="Harga", width=15, anchor="w")\
|
||||
tk.Label(form, text="Kategori", width=15, anchor="w")\
|
||||
.grid(row=1, column=0, pady=5)
|
||||
self.kategori = tk.StringVar()
|
||||
tk.OptionMenu(
|
||||
form,
|
||||
self.kategori,
|
||||
"Makanan",
|
||||
"Minuman",
|
||||
"Dessert"
|
||||
).grid(row=1, column=1, sticky="w")
|
||||
self.kategori.set("Makanan")
|
||||
|
||||
tk.Label(form, text="Harga", width=15, anchor="w")\
|
||||
.grid(row=2, column=0, pady=5)
|
||||
self.harga = tk.Entry(form, width=30)
|
||||
self.harga.grid(row=1, column=1)
|
||||
self.harga.grid(row=2, column=1)
|
||||
|
||||
tk.Label(form, text="Path Gambar", width=15, anchor="w")\
|
||||
.grid(row=2, column=0, pady=5)
|
||||
.grid(row=3, column=0, pady=5)
|
||||
self.gambar = tk.Entry(form, width=30)
|
||||
self.gambar.grid(row=2, column=1)
|
||||
self.gambar.grid(row=3, column=1)
|
||||
|
||||
# =====================
|
||||
# BUTTON CRUD
|
||||
@ -44,76 +56,109 @@ class AdminMenu:
|
||||
btn_frame = tk.Frame(self.frame)
|
||||
btn_frame.pack(pady=10)
|
||||
|
||||
tk.Button(btn_frame, text="Tambah", bg="#d1e7dd",
|
||||
width=10, command=self.tambah).grid(row=0, column=0, padx=5)
|
||||
tk.Button(btn_frame, text="Tambah", width=10,
|
||||
bg="#d1e7dd", command=self.tambah).grid(row=0, column=0, padx=5)
|
||||
|
||||
tk.Button(btn_frame, text="Update", bg="#cfe2ff",
|
||||
width=10, command=self.update).grid(row=0, column=1, padx=5)
|
||||
tk.Button(btn_frame, text="Update", width=10,
|
||||
bg="#cfe2ff", command=self.update).grid(row=0, column=1, padx=5)
|
||||
|
||||
tk.Button(btn_frame, text="Hapus", bg="#f8d7da",
|
||||
width=10, command=self.hapus).grid(row=0, column=2, padx=5)
|
||||
tk.Button(btn_frame, text="Hapus", width=10,
|
||||
bg="#f8d7da", command=self.hapus).grid(row=0, column=2, padx=5)
|
||||
|
||||
tk.Button(btn_frame, text="Refresh",
|
||||
width=10, command=self.load_menu).grid(row=0, column=3, padx=5)
|
||||
tk.Button(btn_frame, text="Refresh", width=10,
|
||||
command=self.load_menu).grid(row=0, column=3, padx=5)
|
||||
|
||||
tk.Button(btn_frame, text="Logout",
|
||||
width=10, command=self.logout).grid(row=0, column=4, padx=5)
|
||||
tk.Button(btn_frame, text="Logout", width=10,
|
||||
command=self.logout).grid(row=0, column=4, padx=5)
|
||||
|
||||
# =====================
|
||||
# FILTER
|
||||
# =====================
|
||||
filter_frame = tk.Frame(self.frame)
|
||||
filter_frame.pack(pady=5)
|
||||
|
||||
tk.Label(filter_frame, text="Filter Kategori:").pack(side=tk.LEFT)
|
||||
self.filter_kategori = tk.StringVar()
|
||||
tk.OptionMenu(
|
||||
filter_frame,
|
||||
self.filter_kategori,
|
||||
"Semua",
|
||||
"Makanan",
|
||||
"Minuman",
|
||||
"Dessert",
|
||||
command=self.load_menu
|
||||
).pack(side=tk.LEFT)
|
||||
self.filter_kategori.set("Semua")
|
||||
|
||||
# =====================
|
||||
# LIST MENU
|
||||
# =====================
|
||||
tk.Label(self.frame, text="Daftar Menu", font=("Arial", 12, "bold"))\
|
||||
.pack(pady=5)
|
||||
|
||||
self.listbox = tk.Listbox(self.frame, width=60, height=10)
|
||||
self.listbox.pack(pady=5)
|
||||
self.listbox = tk.Listbox(self.frame, width=80, height=10)
|
||||
self.listbox.pack(pady=10)
|
||||
self.listbox.bind("<<ListboxSelect>>", self.pilih_menu)
|
||||
|
||||
self.menu_data = []
|
||||
self.load_menu()
|
||||
|
||||
# =====================
|
||||
# DATABASE FUNCTION
|
||||
# LOAD MENU + FILTER
|
||||
# =====================
|
||||
def load_menu(self):
|
||||
def load_menu(self, *_):
|
||||
self.listbox.delete(0, tk.END)
|
||||
db = connect()
|
||||
cur = db.cursor()
|
||||
|
||||
if self.filter_kategori.get() == "Semua":
|
||||
cur.execute("SELECT * FROM menu")
|
||||
else:
|
||||
cur.execute(
|
||||
"SELECT * FROM menu WHERE kategori=?",
|
||||
(self.filter_kategori.get(),)
|
||||
)
|
||||
|
||||
self.menu_data = cur.fetchall()
|
||||
db.close()
|
||||
|
||||
for menu in self.menu_data:
|
||||
for m in self.menu_data:
|
||||
self.listbox.insert(
|
||||
tk.END,
|
||||
f"{menu[0]} | {menu[1]} | Rp {menu[2]:,.0f}"
|
||||
f"{m[0]:<3} | {m[1]:<20} | {m[2]:<10} | Rp {m[3]:,.0f}"
|
||||
)
|
||||
|
||||
# =====================
|
||||
# PILIH MENU
|
||||
# =====================
|
||||
def pilih_menu(self, event):
|
||||
idx = self.listbox.curselection()
|
||||
if not idx:
|
||||
return
|
||||
|
||||
menu = self.menu_data[idx[0]]
|
||||
|
||||
self.clear_form()
|
||||
|
||||
self.nama.insert(0, menu[1])
|
||||
self.harga.insert(0, menu[2])
|
||||
self.gambar.insert(0, menu[3])
|
||||
self.kategori.set(menu[2])
|
||||
self.harga.insert(0, menu[3])
|
||||
self.gambar.insert(0, menu[4])
|
||||
|
||||
# =====================
|
||||
# CRUD
|
||||
# =====================
|
||||
def tambah(self):
|
||||
if not self.nama.get() or not self.harga.get():
|
||||
messagebox.showwarning("Error", "Nama dan Harga wajib diisi")
|
||||
messagebox.showwarning("Error", "Data belum lengkap")
|
||||
return
|
||||
|
||||
db = connect()
|
||||
cur = db.cursor()
|
||||
cur.execute(
|
||||
"INSERT INTO menu(nama,harga,gambar) VALUES (?,?,?)",
|
||||
(self.nama.get(), float(self.harga.get()), self.gambar.get())
|
||||
"INSERT INTO menu(nama,kategori,harga,gambar) VALUES (?,?,?,?)",
|
||||
(
|
||||
self.nama.get(),
|
||||
self.kategori.get(),
|
||||
float(self.harga.get()),
|
||||
self.gambar.get()
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
db.close()
|
||||
@ -125,7 +170,7 @@ class AdminMenu:
|
||||
def update(self):
|
||||
idx = self.listbox.curselection()
|
||||
if not idx:
|
||||
messagebox.showwarning("Pilih", "Pilih menu yang ingin diupdate")
|
||||
messagebox.showwarning("Pilih", "Pilih menu dulu")
|
||||
return
|
||||
|
||||
menu_id = self.menu_data[idx[0]][0]
|
||||
@ -133,8 +178,14 @@ class AdminMenu:
|
||||
db = connect()
|
||||
cur = db.cursor()
|
||||
cur.execute(
|
||||
"UPDATE menu SET nama=?, harga=?, gambar=? WHERE id=?",
|
||||
(self.nama.get(), float(self.harga.get()), self.gambar.get(), menu_id)
|
||||
"UPDATE menu SET nama=?, kategori=?, harga=?, gambar=? WHERE id=?",
|
||||
(
|
||||
self.nama.get(),
|
||||
self.kategori.get(),
|
||||
float(self.harga.get()),
|
||||
self.gambar.get(),
|
||||
menu_id
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
db.close()
|
||||
@ -145,7 +196,7 @@ class AdminMenu:
|
||||
def hapus(self):
|
||||
idx = self.listbox.curselection()
|
||||
if not idx:
|
||||
messagebox.showwarning("Pilih", "Pilih menu yang ingin dihapus")
|
||||
messagebox.showwarning("Pilih", "Pilih menu dulu")
|
||||
return
|
||||
|
||||
menu_id = self.menu_data[idx[0]][0]
|
||||
@ -165,6 +216,7 @@ class AdminMenu:
|
||||
self.nama.delete(0, tk.END)
|
||||
self.harga.delete(0, tk.END)
|
||||
self.gambar.delete(0, tk.END)
|
||||
self.kategori.set("Makanan")
|
||||
|
||||
def logout(self):
|
||||
self.frame.destroy()
|
||||
|
||||
@ -7,7 +7,9 @@ def setup_database():
|
||||
db = connect()
|
||||
cur = db.cursor()
|
||||
|
||||
# Tabel Users
|
||||
# =====================
|
||||
# TABEL USERS
|
||||
# =====================
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS users(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@ -17,23 +19,28 @@ def setup_database():
|
||||
)
|
||||
""")
|
||||
|
||||
# Tabel Menu
|
||||
# =====================
|
||||
# TABEL MENU (ADA KATEGORI)
|
||||
# =====================
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS menu(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
nama TEXT,
|
||||
kategori TEXT,
|
||||
harga REAL,
|
||||
gambar TEXT
|
||||
)
|
||||
""")
|
||||
|
||||
# Tambah kolom kategori (aman)
|
||||
# Jaga-jaga DB lama (tidak error)
|
||||
try:
|
||||
cur.execute("ALTER TABLE menu ADD COLUMN kategori TEXT")
|
||||
except sqlite3.OperationalError:
|
||||
pass
|
||||
|
||||
# Tabel Transaksi
|
||||
# =====================
|
||||
# TABEL TRANSAKSI
|
||||
# =====================
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS transaksi(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@ -44,7 +51,9 @@ def setup_database():
|
||||
)
|
||||
""")
|
||||
|
||||
# Tabel Detail Transaksi
|
||||
# =====================
|
||||
# DETAIL TRANSAKSI
|
||||
# =====================
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS detail_transaksi(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@ -56,7 +65,9 @@ def setup_database():
|
||||
)
|
||||
""")
|
||||
|
||||
# Insert Akun Default
|
||||
# =====================
|
||||
# INSERT USER DEFAULT
|
||||
# =====================
|
||||
cur.execute("SELECT COUNT(*) FROM users")
|
||||
if cur.fetchone()[0] == 0:
|
||||
users = [
|
||||
@ -70,9 +81,10 @@ def setup_database():
|
||||
"INSERT INTO users(username,password,role) VALUES (?,?,?)",
|
||||
users
|
||||
)
|
||||
db.commit()
|
||||
|
||||
# Insert Menu Default
|
||||
# =====================
|
||||
# INSERT MENU DEFAULT
|
||||
# =====================
|
||||
cur.execute("SELECT COUNT(*) FROM menu")
|
||||
if cur.fetchone()[0] == 0:
|
||||
menus = [
|
||||
@ -80,13 +92,13 @@ def setup_database():
|
||||
('Bakso', 'Makanan', 15000, 'aset/bakso.jpg'),
|
||||
('Mie Ayam', 'Makanan', 12000, 'aset/mie_ayam.jpg'),
|
||||
('Es Teh', 'Minuman', 5000, 'aset/es_teh.jpg'),
|
||||
('Es Teler', 'Minuman', 15000, 'aset/es_teler.jpg'),
|
||||
('Es Teler', 'Dessert', 15000, 'aset/es_teler.jpg'),
|
||||
('Jus Jeruk', 'Minuman', 8000, 'aset/jus_jeruk.jpg')
|
||||
]
|
||||
cur.executemany(
|
||||
"INSERT INTO menu (nama, kategori, harga, gambar) VALUES (?,?,?,?)",
|
||||
menus
|
||||
)
|
||||
db.commit()
|
||||
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user