CRUD DATA PADA TABEL Gn_Cyan_Atas, dan Sub_Gn_Cyan_Atas PADA DATA KOLOM Bass DAN KOLOM Trebel (Part-1)
CRUD DATA PADA TABEL Gn_Cyan_Atas, dan Sub_Gn_Cyan_Atas PADA DATA KOLOM Bass
Kita sudah selesai melakukan CRUD data pada tabel Gn_Cyan_Bawah, dan Sub_Gn_Cyan_Bawah pada data kolom Bass, Trebel_Biasa, dan Trebel_Aneh. Sekarang akan kita mulai melakukan CRUD pada tabel Gn_Cyan_Atas, dan Sub_Gn_Cyan_Atas pada data Bass dan data Trebel.
Baik, jika kita akan mulai melakukan CRUD pada tabel `Gn_Cyan_Atas` dan `Sub_Gn_Cyan_Atas`, langkah-langkah yang perlu diambil adalah sebagai berikut:
### 1. **Desain Struktur Tabel**
- Pastikan tabel `Gn_Cyan_Atas` dan `Sub_Gn_Cyan_Atas` memiliki kolom yang sesuai dengan kebutuhan CRUD (Create, Read, Update, Delete). Biasanya, kolom yang diperlukan meliputi ID, Level_Gn, Bass, Trebel, Progres, Mode, dll.
### 2. **Buat Fungsi CRUD untuk `Gn_Cyan_Atas` dan `Sub_Gn_Cyan_Atas`**
- Kita akan membuat fungsi yang menangani operasi CRUD untuk kedua tabel. Operasi ini mencakup menambahkan data baru, membaca data yang ada, memperbarui data, dan menghapus data.
### 3. **Integrasikan dengan Tkinter GUI**
- Kita akan menambahkan input form, tombol, dan grid view untuk memfasilitasi operasi CRUD melalui antarmuka pengguna.
Berikut adalah struktur kode dasar untuk mulai melakukan CRUD:
#### a. **Create Function untuk `Gn_Cyan_Atas` dan `Sub_Gn_Cyan_Atas`**
- Fungsi untuk menambah data baru ke dalam tabel.
```python
import sqlite3
def create_data_gn_atas(id, level_gn, bass, trebel, progres, mode):
conn = sqlite3.connect('DATAMUSIK.db')
cursor = conn.cursor()
cursor.execute('''INSERT INTO Gn_Cyan_Atas (ID, Level_Gn, Bass, Trebel, Progres, Mode)
VALUES (?, ?, ?, ?, ?, ?)''', (id, level_gn, bass, trebel, progres, mode))
conn.commit()
conn.close()
def create_data_sub_gn_atas(sub_id, level_gn, bass, trebel, progres, mode):
conn = sqlite3.connect('DATAMUSIK.db')
cursor = conn.cursor()
cursor.execute('''INSERT INTO Sub_Gn_Cyan_Atas (Sub_ID, Level_Gn, Bass, Trebel, Progres, Mode)
VALUES (?, ?, ?, ?, ?, ?)''', (sub_id, level_gn, bass, trebel, progres, mode))
conn.commit()
conn.close()
```
#### b. **Read Function untuk `Gn_Cyan_Atas` dan `Sub_Gn_Cyan_Atas`**
- Fungsi untuk membaca data yang ada di dalam tabel.
```python
def read_data_gn_atas():
conn = sqlite3.connect('DATAMUSIK.db')
cursor = conn.cursor()
cursor.execute('''SELECT * FROM Gn_Cyan_Atas''')
records = cursor.fetchall()
conn.close()
return records
def read_data_sub_gn_atas():
conn = sqlite3.connect('DATAMUSIK.db')
cursor = conn.cursor()
cursor.execute('''SELECT * FROM Sub_Gn_Cyan_Atas''')
records = cursor.fetchall()
conn.close()
return records
```
#### c. **Update Function untuk `Gn_Cyan_Atas` dan `Sub_Gn_Cyan_Atas`**
- Fungsi untuk memperbarui data yang sudah ada di dalam tabel.
```python
def update_data_gn_atas(id, level_gn, bass, trebel, progres, mode):
conn = sqlite3.connect('DATAMUSIK.db')
cursor = conn.cursor()
cursor.execute('''UPDATE Gn_Cyan_Atas SET Level_Gn=?, Bass=?, Trebel=?, Progres=?, Mode=?
WHERE ID=?''', (level_gn, bass, trebel, progres, mode, id))
conn.commit()
conn.close()
def update_data_sub_gn_atas(sub_id, level_gn, bass, trebel, progres, mode):
conn = sqlite3.connect('DATAMUSIK.db')
cursor = conn.cursor()
cursor.execute('''UPDATE Sub_Gn_Cyan_Atas SET Level_Gn=?, Bass=?, Trebel=?, Progres=?, Mode=?
WHERE Sub_ID=?''', (level_gn, bass, trebel, progres, mode, sub_id))
conn.commit()
conn.close()
```
#### d. **Delete Function untuk `Gn_Cyan_Atas` dan `Sub_Gn_Cyan_Atas`**
- Fungsi untuk menghapus data yang ada di dalam tabel.
```python
def delete_data_gn_atas(id):
conn = sqlite3.connect('DATAMUSIK.db')
cursor = conn.cursor()
cursor.execute('''DELETE FROM Gn_Cyan_Atas WHERE ID=?''', (id,))
conn.commit()
conn.close()
def delete_data_sub_gn_atas(sub_id):
conn = sqlite3.connect('DATAMUSIK.db')
cursor = conn.cursor()
cursor.execute('''DELETE FROM Sub_Gn_Cyan_Atas WHERE Sub_ID=?''', (sub_id,))
conn.commit()
conn.close()
```
### 4. **Integrasikan CRUD dengan GUI Tkinter**
- Gunakan input field, button, dan grid layout untuk mengimplementasikan antarmuka CRUD pada GUI Tkinter. Anda bisa menggunakan `Entry`, `Button`, dan `Treeview` atau `Listbox` untuk menampilkan data.
### 5. **Pengujian CRUD**
- Setelah semua fungsi CRUD dibuat dan diintegrasikan dengan GUI, lakukan pengujian untuk memastikan bahwa operasi CRUD berjalan sesuai yang diharapkan pada tabel `Gn_Cyan_Atas` dan `Sub_Gn_Cyan_Atas`.
Jika ada spesifikasi lebih lanjut tentang bagaimana data harus diproses atau ditampilkan, atau jika ada kebutuhan khusus terkait dengan logika bisnis atau antarmuka, Anda bisa memberikan detail tersebut untuk diterapkan dalam kode ini.
Sebuah file sudah saya buat dengan nama file: CRUD_Gn_Cyan_Atas(gabung).py. Pada file berisikan kode fungsi menginisialisasi database DATAMUSIK.db apakah tabel Gn_Cyan_Atas, dan Sub_Gn_Cyan_Atas ada atau tidak pada database, kode juga membentuk frame sesuai dengan yang diinginkan, sementara kode isi file berikut ini:
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import sqlite3
from tkinter import Tk, Label, Entry, Button, Frame, StringVar
# Function to initialize database
def init_db():
conn = sqlite3.connect("DATAMUSIK.db")
c = conn.cursor()
# Create Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Gn_Cyan_Atas (
ID INTEGER PRIMARY KEY,
Level_R TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
# Create Sub_Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Sub_Gn_Cyan_Atas (
Sub_ID INTEGER PRIMARY KEY,
Level_D TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
conn.commit()
conn.close()
# Initialize the main window
root = tk.Tk()
root.geometry("690x1390")
root.title("Arah Arus Musik")
# Head Frame
head_frame = tk.Frame(root, bg="#158aff", highlightbackground="white", highlightthickness=1)
head_frame.place()
# Content Frame
content_frame = tk.Frame(root)
content_frame.pack()
# Function to clear content frame
def clear_content_frame():
for widget in content_frame.winfo_children():
widget.destroy()
# Initialize datab
ase
init_db()
# Start the main loop
root.mainloop()
Kode yang telah Anda buat pada file `CRUD_Gn_Cyan_Atas(gabung).py` sudah cukup baik dan berfungsi untuk menginisialisasi tabel `Gn_Cyan_Atas` dan `Sub_Gn_Cyan_Atas` pada database `DATAMUSIK.db`, serta membentuk frame dasar pada aplikasi menggunakan tkinter. Ada beberapa hal yang bisa ditambahkan atau disesuaikan tergantung pada kebutuhan proyek Anda. Berikut beberapa saran:
1. **Penambahan Entry dan Label untuk Data Input**:
Jika Anda ingin memungkinkan pengguna untuk memasukkan data ke dalam tabel `Gn_Cyan_Atas` dan `Sub_Gn_Cyan_Atas`, Anda bisa menambahkan beberapa `Label` dan `Entry` widget pada `content_frame` untuk memungkinkan input dari pengguna. Contoh penambahan input form adalah sebagai berikut:
```python
# Labels and Entries for Gn_Cyan_Atas
Label(content_frame, text="ID").grid(row=0, column=0)
entry_id = Entry(content_frame)
entry_id.grid(row=0, column=1)
Label(content_frame, text="Level_R").grid(row=1, column=0)
entry_level_r = Entry(content_frame)
entry_level_r.grid(row=1, column=1)
# Add similar Labels and Entries for Bass_1 to Trebel_5
```
2. **Fungsi untuk Menyimpan Data ke Database**:
Buat fungsi yang akan menyimpan data dari `Entry` widget ke dalam tabel yang relevan. Berikut contoh kode fungsi untuk menyimpan data ke `Gn_Cyan_Atas`:
```python
def save_data():
conn = sqlite3.connect("DATAMUSIK.db")
c = conn.cursor()
c.execute('''INSERT INTO Gn_Cyan_Atas (ID, Level_R, Bass_1, Bass_2, Bass_3, Bass_4, Bass_5, Trebel_1, Trebel_2, Trebel_3, Trebel_4, Trebel_5)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''',
(entry_id.get(), entry_level_r.get(), entry_bass_1.get(), entry_bass_2.get(), entry_bass_3.get(), entry_bass_4.get(), entry_bass_5.get(),
entry_trebel_1.get(), entry_trebel_2.get(), entry_trebel_3.get(), entry_trebel_4.get(), entry_trebel_5.get()))
conn.commit()
conn.close()
messagebox.showinfo("Success", "Data saved successfully!")
```
3. **Tombol untuk Menyimpan Data**:
Setelah membuat fungsi untuk menyimpan data, Anda perlu menambahkan tombol di `content_frame` yang memanggil fungsi `save_data()`:
```python
save_button = Button(content_frame, text="Save Data", command=save_data)
save_button.grid(row=12, column=1)
```
4. **Fungsi untuk Menampilkan Data**:
Jika Anda ingin menampilkan data dari database ke dalam aplikasi tkinter, Anda bisa menambahkan fungsi untuk mengambil data dari tabel dan menampilkannya di aplikasi:
```python
def display_data():
conn = sqlite3.connect("DATAMUSIK.db")
c = conn.cursor()
c.execute("SELECT * FROM Gn_Cyan_Atas")
records = c.fetchall()
for record in records:
print(record) # Or display it in the GUI
conn.close()
```
Anda bisa memanggil fungsi ini pada saat aplikasi dimulai atau saat tombol tertentu ditekan.
Dengan penambahan ini, aplikasi Anda akan lebih interaktif, memungkinkan pengguna untuk memasukkan dan menyimpan data ke dalam database. Sesuaikan layout dan fitur tambahan sesuai dengan kebutuhan aplikasi Anda.
Kemudian, pada bagian head_frame, saya membuat toggle_menu yang berisikan 3 buah tombol: toggle_btn1, toggle_btn2, dan toggle_btn3 untuk mengatur Progres, dan mode Naik atau Turun. Saat applikasi pertama kali dijalankan, progres di set_up pada mode Naik. Kemudian pada option_frame saya membuat 5 buah tombol: Gn_A_Trebel_btn untuk menampilkan halaman Gn_A_Trebel_Page, Gn_Biru_btn untuk menampilkan halaman Gn_Biru_Page, Gn_Cyan_Bawah_btn untuk menampilkan halaman Gn_Cyan_Bawah_Page, Gn_Cyan_Atas_btn untuk menampilkan halaman Gn_Cyan_Atas_Page, dan Gn_A_Bass_btn untuk menampilkan halaman Gn_A_Bass_Page, sehingga kode isi file berkembang menjadi berikut ini:
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import sqlite3
from tkinter import Tk, Label, Entry, Button, Frame, StringVar
# Function to initialize database
def init_db():
conn = sqlite3.connect("DATAMUSIK.db")
c = conn.cursor()
# Create Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Gn_Cyan_Atas (
ID INTEGER PRIMARY KEY,
Level_R TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
# Create Sub_Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Sub_Gn_Cyan_Atas (
Sub_ID INTEGER PRIMARY KEY,
Level_D TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
conn.commit()
conn.close()
# Initialize the main window
root = tk.Tk()
root.geometry("690x1390")
root.title("Arah Arus Musik")
# Head Frame
head_frame = tk.Frame(root, bg="#158aff", highlightbackground="white", highlightthickness=1)
head_frame.place()
# Content Frame
content_frame = tk.Frame(root)
content_frame.pack()
# Function to clear content frame
def clear_content_frame():
for widget in content_frame.winfo_children():
widget.destroy()
# Function to switch modes and update pages
def switch_mode(progres, mode):
if mode[progres - 1] == "naik":
mode[progres - 1] = "turun"
setup_turun_mode(progres)
else:
mode[progres - 1] = "naik"
setup_naik_mode(progres)
# Function to toggle menus
def toggle_menu(progres, mode):
if mode[progres - 1] == 'naik':
mode[progres - 1] = 'turun'
toggle_btns[progres - 1].config(text="X _ T")
pages[progres - 1] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
indicator_labels[progres - 1].config(text="Turun")
else:
mode[progres - 1] = 'naik'
toggle_btns[progres - 1].config(text="☰ _ N")
pages[progres - 1] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
indicator_labels[progres - 1].config(text="Naik")
display_pages(progres)
# Function to display pages
def display_pages(progres):
for widget in content_frame.winfo_children():
widget.destroy()
for page in pages[progres - 1]:
if page == 'Gn_Cyan_Atas':
Gn_Cyan_Atas_page()
elif page == 'Gn_Cyan_Atas':
Gn_Cyan_Atas_page()
# Function to setup "Naik" mode
def setup_naik_mode(progres):
if progres == 1:
pages[0] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
elif progres == 2:
pages[1] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
elif progres == 3:
pages[2] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
display_pages(progres)
# Function to setup "Turun" mode
def setup_turun_mode(progres):
if progres == 1:
pages[0] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
elif progres == 2:
pages[1] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
elif progres == 3:
pages[2] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
display_pages(progres)
# Current mode for each toggle button
current_mode = ['naik', 'naik', 'naik']
toggle_btns = []
indicator_labels = []
pages = [
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass'],
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass'],
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
]
# Toggle Button 1
toggle_btn1 = tk.Button(head_frame, text="☰ _ P 1", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(1, current_mode))
toggle_btn1.pack(side=tk.LEFT)
toggle_btn1.place(x=5, y=20)
toggle_btns.append(toggle_btn1)
title_lb1 = tk.Label(head_frame, text="Progres-1", bg="#158aff", fg="white", font=("bold", 7))
title_lb1.pack(side=tk.LEFT)
title_lb1.place(x=108, y=11)
indicator_label1 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label1.pack(side=tk.LEFT)
indicator_label1.place(x=127, y=55)
indicator_labels.append(indicator_label1)
# Toggle Button 2
toggle_btn2 = tk.Button(head_frame, text="☰ _ P 2", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(2, current_mode))
toggle_btn2.pack(side=tk.LEFT)
toggle_btn2.place(x=232, y=20)
toggle_btns.append(toggle_btn2)
title_lb2 = tk.Label(head_frame, text="Progres-2", bg="#158aff", fg="white", font=("bold", 7))
title_lb2.pack(side=tk.LEFT)
title_lb2.place(x=334, y=5)
indicator_label2 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label2.pack(side=tk.LEFT)
indicator_label2.place(x=368, y=55)
indicator_labels.append(indicator_label2)
# Toggle Button 3
toggle_btn3 = tk.Button(head_frame, text="☰ _ P 3", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(3, current_mode))
toggle_btn3.pack(side=tk.LEFT)
toggle_btn3.place(x=460, y=20)
toggle_btns.append(toggle_btn3)
title_lb3 = tk.Label(head_frame, text="Progres-3", bg="#158aff", fg="white", font=("bold", 7))
title_lb3.pack(side=tk.LEFT)
title_lb3.place(x=565, y=5)
indicator_label3 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label3.pack(side=tk.LEFT)
indicator_label3.place(x=599, y=55)
indicator_labels.append(indicator_label3)
head_frame.pack(side=tk.TOP, fill=tk.X)
head_frame.pack_propagate(False)
head_frame.configure(height=120)
# Options Frame
options_frame = tk.Frame(root, bg="#c3c3c3")
# Function to display Gn_A_Trebel page
def Gn_A_Trebel_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_A_Trebel", font=("Helvetica", 10)).pack()
# Function to display Gn_Biru page
def Gn_Biru_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Biru", font=("Helvetica", 10)).pack()
# Function to display Gn_Cyan_Bawah page
def Gn_Cyan_Bawah_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Cyan_Bawah", font=("Helvetica", 10)).pack()
# Function to display Gn_Cyan_Atas page
def Gn_Cyan_Atas_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Cyan_Atas", font=("Helvetica", 10)).pack()
# Function to display Gn_Biru page
def Gn_A_Bass_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_A_Bass", font=("Helvetica", 10)).pack()
#Fungsi tombol di options_frame
Gn_A_Trebel_btn = tk.Button(options_frame, text="Gn_A_Trebel", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=None)
Gn_A_Trebel_btn.place(x=3, y=50)
Gn_A_Trebel_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Trebel_indicate.place(x=2, y=55, width=5, height=45)
Gn_Biru_btn = tk.Button(options_frame, text="Gn_Biru", font=("bold", 7), fg="yellow", bd=0, bg="blue", command=None)
Gn_Biru_btn.place(x=3, y=100)
Gn_Biru_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Biru_indicate.place(x=2, y=105, width=5, height=45)
Gn_Cyan_Bawah_btn = tk.Button(options_frame, text="Gn_Cyan_Bawah", font=("bold", 5), fg="yellow", bd=0, bg="green", command=None)
Gn_Cyan_Bawah_btn.place(x=3, y=150)
Gn_Cyan_Bawah_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Bawah_indicate.place(x=2, y=155, width=5, height=45)
Gn_Cyan_Atas_btn = tk.Button(options_frame, text="Gn_Cyan_Atas", font=("bold", 5), fg="yellow", bd=0, bg="green", command=None)
Gn_Cyan_Atas_btn.place(x=3, y=200)
Gn_Cyan_Atas_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Atas_indicate.place(x=2, y=205, width=5, height=45)
Gn_A_Bass_btn = tk.Button(options_frame, text="Gn_A_Bass", font=("bold", 7), fg="yellow", bd=0, bg="grey", command=None)
Gn_A_Bass_btn.place(x=3, y=250)
Gn_A_Bass_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Bass_indicate.place(x=2, y=255, width=5, height=45)
options_frame.pack(side=tk.LEFT)
options_frame.pack_propagate(False)
options_frame.configure(width=170, height=300)
# Initialize the application with the first progress setup
setup_naik_mode(1)
# Initialize database
init_db()
# Start the main loop
root.mainloop()
Selanjutnya, tolong modifikasi kode, agar supaya susunan tombol di options_frame dari atas ke bawah ketika mode Turun adalah Gn_Cyan_Atas, Gn_Cyan_Bawah, Gn_A_Trebel. Sedangkan bila mode Naik, maka susunan tombol dari atas ke bawah adalah Gn_Cyan_Atas, Gn_Biru, Gn_A_Bass.
Kode yang Anda buat untuk aplikasi tkinter dengan menggunakan toggle buttons dan beberapa halaman (pages) telah terstruktur dengan baik. Namun, ada beberapa detail kecil yang perlu diperbaiki untuk memastikan aplikasi berfungsi sesuai dengan keinginan Anda. Berikut adalah beberapa poin penting:
1. **Fungsi Halaman**:
- Pada fungsi `display_pages()`, ada kesalahan di mana Anda memanggil `Gn_Cyan_Atas_page()` dua kali dalam pengkondisian `if-elif`. Anda seharusnya menggantinya dengan fungsi yang sesuai untuk halaman yang berbeda. Contohnya, `elif page == 'Gn_Cyan_Bawah':` seharusnya memanggil `Gn_Cyan_Bawah_page()`.
2. **Fungsi Tombol di `options_frame`**:
- Anda telah mendefinisikan tombol di `options_frame` seperti `Gn_A_Trebel_btn`, tetapi perintah `command` saat ini tidak mengarahkan ke halaman yang diinginkan. Anda perlu menyesuaikan perintah `command` untuk setiap tombol agar mengarahkan ke halaman yang sesuai. Misalnya:
```python
Gn_A_Trebel_btn.config(command=Gn_A_Trebel_page)
Gn_Biru_btn.config(command=Gn_Biru_page)
Gn_Cyan_Bawah_btn.config(command=Gn_Cyan_Bawah_page)
Gn_Cyan_Atas_btn.config(command=Gn_Cyan_Atas_page)
Gn_A_Bass_btn.config(command=Gn_A_Bass_page)
```
3. **Penempatan `options_frame`**:
- Saat ini, tidak ada kode yang menempatkan `options_frame` di dalam window utama (`root`). Anda perlu menambahkan kode berikut untuk menempatkan `options_frame`:
```python
options_frame.pack(side=tk.LEFT, fill=tk.Y)
```
4. **Indikator Tombol (Indicator Labels)**:
- Indikator tombol seperti `Gn_A_Trebel_indicate`, `Gn_Biru_indicate`, dll. sebaiknya diperbarui saat tombol ditekan agar menandakan halaman yang sedang aktif. Anda dapat mengubah warna atau teks indikator ini saat tombol ditekan.
Berikut adalah versi kode Anda dengan beberapa perbaikan dan penyesuaian:
```python
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import sqlite3
from tkinter import Tk, Label, Entry, Button, Frame, StringVar
# Function to initialize database
def init_db():
conn = sqlite3.connect("DATAMUSIK.db")
c = conn.cursor()
# Create Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Gn_Cyan_Atas (
ID INTEGER PRIMARY KEY,
Level_R TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
# Create Sub_Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Sub_Gn_Cyan_Atas (
Sub_ID INTEGER PRIMARY KEY,
Level_D TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
conn.commit()
conn.close()
# Initialize the main window
root = tk.Tk()
root.geometry("690x1390")
root.title("Arah Arus Musik")
# Head Frame
head_frame = tk.Frame(root, bg="#158aff", highlightbackground="white", highlightthickness=1)
head_frame.place()
# Content Frame
content_frame = tk.Frame(root)
content_frame.pack()
# Function to clear content frame
def clear_content_frame():
for widget in content_frame.winfo_children():
widget.destroy()
# Function to switch modes and update pages
def switch_mode(progres, mode):
if mode[progres - 1] == "naik":
mode[progres - 1] = "turun"
setup_turun_mode(progres)
else:
mode[progres - 1] = "naik"
setup_naik_mode(progres)
# Function to toggle menus
def toggle_menu(progres, mode):
if mode[progres - 1] == 'naik':
mode[progres - 1] = 'turun'
toggle_btns[progres - 1].config(text="X _ T")
pages[progres - 1] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
indicator_labels[progres - 1].config(text="Turun")
else:
mode[progres - 1] = 'naik'
toggle_btns[progres - 1].config(text="☰ _ N")
pages[progres - 1] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
indicator_labels[progres - 1].config(text="Naik")
display_pages(progres)
# Function to display pages
def display_pages(progres):
for widget in content_frame.winfo_children():
widget.destroy()
for page in pages[progres - 1]:
if page == 'Gn_Cyan_Atas':
Gn_Cyan_Atas_page()
elif page == 'Gn_Cyan_Bawah':
Gn_Cyan_Bawah_page()
elif page == 'Gn_A_Trebel':
Gn_A_Trebel_page()
elif page == 'Gn_Biru':
Gn_Biru_page()
elif page == 'Gn_A_Bass':
Gn_A_Bass_page()
# Function to setup "Naik" mode
def setup_naik_mode(progres):
if progres == 1:
pages[0] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
elif progres == 2:
pages[1] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
elif progres == 3:
pages[2] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
display_pages(progres)
# Function to setup "Turun" mode
def setup_turun_mode(progres):
if progres == 1:
pages[0] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
elif progres == 2:
pages[1] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
elif progres == 3:
pages[2] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
display_pages(progres)
# Current mode for each toggle button
current_mode = ['naik', 'naik', 'naik']
toggle_btns = []
indicator_labels = []
pages = [
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass'],
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass'],
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
]
# Toggle Button 1
toggle_btn1 = tk.Button(head_frame, text="☰ _ P 1", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(1, current_mode))
toggle_btn1.pack(side=tk.LEFT)
toggle_btn1.place(x=5, y=20)
toggle_btns.append(toggle_btn1)
title_lb1 = tk.Label(head_frame, text="Progres-1", bg="#158aff", fg="white", font=("bold", 7))
title_lb1.pack(side=tk.LEFT)
title_lb1.place(x=108, y=11)
indicator_label1 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label1.pack(side=tk.LEFT)
indicator_label1.place(x=127, y=55)
indicator_labels.append(indicator_label1)
# Toggle Button 2
toggle_btn2 = tk.Button(head_frame, text="☰ _ P 2", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(2, current_mode))
toggle_btn2.pack(side=tk.LEFT)
toggle_btn2.place(x=232, y=20)
toggle_btns.append(toggle_btn2)
title_lb2 = tk.Label(head_frame, text="Progres-2", bg="#158aff", fg="white", font=("bold", 7))
title_lb2.pack(side=tk.LEFT)
title_lb2.place(x=334, y=5)
indicator_label2 =
Setelah title_lb2=, lanjutkan kodenya hingga selesai.
Berikut kelanjutan dari kode tersebut:
```python
# Lanjutkan dari title_lb2 =
title_lb2 = tk.Label(head_frame, text="Progres-2", bg="#158aff", fg="white", font=("bold", 7))
title_lb2.pack(side=tk.LEFT)
title_lb2.place(x=334, y=5)
indicator_label2 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label2.pack(side=tk.LEFT)
indicator_label2.place(x=368, y=55)
indicator_labels.append(indicator_label2)
# Toggle Button 3
toggle_btn3 = tk.Button(head_frame, text="☰ _ P 3", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(3, current_mode))
toggle_btn3.pack(side=tk.LEFT)
toggle_btn3.place(x=460, y=20)
toggle_btns.append(toggle_btn3)
title_lb3 = tk.Label(head_frame, text="Progres-3", bg="#158aff", fg="white", font=("bold", 7))
title_lb3.pack(side=tk.LEFT)
title_lb3.place(x=565, y=5)
indicator_label3 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label3.pack(side=tk.LEFT)
indicator_label3.place(x=599, y=55)
indicator_labels.append(indicator_label3)
head_frame.pack(side=tk.TOP, fill=tk.X)
head_frame.pack_propagate(False)
head_frame.configure(height=120)
# Options Frame
options_frame = tk.Frame(root, bg="#c3c3c3")
options_frame.pack(side=tk.LEFT, fill=tk.Y)
# Function to highlight selected button
def highlight_button(button, indicator):
clear_highlight()
button.config(bg="grey")
indicator.config(bg="yellow")
def clear_highlight():
Gn_A_Trebel_btn.config(bg="grey")
Gn_A_Trebel_indicate.config(bg="#c3c3c3")
Gn_Biru_btn.config(bg="blue")
Gn_Biru_indicate.config(bg="#c3c3c3")
Gn_Cyan_Bawah_btn.config(bg="cyan")
Gn_Cyan_Bawah_indicate.config(bg="#c3c3c3")
Gn_Cyan_Atas_btn.config(bg="cyan")
Gn_Cyan_Atas_indicate.config(bg="#c3c3c3")
Gn_A_Bass_btn.config(bg="grey")
Gn_A_Bass_indicate.config(bg="#c3c3c3")
# Function to display Gn_A_Trebel page
def Gn_A_Trebel_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_A_Trebel", font=("Helvetica", 10)).pack()
# Function to display Gn_Biru page
def Gn_Biru_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Biru", font=("Helvetica", 10)).pack()
# Function to display Gn_Cyan_Bawah page
def Gn_Cyan_Bawah_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Cyan_Bawah", font=("Helvetica", 10)).pack()
# Function to display Gn_Cyan_Atas page
def Gn_Cyan_Atas_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Cyan_Atas", font=("Helvetica", 10)).pack()
# Function to display Gn_A_Bass page
def Gn_A_Bass_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_A_Bass", font=("Helvetica", 10)).pack()
# Button and Indicator for Gn_A_Trebel
Gn_A_Trebel_btn = tk.Button(options_frame, text="Gn_A_Trebel", font=("bold", 7), fg="yellow", bd=0, bg="grey", command=lambda: [highlight_button(Gn_A_Trebel_btn, Gn_A_Trebel_indicate), Gn_A_Trebel_page()])
Gn_A_Trebel_btn.place(x=3, y=50)
Gn_A_Trebel_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Trebel_indicate.place(x=2, y=55, width=5, height=45)
# Button and Indicator for Gn_Biru
Gn_Biru_btn = tk.Button(options_frame, text="Gn_Biru", font=("bold", 7), fg="yellow", bd=0, bg="blue", command=lambda: [highlight_button(Gn_Biru_btn, Gn_Biru_indicate), Gn_Biru_page()])
Gn_Biru_btn.place(x=3, y=100)
Gn_Biru_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Biru_indicate.place(x=2, y=105, width=5, height=45)
# Button and Indicator for Gn_Cyan_Bawah
Gn_Cyan_Bawah_btn = tk.Button(options_frame, text="Gn_Cyan_Bawah", font=("bold", 7), fg="yellow", bd=0, bg="cyan", command=lambda: [highlight_button(Gn_Cyan_Bawah_btn, Gn_Cyan_Bawah_indicate), Gn_Cyan_Bawah_page()])
Gn_Cyan_Bawah_btn.place(x=3, y=150)
Gn_Cyan_Bawah_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Bawah_indicate.place(x=2, y=155, width=5, height=45)
# Button and Indicator for Gn_Cyan_Atas
Gn_Cyan_Atas_btn = tk.Button(options_frame, text="Gn_Cyan_Atas", font=("bold", 7), fg="yellow", bd=0, bg="cyan", command=lambda: [highlight_button(Gn_Cyan_Atas_btn, Gn_Cyan_Atas_indicate), Gn_Cyan_Atas_page()])
Gn_Cyan_Atas_btn.place(x=3, y=200)
Gn_Cyan_Atas_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Atas_indicate.place(x=2, y=205, width=5, height=45)
# Button and Indicator for Gn_A_Bass
Gn_A_Bass_btn = tk.Button(options_frame, text="Gn_A_Bass", font=("bold", 7), fg="yellow", bd=0, bg="grey", command=lambda: [highlight_button(Gn_A_Bass_btn, Gn_A_Bass_indicate), Gn_A_Bass_page()])
Gn_A_Bass_btn.place(x=3, y=250)
Gn_A_Bass_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Bass_indicate.place(x=2, y=255, width=5, height=45)
# Main loop
root.mainloop()
```
Kode ini menyelesaikan implementasi tampilan halaman, navigasi, dan fungsionalitas toggle buttons yang mengatur mode serta menampilkan halaman sesuai dengan tombol yang dipilih di option frame.
Tombol di options_frame akan difungsikan untuk membuka file terkait dengan perintah (command)yang sesuai, contohnya command=run_gn_a_trebel_file untuk membuka file CRUD_Gn_A_Trebel.py adalah berikut:
Gn_A_Trebel_btn = tk.Button(options_frame, text="Gn_A_Trebel", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=run_gn_a_trebel_file)
Gn_A_Trebel_btn.place(x=3, y=50)
Kemudian dari penjelasan yang kamu berikan, maka kode telah berkembang menjadi berikut ini:
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import sqlite3
from tkinter import Tk, Label, Entry, Button, Frame, StringVar
# Function to initialize database
def init_db():
conn = sqlite3.connect("DATAMUSIK.db")
c = conn.cursor()
# Create Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Gn_Cyan_Atas (
ID INTEGER PRIMARY KEY,
Level_R TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
# Create Sub_Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Sub_Gn_Cyan_Atas (
Sub_ID INTEGER PRIMARY KEY,
Level_D TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
conn.commit()
conn.close()
# Initialize the main window
root = tk.Tk()
root.geometry("690x1390")
root.title("Arah Arus Musik")
# Head Frame
head_frame = tk.Frame(root, bg="#158aff", highlightbackground="white", highlightthickness=1)
head_frame.place()
# Content Frame
content_frame = tk.Frame(root)
content_frame.pack()
# Function to clear content frame
def clear_content_frame():
for widget in content_frame.winfo_children():
widget.destroy()
# Function to switch modes and update pages
def switch_mode(progres, mode):
if mode[progres - 1] == "naik":
mode[progres - 1] = "turun"
setup_turun_mode(progres)
else:
mode[progres - 1] = "naik"
setup_naik_mode(progres)
# Function to toggle menus
def toggle_menu(progres, mode):
if mode[progres - 1] == 'naik':
mode[progres - 1] = 'turun'
toggle_btns[progres - 1].config(text="X _ T")
pages[progres - 1] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
indicator_labels[progres - 1].config(text="Turun")
else:
mode[progres - 1] = 'naik'
toggle_btns[progres - 1].config(text="☰ _ N")
pages[progres - 1] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
indicator_labels[progres - 1].config(text="Naik")
display_pages(progres)
# Function to display pages
def display_pages(progres):
for widget in content_frame.winfo_children():
widget.destroy()
for page in pages[progres - 1]:
if page == 'Gn_Cyan_Bawah':
Gn_Cyan_Bawah_page()
elif page == 'Gn_A_Trebel':
Gn_A_Trebel_page()
elif page == 'Gn_Biru':
Gn_Biru_page()
elif page == 'Gn_Cyan_Atas':
Gn_Cyan_Atas_page()
# Function to setup "Naik" mode
def setup_naik_mode(progres):
if progres == 1:
pages[0] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
elif progres == 2:
pages[1] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
elif progres == 3:
pages[2] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
display_pages(progres)
# Function to setup "Turun" mode
def setup_turun_mode(progres):
if progres == 1:
pages[0] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
elif progres == 2:
pages[1] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
elif progres == 3:
pages[2] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
display_pages(progres)
# Current mode for each toggle button
current_mode = ['naik', 'naik', 'naik']
toggle_btns = []
indicator_labels = []
pages = [
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass'],
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass'],
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
]
# Toggle Button 1
toggle_btn1 = tk.Button(head_frame, text="☰ _ P 1", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(1, current_mode))
toggle_btn1.pack(side=tk.LEFT)
toggle_btn1.place(x=5, y=20)
toggle_btns.append(toggle_btn1)
title_lb1 = tk.Label(head_frame, text="Progres-1", bg="#158aff", fg="white", font=("bold", 7))
title_lb1.pack(side=tk.LEFT)
title_lb1.place(x=108, y=11)
indicator_label1 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label1.pack(side=tk.LEFT)
indicator_label1.place(x=127, y=55)
indicator_labels.append(indicator_label1)
# Toggle Button 2
toggle_btn2 = tk.Button(head_frame, text="☰ _ P 2", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(2, current_mode))
toggle_btn2.pack(side=tk.LEFT)
toggle_btn2.place(x=232, y=20)
toggle_btns.append(toggle_btn2)
title_lb2 = tk.Label(head_frame, text="Progres-2", bg="#158aff", fg="white", font=("bold", 7))
title_lb2.pack(side=tk.LEFT)
title_lb2.place(x=334, y=5)
indicator_label2 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label2.pack(side=tk.LEFT)
indicator_label2.place(x=368, y=55)
indicator_labels.append(indicator_label2)
# Toggle Button 3
toggle_btn3 = tk.Button(head_frame, text="☰ _ P 3", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(3, current_mode))
toggle_btn3.pack(side=tk.LEFT)
toggle_btn3.place(x=460, y=20)
toggle_btns.append(toggle_btn3)
title_lb3 = tk.Label(head_frame, text="Progres-3", bg="#158aff", fg="white", font=("bold", 7))
title_lb3.pack(side=tk.LEFT)
title_lb3.place(x=565, y=5)
indicator_label3 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label3.pack(side=tk.LEFT)
indicator_label3.place(x=599, y=55)
indicator_labels.append(indicator_label3)
head_frame.pack(side=tk.TOP, fill=tk.X)
head_frame.pack_propagate(False)
head_frame.configure(height=120)
# Options Frame
options_frame = tk.Frame(root, bg="#c3c3c3")
options_frame.place(x=0, y=50)
#options_frame.pack(side=tk.LEFT, fill=tk.X)
# Function to highlight selected button
def highlight_button(button, indicator):
clear_highlight()
button.config(bg="tomato")
indicator.config(bg="yellow")
def clear_highlight():
Gn_A_Trebel_btn.config(bg="grey")
Gn_A_Trebel_indicate.config(bg="#c3c3c3")
Gn_Biru_btn.config(bg="blue")
Gn_Biru_indicate.config(bg="#c3c3c3")
Gn_Cyan_Bawah_btn.config(bg="green")
Gn_Cyan_Bawah_indicate.config(bg="#c3c3c3")
Gn_Cyan_Atas_btn.config(bg="green")
Gn_Cyan_Atas_indicate.config(bg="#c3c3c3")
Gn_A_Bass_btn.config(bg="grey")
Gn_A_Bass_indicate.config(bg="#c3c3c3")
# Function to display Gn_A_Trebel page
def Gn_A_Trebel_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_A_Trebel", font=("Helvetica", 10)).pack()
# Function to display Gn_Biru page
def Gn_Biru_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Biru", font=("Helvetica", 10)).pack()
# Function to display Gn_Cyan_Bawah page
def Gn_Cyan_Bawah_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Cyan_Bawah", font=("Helvetica", 10)).pack()
# Function to display Gn_Cyan_Atas page
def Gn_Cyan_Atas_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Cyan_Atas", font=("Helvetica", 10)).pack()
# Function to display Gn_Biru page
def Gn_A_Bass_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_A_Bass", font=("Helvetica", 10)).pack()
# Button and Indicator for Gn_A_Trebel
Gn_A_Trebel_btn = tk.Button(options_frame, text="Gn_A_Trebel", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: [highlight_button(Gn_A_Trebel_btn, Gn_A_Trebel_indicate), Gn_A_Trebel_page()])
Gn_A_Trebel_btn.place(x=3, y=50)
Gn_A_Trebel_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Trebel_indicate.place(x=2, y=55, width=5, height=45)
# Button and Indicator for Gn_Biru
Gn_Biru_btn = tk.Button(options_frame, text="Gn_Biru", font=("bold", 7), fg="yellow", bd=0, bg="blue", command=lambda: [highlight_button(Gn_Biru_btn, Gn_Biru_indicate), Gn_Biru_page()])
Gn_Biru_btn.place(x=3, y=100)
Gn_Biru_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Biru_indicate.place(x=2, y=105, width=5, height=45)
# Button and Indicator for Gn_Cyan_Bawah
Gn_Cyan_Bawah_btn = tk.Button(options_frame, text="Gn_Cyan_Bawah", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: [highlight_button(Gn_Cyan_Bawah_btn, Gn_Cyan_Bawah_indicate), Gn_Cyan_Bawah_page()])
Gn_Cyan_Bawah_btn.place(x=3, y=150)
Gn_Cyan_Bawah_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Bawah_indicate.place(x=2, y=155, width=5, height=45)
# Button and Indicator for Gn_Cyan_Atas
Gn_Cyan_Atas_btn = tk.Button(options_frame, text="Gn_Cyan_Atas", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: [highlight_button(Gn_Cyan_Atas_btn, Gn_Cyan_Atas_indicate), Gn_Cyan_Atas_page()])
Gn_Cyan_Atas_btn.place(x=3, y=200)
Gn_Cyan_Atas_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Atas_indicate.place(x=2, y=205, width=5, height=45)
# Button and Indicator for Gn_A_Bass
Gn_A_Bass_btn = tk.Button(options_frame, text="Gn_A_Bass", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: [highlight_button(Gn_A_Bass_btn, Gn_A_Bass_indicate), Gn_A_Bass_page()])
Gn_A_Bass_btn.place(x=3, y=250)
Gn_A_Bass_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Bass_indicate.place(x=2, y=255, width=5, height=45)
options_frame.pack(side=tk.LEFT)
options_frame.pack_propagate(False)
options_frame.configure(width=170, height=300)
# Initialize the application with the first progress setup
setup_naik_mode(1)
# Initialize database
init_db()
# Start the main loop
root.mainloop()
Gn_A_Trebel_btn = tk.Button(options_frame, text="Gn_A_Trebel", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: [Gn_A_Trebel_page(), highlight_button(Gn_A_Trebel_btn, Gn_A_Trebel_indicate)])
Gn_A_Trebel_btn.place(x=3, y=50)
Gn_A_Trebel_indicate = tk.Label(options_frame, text="", bg="#c3c3c3")
Gn_A_Trebel_indicate.place(x=3, y=50, width=10, height=25)
Gn_Biru_btn = tk.Button(options_frame, text="Gn_Biru", font=("bold", 6), fg="white", bd=0, bg="blue", command=lambda: [Gn_Biru_page(), highlight_button(Gn_Biru_btn, Gn_Biru_indicate)])
Gn_Biru_btn.place(x=3, y=90)
Gn_Biru_indicate = tk.Label(options_frame, text="", bg="#c3c3c3")
Gn_Biru_indicate.place(x=3, y=90, width=10, height=25)
Gn_Cyan_Bawah_btn = tk.Button(options_frame, text="Gn_Cyan_Bawah", font=("bold", 6), fg="white", bd=0, bg="green", command=lambda: [Gn_Cyan_Bawah_page(), highlight_button(Gn_Cyan_Bawah_btn, Gn_Cyan_Bawah_indicate)])
Gn_Cyan_Bawah_btn.place(x=3, y=130)
Gn_Cyan_Bawah_indicate = tk.Label(options_frame, text="", bg="#c3c3c3")
Gn_Cyan_Bawah_indicate.place(x=3, y=130, width=10, height=25)
Gn_Cyan_Atas_btn = tk.Button(options_frame, text="Gn_Cyan_Atas", font=("bold", 6), fg="white", bd=0, bg="green", command=lambda: [Gn_Cyan_Atas_page(), highlight_button(Gn_Cyan_Atas_btn, Gn_Cyan_Atas_indicate)])
Gn_Cyan_Atas_btn.place(x=3, y=170)
Gn_Cyan_Atas_indicate = tk.Label(options_frame, text="", bg="#c3c3c3")
Gn_Cyan_Atas_indicate.place(x=3, y=170, width=10, height=25)
Gn_A_Bass_btn = tk.Button(options_frame, text="Gn_A_Bass", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: [Gn_A_Bass_page(), highlight_button(Gn_A_Bass_btn, Gn_A_Bass_indicate)])
Gn_A_Bass_btn.place(x=3, y=210)
Gn_A_Bass_indicate = tk.Label(options_frame, text="", bg="#c3c3c3")
Gn_A_Bass_indicate.place(x=3, y=210, width=10, height=25)
# Initialize database and run the main loop
init_db()
root.mainloop()
Tolong modifikasi kode, agar supaya ketika saat pertama kali applikasi dijalankan (default) maka tombol yang tampil pada options_frame dengan susunan dari atas ke bawah adalah Gn_A_Bass_btn, Gn_Biru_btn, Gn_Cyan_Atas_btn, Gn_Cyan_Bawah_btn, Gn_A_Trebel_btn, dan teks yang tampil tertulis adalah "Halaman Default Gugus Nada". Kemudian ketika tombol di toggle_menu ditekan baik toggle_btn1, 2, atau 3 di mode Naik maka teks yang tampil tertulis adalah "Ini adalah Halaman Gn_Cyan_Atas", begitu juga ketika mode Turun maka teks yang tampil tertulis sama adalah "Ini adalah Halaman Gn_Cyan_Atas".
Tetapi ketika tombol di toggle_menu ditekan di mode Naik maka seharusnya tombol di options_frame yang tampil adalah Gn_Cyan_Atas_btn, Gn_Biru_btn, Gn_A_Bass, sedangkan di mode Turun maka seharusnya tombol di options_frame yang tampil adalah Gn_Cyan_Atas_btn, Gn_Cyan_Bawah_btn, Gn_A_Trebel_btn.
Untuk memodifikasi kode agar memenuhi kebutuhan tersebut, berikut adalah langkah-langkah yang harus dilakukan:
1. **Mendefinisikan Susunan Tombol Default dan Mengatur Teks Default**:
- Saat pertama kali aplikasi dijalankan, tombol yang muncul dalam `options_frame` adalah: `Gn_A_Bass_btn`, `Gn_Biru_btn`, `Gn_Cyan_Atas_btn`, `Gn_Cyan_Bawah_btn`, dan `Gn_A_Trebel_btn`.
- Teks yang tampil tertulis adalah "Halaman Default Gugus Nada".
2. **Mengatur Tampilan Tombol Berdasarkan Toggle Mode**:
- Ketika tombol di `toggle_menu` ditekan di mode **Naik**, tombol yang muncul adalah: `Gn_Cyan_Atas_btn`, `Gn_Biru_btn`, `Gn_A_Bass_btn`, dan teks yang tampil tertulis "Ini adalah Halaman Gn_Cyan_Atas".
- Ketika tombol di `toggle_menu` ditekan di mode **Turun**, tombol yang muncul adalah: `Gn_Cyan_Atas_btn`, `Gn_Cyan_Bawah_btn`, `Gn_A_Trebel_btn`, dan teks yang tampil tertulis "Ini adalah Halaman Gn_Cyan_Atas".
### Contoh Modifikasi Kode:
```python
import tkinter as tk
from tkinter import ttk
def update_options_frame(toggle_mode=None):
# Clear all buttons from options_frame
for widget in options_frame.winfo_children():
widget.grid_forget()
# Default state
if toggle_mode is None:
Gn_A_Bass_btn.grid(row=0, column=0, pady=5)
Gn_Biru_btn.grid(row=1, column=0, pady=5)
Gn_Cyan_Atas_btn.grid(row=2, column=0, pady=5)
Gn_Cyan_Bawah_btn.grid(row=3, column=0, pady=5)
Gn_A_Trebel_btn.grid(row=4, column=0, pady=5)
status_label.config(text="Halaman Default Gugus Nada")
elif toggle_mode == 'Naik':
Gn_Cyan_Atas_btn.grid(row=0, column=0, pady=5)
Gn_Biru_btn.grid(row=1, column=0, pady=5)
Gn_A_Bass_btn.grid(row=2, column=0, pady=5)
status_label.config(text="Ini adalah Halaman Gn_Cyan_Atas")
elif toggle_mode == 'Turun':
Gn_Cyan_Atas_btn.grid(row=0, column=0, pady=5)
Gn_Cyan_Bawah_btn.grid(row=1, column=0, pady=5)
Gn_A_Trebel_btn.grid(row=2, column=0, pady=5)
status_label.config(text="Ini adalah Halaman Gn_Cyan_Atas")
def on_toggle_press(toggle_mode):
update_options_frame(toggle_mode)
root = tk.Tk()
root.title("Gugus Nada Application")
# Frame for toggle buttons
toggle_frame = tk.Frame(root)
toggle_frame.pack(side=tk.TOP, fill=tk.X)
toggle_btn1 = tk.Button(toggle_frame, text="Toggle 1", command=lambda: on_toggle_press('Naik'))
toggle_btn2 = tk.Button(toggle_frame, text="Toggle 2", command=lambda: on_toggle_press('Naik'))
toggle_btn3 = tk.Button(toggle_frame, text="Toggle 3", command=lambda: on_toggle_press('Turun'))
toggle_btn1.pack(side=tk.LEFT, padx=5, pady=5)
toggle_btn2.pack(side=tk.LEFT, padx=5, pady=5)
toggle_btn3.pack(side=tk.LEFT, padx=5, pady=5)
# Frame for options buttons
options_frame = tk.Frame(root)
options_frame.pack(side=tk.TOP, fill=tk.X)
Gn_A_Bass_btn = tk.Button(options_frame, text="Gn_A_Bass")
Gn_Biru_btn = tk.Button(options_frame, text="Gn_Biru")
Gn_Cyan_Atas_btn = tk.Button(options_frame, text="Gn_Cyan_Atas")
Gn_Cyan_Bawah_btn = tk.Button(options_frame, text="Gn_Cyan_Bawah")
Gn_A_Trebel_btn = tk.Button(options_frame, text="Gn_A_Trebel")
# Label to display the current status
status_label = tk.Label(root, text="Halaman Default Gugus Nada", font=('Arial', 14))
status_label.pack(side=tk.TOP, pady=10)
# Initialize with the default layout
update_options_frame()
root.mainloop()
```
### Penjelasan:
- **`update_options_frame`**: Fungsi ini akan memodifikasi tampilan tombol dalam `options_frame` berdasarkan mode yang dipilih (`Naik`, `Turun`, atau default). Ketika aplikasi dijalankan, fungsi ini dipanggil tanpa parameter untuk memunculkan susunan tombol default.
- **`on_toggle_press`**: Fungsi ini akan dipanggil ketika salah satu toggle button ditekan, dan akan mengubah tampilan `options_frame` sesuai mode yang dipilih (`Naik` atau `Turun`).
- **Label Status**: Teks di label (`status_label`) akan berubah sesuai dengan mode yang aktif.
Dengan menggunakan kode ini, ketika aplikasi pertama kali dijalankan, susunan tombol akan sesuai dengan kebutuhan, dan setiap kali tombol toggle ditekan, susunan tombol dan teks status akan berubah sesuai logika yang diinginkan.
Jadi kode yang benar adalah ketika skrip dijalankan secara langsung, maka akan memanggil fungsi `init_default_page(). Semua tombol di options_frame diatur tata letaknya dalam bentuk "place" ketika sedang tampil default atau telah mengalami update, sehingga kode berkembang menjadi seperti berikut ini:
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import sqlite3
from tkinter import Tk, Label, Entry, Button, Frame, StringVar
# Function to initialize database
def init_db():
conn = sqlite3.connect("DATAMUSIK.db")
c = conn.cursor()
# Create Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Gn_Cyan_Atas (
ID INTEGER PRIMARY KEY,
Level_R TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
# Create Sub_Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Sub_Gn_Cyan_Atas (
Sub_ID INTEGER PRIMARY KEY,
Level_D TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
conn.commit()
conn.close()
# Initialize the main window
root = tk.Tk()
root.geometry("690x1390")
root.title("Arah Arus Musik")
# Head Frame
head_frame = tk.Frame(root, bg="#158aff", highlightbackground="white", highlightthickness=1)
head_frame.place()
# Content Frame
content_frame = tk.Frame(root)
content_frame.pack()
# Function to clear content frame
def clear_content_frame():
for widget in content_frame.winfo_children():
widget.destroy()
# Function to switch modes and update pages
def switch_mode(progres, mode):
if mode[progres - 1] == "naik":
mode[progres - 1] = "turun"
setup_turun_mode(progres)
else:
mode[progres - 1] = "naik"
setup_naik_mode(progres)
# Current mode for each toggle button
current_mode = ['naik', 'naik', 'naik']
toggle_btns = []
indicator_labels = []
pages = [
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass'],
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass'],
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
]
# Function to toggle menus
def toggle_menu(progres, mode):
if mode[progres - 1] == 'naik':
mode[progres - 1] = 'turun'
toggle_btns[progres - 1].config(text="X _ T")
pages[progres - 1] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
indicator_labels[progres - 1].config(text="Turun")
else:
mode[progres - 1] = 'naik'
toggle_btns[progres - 1].config(text="☰ _ N")
pages[progres - 1] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
indicator_labels[progres - 1].config(text="Naik")
update_options_frame(progres)
display_pages(progres)
# Function to display pages
def display_pages(progres):
for widget in content_frame.winfo_children():
widget.destroy()
display_gn_cyan_atas_page()
# Function to display default page
def display_gn_cyan_atas_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah Halaman Gn_Cyan_Atas", font=("Helvetica", 10)).pack()
# Function to setup "Naik" mode
def setup_naik_mode(progres):
if progres == 1:
pages[0] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
elif progres == 2:
pages[1] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
elif progres == 3:
pages[2] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
display_pages(progres)
# Function to setup "Turun" mode
def setup_turun_mode(progres):
if progres == 1:
pages[0] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
elif progres == 2:
pages[1] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
elif progres == 3:
pages[2] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
display_pages(progres)
# Toggle Button 1
toggle_btn1 = tk.Button(head_frame, text="☰ _ P 1", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(1, current_mode))
toggle_btn1.pack(side=tk.LEFT)
toggle_btn1.place(x=5, y=20)
toggle_btns.append(toggle_btn1)
title_lb1 = tk.Label(head_frame, text="Progres-1", bg="#158aff", fg="white", font=("bold", 7))
title_lb1.pack(side=tk.LEFT)
title_lb1.place(x=108, y=11)
indicator_label1 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label1.pack(side=tk.LEFT)
indicator_label1.place(x=127, y=55)
indicator_labels.append(indicator_label1)
# Toggle Button 2
toggle_btn2 = tk.Button(head_frame, text="☰ _ P 2", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(2, current_mode))
toggle_btn2.pack(side=tk.LEFT)
toggle_btn2.place(x=232, y=20)
toggle_btns.append(toggle_btn2)
title_lb2 = tk.Label(head_frame, text="Progres-2", bg="#158aff", fg="white", font=("bold", 7))
title_lb2.pack(side=tk.LEFT)
title_lb2.place(x=334, y=5)
indicator_label2 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label2.pack(side=tk.LEFT)
indicator_label2.place(x=368, y=55)
indicator_labels.append(indicator_label2)
# Toggle Button 3
toggle_btn3 = tk.Button(head_frame, text="☰ _ P 3", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(3, current_mode))
toggle_btn3.pack(side=tk.LEFT)
toggle_btn3.place(x=460, y=20)
toggle_btns.append(toggle_btn3)
title_lb3 = tk.Label(head_frame, text="Progres-3", bg="#158aff", fg="white", font=("bold", 7))
title_lb3.pack(side=tk.LEFT)
title_lb3.place(x=565, y=5)
indicator_label3 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label3.pack(side=tk.LEFT)
indicator_label3.place(x=599, y=55)
indicator_labels.append(indicator_label3)
head_frame.pack(side=tk.TOP, fill=tk.X)
head_frame.pack_propagate(False)
head_frame.configure(height=120)
# Options Frame
options_frame = tk.Frame(root, bg="#c3c3c3")
options_frame.place(x=0, y=50)
#options_frame.pack(side=tk.LEFT, fill=tk.X)
# Function to highlight selected button
def highlight_button(button, indicator):
clear_highlight()
button.config(bg="tomato")
indicator.config(bg="yellow")
def clear_highlight():
Gn_A_Trebel_btn.config(bg="grey")
Gn_A_Trebel_indicate.config(bg="#c3c3c3")
Gn_Biru_btn.config(bg="blue")
Gn_Biru_indicate.config(bg="#c3c3c3")
Gn_Cyan_Bawah_btn.config(bg="green")
Gn_Cyan_Bawah_indicate.config(bg="#c3c3c3")
Gn_Cyan_Atas_btn.config(bg="green")
Gn_Cyan_Atas_indicate.config(bg="#c3c3c3")
Gn_A_Bass_btn.config(bg="grey")
Gn_A_Bass_indicate.config(bg="#c3c3c3")
# Function to display the options based on the current mode and progress
def update_options_frame(progres):
# Clear existing buttons
for widget in options_frame.winfo_children():
widget.destroy()
if current_mode[progres - 1] == "turun":
# Button and Indicator for Gn_Cyan_Atas
Gn_Cyan_Atas_btn = tk.Button(options_frame, text="Gn_Cyan_Atas", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: [highlight_button(Gn_Cyan_Atas_btn, Gn_Cyan_Atas_indicate), Gn_Cyan_Atas_page()])
Gn_Cyan_Atas_btn.place(x=3, y=0)
Gn_Cyan_Atas_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Atas_indicate.place(x=2, y=5, width=5, height=45)
# Button and Indicator for Gn_Cyan_Bawah
Gn_Cyan_Bawah_btn = tk.Button(options_frame, text="Gn_Cyan_Bawah", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: [highlight_button(Gn_Cyan_Bawah_btn, Gn_Cyan_Bawah_indicate), Gn_Cyan_Bawah_page()])
Gn_Cyan_Bawah_btn.place(x=3, y=50)
Gn_Cyan_Bawah_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Bawah_indicate.place(x=2, y=55, width=5, height=45)
# Button and Indicator for Gn_A_Trebel
Gn_A_Trebel_btn = tk.Button(options_frame, text="Gn_A_Trebel", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: [highlight_button(Gn_A_Trebel_btn, Gn_A_Trebel_indicate), Gn_A_Trebel_page()])
Gn_A_Trebel_btn.place(x=3, y=100)
Gn_A_Trebel_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Trebel_indicate.place(x=2, y=105, width=5, height=45)
else: # Mode "naik"
Gn_Cyan_Atas_btn = tk.Button(options_frame, text="Gn_Cyan_Atas", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: [highlight_button(Gn_Cyan_Atas_btn, Gn_Cyan_Atas_indicate), Gn_Cyan_Atas_page()])
Gn_Cyan_Atas_btn.place(x=3, y=0)
Gn_Cyan_Atas_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Atas_indicate.place(x=2, y=5, width=5, height=45)
# Button and Indicator for Gn_Biru
Gn_Biru_btn = tk.Button(options_frame, text="Gn_Biru", font=("bold", 7), fg="yellow", bd=0, bg="blue", command=lambda: [highlight_button(Gn_Biru_btn, Gn_Biru_indicate), Gn_Biru_page()])
Gn_Biru_btn.place(x=3, y=50)
Gn_Biru_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Biru_indicate.place(x=2, y=55, width=5, height=45)
Gn_A_Bass_btn = tk.Button(options_frame, text="Gn_A_Bass", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: [highlight_button(Gn_A_Bass_btn, Gn_A_Bass_indicate), Gn_A_Bass_page()])
Gn_A_Bass_btn.place(x=3, y=100)
Gn_A_Bass_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Bass_indicate.place(x=2, y=105, width=5, height=45)
# Call update_options_frame for the initial setup
update_options_frame(1)
# Function to display Gn_A_Trebel page
def Gn_A_Trebel_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_A_Trebel", font=("Helvetica", 10)).pack()
# Function to display Gn_Biru page
def Gn_Biru_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Biru", font=("Helvetica", 10)).pack()
# Function to display Gn_Cyan_Bawah page
def Gn_Cyan_Bawah_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Cyan_Bawah", font=("Helvetica", 10)).pack()
# Function to display Gn_Cyan_Atas page
def Gn_Cyan_Atas_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Cyan_Atas", font=("Helvetica", 10)).pack()
# Function to display Gn_Biru page
def Gn_A_Bass_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_A_Bass", font=("Helvetica", 10)).pack()
# Initialize the default page
def init_default_page():
update_options_frame(1) # Default to showing options for Progres-1
clear_content_frame()
tk.Label(content_frame, text="Halaman Default Gugus Nada", font=("Helvetica", 10)).pack()
#Fungsi tombol di options_frame
# Button and Indicator for Gn_A_Bass
Gn_A_Bass_btn = tk.Button(options_frame, text="Gn_A_Bass", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: [highlight_button(Gn_A_Bass_btn, Gn_A_Bass_indicate), Gn_A_Bass_page()])
Gn_A_Bass_btn.place(x=3, y=0)
Gn_A_Bass_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Bass_indicate.place(x=2, y=5, width=5, height=45)
# Button and Indicator for Gn_Biru
Gn_Biru_btn = tk.Button(options_frame, text="Gn_Biru", font=("bold", 7), fg="yellow", bd=0, bg="blue", command=lambda: [highlight_button(Gn_Biru_btn, Gn_Biru_indicate), Gn_Biru_page()])
Gn_Biru_btn.place(x=3, y=50)
Gn_Biru_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Biru_indicate.place(x=2, y=55, width=5, height=45)
# Button and Indicator for Gn_Cyan_Atas
Gn_Cyan_Atas_btn = tk.Button(options_frame, text="Gn_Cyan_Atas", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: [highlight_button(Gn_Cyan_Atas_btn, Gn_Cyan_Atas_indicate), Gn_Cyan_Atas_page()])
Gn_Cyan_Atas_btn.place(x=3, y=100)
Gn_Cyan_Atas_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Atas_indicate.place(x=2, y=105, width=5, height=45)
# Button and Indicator for Gn_Cyan_Bawah
Gn_Cyan_Bawah_btn = tk.Button(options_frame, text="Gn_Cyan_Bawah", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: [highlight_button(Gn_Cyan_Bawah_btn, Gn_Cyan_Bawah_indicate), Gn_Cyan_Bawah_page()])
Gn_Cyan_Bawah_btn.place(x=3, y=150)
Gn_Cyan_Bawah_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Bawah_indicate.place(x=2, y=155, width=5, height=45)
# Button and Indicator for Gn_A_Trebel
Gn_A_Trebel_btn = tk.Button(options_frame, text="Gn_A_Trebel", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: [highlight_button(Gn_A_Trebel_btn, Gn_A_Trebel_indicate), Gn_A_Trebel_page()])
Gn_A_Trebel_btn.place(x=3, y=200)
Gn_A_Trebel_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Trebel_indicate.place(x=2, y=205, width=5, height=45)
options_frame.pack(side=tk.LEFT)
options_frame.pack_propagate(False)
options_frame.configure(width=170, height=300)
# Initialize the application with the first progress setup
setup_naik_mode(1)
# Initialize database
init_db()
# Main Function
if __name__ == "__main__":
init_default_page()
root.mainloop()
Tampaknya kode Anda hampir selesai, namun terpotong di bagian akhir. Berikut adalah kelanjutan dari kode tersebut untuk memastikan bahwa seluruh fungsionalitas yang diinginkan berjalan dengan baik:
```python
Gn_Cyan_Bawah_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Bawah_indicate.place(x=2, y=55, width=5, height=45)
# Button and Indicator for Gn_Biru
Gn_Biru_btn = tk.Button(options_frame, text="Gn_Biru", font=("bold", 5), fg="yellow", bd=0, bg="blue", command=lambda: [highlight_button(Gn_Biru_btn, Gn_Biru_indicate), Gn_Biru_page()])
Gn_Biru_btn.place(x=3, y=100)
Gn_Biru_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Biru_indicate.place(x=2, y=105, width=5, height=45)
# Button and Indicator for Gn_A_Bass
Gn_A_Bass_btn = tk.Button(options_frame, text="Gn_A_Bass", font=("bold", 5), fg="yellow", bd=0, bg="grey", command=lambda: [highlight_button(Gn_A_Bass_btn, Gn_A_Bass_indicate), Gn_A_Bass_page()])
Gn_A_Bass_btn.place(x=3, y=150)
Gn_A_Bass_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Bass_indicate.place(x=2, y=155, width=5, height=45)
elif current_mode[progres - 1] == "naik":
# Button and Indicator for Gn_Cyan_Atas
Gn_Cyan_Atas_btn = tk.Button(options_frame, text="Gn_Cyan_Atas", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: [highlight_button(Gn_Cyan_Atas_btn, Gn_Cyan_Atas_indicate), Gn_Cyan_Atas_page()])
Gn_Cyan_Atas_btn.place(x=3, y=0)
Gn_Cyan_Atas_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Atas_indicate.place(x=2, y=5, width=5, height=45)
# Button and Indicator for Gn_A_Trebel
Gn_A_Trebel_btn = tk.Button(options_frame, text="Gn_A_Trebel", font=("bold", 5), fg="yellow", bd=0, bg="grey", command=lambda: [highlight_button(Gn_A_Trebel_btn, Gn_A_Trebel_indicate), Gn_A_Trebel_page()])
Gn_A_Trebel_btn.place(x=3, y=50)
Gn_A_Trebel_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Trebel_indicate.place(x=2, y=55, width=5, height=45)
# Button and Indicator for Gn_Biru
Gn_Biru_btn = tk.Button(options_frame, text="Gn_Biru", font=("bold", 5), fg="yellow", bd=0, bg="blue", command=lambda: [highlight_button(Gn_Biru_btn, Gn_Biru_indicate), Gn_Biru_page()])
Gn_Biru_btn.place(x=3, y=100)
Gn_Biru_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Biru_indicate.place(x=2, y=105, width=5, height=45)
# Button and Indicator for Gn_A_Bass
Gn_A_Bass_btn = tk.Button(options_frame, text="Gn_A_Bass", font=("bold", 5), fg="yellow", bd=0, bg="grey", command=lambda: [highlight_button(Gn_A_Bass_btn, Gn_A_Bass_indicate), Gn_A_Bass_page()])
Gn_A_Bass_btn.place(x=3, y=150)
Gn_A_Bass_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Bass_indicate.place(x=2, y=155, width=5, height=45)
# Functions to Display Pages (Placeholder for actual logic)
def Gn_Cyan_Atas_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah Halaman Gn_Cyan_Atas", font=("Helvetica", 10)).pack()
def Gn_Cyan_Bawah_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah Halaman Gn_Cyan_Bawah", font=("Helvetica", 10)).pack()
def Gn_Biru_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah Halaman Gn_Biru", font=("Helvetica", 10)).pack()
def Gn_A_Bass_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah Halaman Gn_A_Bass", font=("Helvetica", 10)).pack()
def Gn_A_Trebel_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah Halaman Gn_A_Trebel", font=("Helvetica", 10)).pack()
# Initialize the application with the default page
display_gn_cyan_atas_page()
root.mainloop()
```
**Penjelasan:**
1. **`toggle_menu(progres, mode)`**:
- Fungsi ini mengubah mode antara "naik" dan "turun" dan memperbarui tombol serta label yang sesuai.
2. **`update_options_frame(progres)`**:
- Fungsi ini mengatur ulang tombol-tombol dalam `options_frame` berdasarkan mode saat ini (`naik` atau `turun`).
3. **Highlight dan Indikator:**
- Setiap tombol di `options_frame` memiliki fungsi `highlight_button()` untuk menyorot tombol yang dipilih dan indikator di sebelahnya.
4. **Display Pages:**
- Fungsi `Gn_Cyan_Atas_page()`, `Gn_Cyan_Bawah_page()`, `Gn_Biru_page()`, `Gn_A_Bass_page()`, dan `Gn_A_Trebel_page()` adalah placeholder untuk menampilkan halaman yang sesuai.
5. **Inisialisasi Awal:**
- `display_gn_cyan_atas_page()` diinisialisasi ketika aplikasi pertama kali dibuka untuk menampilkan halaman default.
Dengan setup ini, aplikasi Anda akan menampilkan halaman yang sesuai berdasarkan mode dan progres yang dipilih oleh pengguna. Tombol-tombol akan ditata secara dinamis dalam `options_frame`, dan setiap halaman akan dimuat dengan benar ketika dipilih.
Sudah saya tambahkan fungsi pemanggilan halaman gn_cyan_atas, yaitu: display_gn_cyan_atas_page().
Kemudian pada kode saya tambahkan fungsi untuk membuka dan menjalankan file CRUD_Gn_A_Trebel.py, CRUD_Gn_Biru.py, CRUD_Gn_Cyan_Bawah.py, CRUD_Gn_Cysn_Atas.py, dan CRUD_Gn_A_Bass.py, agar supaya ketika sebuah tombol di options_frame ditekan maka akan membuka file yang sesuai terkait dengan fungsi tombol tersebut, sehingga kodenya menjadi berikut ini: Tetapi tolong perbaiki kode pada fungsi lambda pada semua tombol di options_frame. Berikut kodenya:
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import sqlite3
from tkinter import Tk, Label, Entry, Button, Frame, StringVar
# Function to initialize database
def init_db():
conn = sqlite3.connect("DATAMUSIK.db")
c = conn.cursor()
# Create Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Gn_Cyan_Atas (
ID INTEGER PRIMARY KEY,
Level_R TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
# Create Sub_Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Sub_Gn_Cyan_Atas (
Sub_ID INTEGER PRIMARY KEY,
Level_D TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
conn.commit()
conn.close()
# Initialize the main window
root = tk.Tk()
root.geometry("690x1390")
root.title("Arah Arus Musik")
# Head Frame
head_frame = tk.Frame(root, bg="#158aff", highlightbackground="white", highlightthickness=1)
head_frame.place()
# Content Frame
content_frame = tk.Frame(root)
content_frame.pack()
# Function to clear content frame
def clear_content_frame():
for widget in content_frame.winfo_children():
widget.destroy()
# Function to switch modes and update pages
def switch_mode(progres, mode):
if mode[progres - 1] == "naik":
mode[progres - 1] = "turun"
setup_turun_mode(progres)
else:
mode[progres - 1] = "naik"
setup_naik_mode(progres)
# Current mode for each toggle button
current_mode = ['naik', 'naik', 'naik']
toggle_btns = []
indicator_labels = []
pages = [
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass'],
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass'],
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
]
# Function to toggle menus
def toggle_menu(progres, mode):
if mode[progres - 1] == 'naik':
mode[progres - 1] = 'turun'
toggle_btns[progres - 1].config(text="X _ T")
pages[progres - 1] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
indicator_labels[progres - 1].config(text="Turun")
else:
mode[progres - 1] = 'naik'
toggle_btns[progres - 1].config(text="☰ _ N")
pages[progres - 1] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
indicator_labels[progres - 1].config(text="Naik")
update_options_frame(progres)
display_pages(progres)
# Function to display pages
def display_pages(progres):
for widget in content_frame.winfo_children():
widget.destroy()
display_gn_cyan_atas_page()
# Function to display default page
def display_gn_cyan_atas_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah Halaman Gn_Cyan_Atas", font=("Helvetica", 10)).pack()
# Function to setup "Naik" mode
def setup_naik_mode(progres):
if progres == 1:
pages[0] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
elif progres == 2:
pages[1] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
elif progres == 3:
pages[2] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
display_pages(progres)
# Function to setup "Turun" mode
def setup_turun_mode(progres):
if progres == 1:
pages[0] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
elif progres == 2:
pages[1] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
elif progres == 3:
pages[2] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
display_pages(progres)
# Toggle Button 1
toggle_btn1 = tk.Button(head_frame, text="☰ _ P 1", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(1, current_mode))
toggle_btn1.pack(side=tk.LEFT)
toggle_btn1.place(x=5, y=20)
toggle_btns.append(toggle_btn1)
title_lb1 = tk.Label(head_frame, text="Progres-1", bg="#158aff", fg="white", font=("bold", 7))
title_lb1.pack(side=tk.LEFT)
title_lb1.place(x=108, y=11)
indicator_label1 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label1.pack(side=tk.LEFT)
indicator_label1.place(x=127, y=55)
indicator_labels.append(indicator_label1)
# Toggle Button 2
toggle_btn2 = tk.Button(head_frame, text="☰ _ P 2", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(2, current_mode))
toggle_btn2.pack(side=tk.LEFT)
toggle_btn2.place(x=232, y=20)
toggle_btns.append(toggle_btn2)
title_lb2 = tk.Label(head_frame, text="Progres-2", bg="#158aff", fg="white", font=("bold", 7))
title_lb2.pack(side=tk.LEFT)
title_lb2.place(x=334, y=5)
indicator_label2 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label2.pack(side=tk.LEFT)
indicator_label2.place(x=368, y=55)
indicator_labels.append(indicator_label2)
# Toggle Button 3
toggle_btn3 = tk.Button(head_frame, text="☰ _ P 3", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(3, current_mode))
toggle_btn3.pack(side=tk.LEFT)
toggle_btn3.place(x=460, y=20)
toggle_btns.append(toggle_btn3)
title_lb3 = tk.Label(head_frame, text="Progres-3", bg="#158aff", fg="white", font=("bold", 7))
title_lb3.pack(side=tk.LEFT)
title_lb3.place(x=565, y=5)
indicator_label3 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label3.pack(side=tk.LEFT)
indicator_label3.place(x=599, y=55)
indicator_labels.append(indicator_label3)
head_frame.pack(side=tk.TOP, fill=tk.X)
head_frame.pack_propagate(False)
head_frame.configure(height=120)
# Options Frame
options_frame = tk.Frame(root, bg="#c3c3c3")
options_frame.place(x=0, y=50)
#options_frame.pack(side=tk.LEFT, fill=tk.X)
# Fungsi untuk membuka file CRUD_Gn_Cyan_Bawah(gabung).py
active_page = None # Menyimpan halaman aktif
# Fungsi untuk menjalankan file CRUD_Gn_A_Trebel.py, CRUD_Gn_Biru.py, CRUD_Gn_Cyan_Bawah.py, CRUD_Gn_Cysn_Atas.py, dan CRUD_Gn_A_Bass.py
def run_gn_a_trebel_file():
filepath = "CRUD_Gn_A_Trebel.py"
try:
with open(filepath, 'r') as file:
exec(file.read(), globals())
messagebox.showinfo("Success", "File executed successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to execute file: {e}")
def run_gn_biru_file():
filepath = "CRUD_Gn_Biru.py"
try:
with open(filepath, 'r') as file:
exec(file.read(), globals())
messagebox.showinfo("Success", "File executed successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to execute file: {e}")
def run_gn_cyan_bawah_file():
filepath = "CRUD_Gn_Cyan_Bawah(gabung).py"
try:
with open(filepath, 'r') as file:
exec(file.read(), globals())
messagebox.showinfo("Success", "File executed successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to execute file: {e}")
def run_gn_cyan_atas_file():
filepath = "CRUD_Gn_Cyan_Atas(gabung).py"
try:
with open(filepath, 'r') as file:
exec(file.read(), globals())
messagebox.showinfo("Success", "File executed successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to execute file: {e}")
def run_gn_a_bass_file():
filepath = "CRUD_Gn_A_Bass.py"
try:
with open(filepath, 'r') as file:
exec(file.read(), globals())
messagebox.showinfo("Success", "File executed successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to execute file: {e}")
# Function to display the options based on the current mode and progress
def update_options_frame(progres):
# Clear existing buttons
for widget in options_frame.winfo_children():
widget.destroy()
if current_mode[progres - 1] == "turun":
# Button and Indicator for Gn_Cyan_Atas
Gn_Cyan_Atas_btn = tk.Button(options_frame, text="Gn_Cyan_Atas", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: run_gn_cyan_atas_file [highlight_button(Gn_Cyan_Atas_btn, Gn_Cyan_Atas_indicate), Gn_Cyan_Atas_page()])
Gn_Cyan_Atas_btn.place(x=3, y=0)
Gn_Cyan_Atas_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Atas_indicate.place(x=2, y=5, width=5, height=45)
# Button and Indicator for Gn_Cyan_Bawah
Gn_Cyan_Bawah_btn = tk.Button(options_frame, text="Gn_Cyan_Bawah", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: run_gn_cyan_bawah_file [highlight_button(Gn_Cyan_Bawah_btn, Gn_Cyan_Bawah_indicate), Gn_Cyan_Bawah_page()])
Gn_Cyan_Bawah_btn.place(x=3, y=50)
Gn_Cyan_Bawah_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Bawah_indicate.place(x=2, y=55, width=5, height=45)
# Button and Indicator for Gn_A_Trebel
Gn_A_Trebel_btn = tk.Button(options_frame, text="Gn_A_Trebel", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: run_gn_a_trebel_file [highlight_button(Gn_A_Trebel_btn, Gn_A_Trebel_indicate), Gn_A_Trebel_page()])
Gn_A_Trebel_btn.place(x=3, y=100)
Gn_A_Trebel_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Trebel_indicate.place(x=2, y=105, width=5, height=45)
else: # Mode "naik"
Gn_Cyan_Atas_btn = tk.Button(options_frame, text="Gn_Cyan_Atas", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: run_gn_cyan_atas_file [highlight_button(Gn_Cyan_Atas_btn, Gn_Cyan_Atas_indicate), Gn_Cyan_Atas_page()])
Gn_Cyan_Atas_btn.place(x=3, y=0)
Gn_Cyan_Atas_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Atas_indicate.place(x=2, y=5, width=5, height=45)
# Button and Indicator for Gn_Biru
Gn_Biru_btn = tk.Button(options_frame, text="Gn_Biru", font=("bold", 7), fg="yellow", bd=0, bg="blue", command=lambda: run_gn_biru_file [highlight_button(Gn_Biru_btn, Gn_Biru_indicate), Gn_Biru_page()])
Gn_Biru_btn.place(x=3, y=50)
Gn_Biru_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Biru_indicate.place(x=2, y=55, width=5, height=45)
Gn_A_Bass_btn = tk.Button(options_frame, text="Gn_A_Bass", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: run_gn_a_bass_file [highlight_button(Gn_A_Bass_btn, Gn_A_Bass_indicate), Gn_A_Bass_page()])
Gn_A_Bass_btn.place(x=3, y=100)
Gn_A_Bass_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Bass_indicate.place(x=2, y=105, width=5, height=45)
# Call update_options_frame for the initial setup
update_options_frame(1)
# Function to display Gn_A_Trebel page
def Gn_A_Trebel_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_A_Trebel", font=("Helvetica", 10)).pack()
# Function to display Gn_Biru page
def Gn_Biru_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Biru", font=("Helvetica", 10)).pack()
# Function to display Gn_Cyan_Bawah page
def Gn_Cyan_Bawah_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Cyan_Bawah", font=("Helvetica", 10)).pack()
# Function to display Gn_Cyan_Atas page
def Gn_Cyan_Atas_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Cyan_Atas", font=("Helvetica", 10)).pack()
# Function to display Gn_Biru page
def Gn_A_Bass_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_A_Bass", font=("Helvetica", 10)).pack()
# Initialize the default page
def init_default_page():
update_options_frame(1) # Default to showing options for Progres-1
clear_content_frame()
tk.Label(content_frame, text="Halaman Default Gugus Nada", font=("Helvetica", 10)).pack()
#Fungsi tombol di options_frame
# Button and Indicator for Gn_A_Bass
Gn_A_Bass_btn = tk.Button(options_frame, text="Gn_A_Bass", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: run_gn_a_bass_file [highlight_button(Gn_A_Bass_btn, Gn_A_Bass_indicate), Gn_A_Bass_page()])
Gn_A_Bass_btn.place(x=3, y=0)
Gn_A_Bass_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Bass_indicate.place(x=2, y=5, width=5, height=45)
# Button and Indicator for Gn_Biru
Gn_Biru_btn = tk.Button(options_frame, text="Gn_Biru", font=("bold", 7), fg="yellow", bd=0, bg="blue", command=lambda: run_gn_biru_file [highlight_button(Gn_Biru_btn, Gn_Biru_indicate), Gn_Biru_page()])
Gn_Biru_btn.place(x=3, y=50)
Gn_Biru_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Biru_indicate.place(x=2, y=55, width=5, height=45)
# Button and Indicator for Gn_Cyan_Atas
Gn_Cyan_Atas_btn = tk.Button(options_frame, text="Gn_Cyan_Atas", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: run_gn_cyan_atas_file [highlight_button(Gn_Cyan_Atas_btn, Gn_Cyan_Atas_indicate), Gn_Cyan_Atas_page()])
Gn_Cyan_Atas_btn.place(x=3, y=100)
Gn_Cyan_Atas_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Atas_indicate.place(x=2, y=105, width=5, height=45)
# Button and Indicator for Gn_Cyan_Bawah
Gn_Cyan_Bawah_btn = tk.Button(options_frame, text="Gn_Cyan_Bawah", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: run_gn_cyan_bawah_file [highlight_button(Gn_Cyan_Bawah_btn, Gn_Cyan_Bawah_indicate), Gn_Cyan_Bawah_page()])
Gn_Cyan_Bawah_btn.place(x=3, y=150)
Gn_Cyan_Bawah_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Bawah_indicate.place(x=2, y=155, width=5, height=45)
# Button and Indicator for Gn_A_Trebel
Gn_A_Trebel_btn = tk.Button(options_frame, text="Gn_A_Trebel", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: run_gn_a_trebel_file [highlight_button(Gn_A_Trebel_btn, Gn_A_Trebel_indicate), Gn_A_Trebel_page()])
Gn_A_Trebel_btn.place(x=3, y=200)
Gn_A_Trebel_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Trebel_indicate.place(x=2, y=205, width=5, height=45)
options_frame.pack(side=tk.LEFT)
options_frame.pack_propagate(False)
options_frame.configure(width=170, height=300)
# Function to highlight selected button
def highlight_button(button, indicator):
clear_highlight()
button.config(bg="tomato")
indicator.config(bg="yellow")
def clear_highlight():
Gn_A_Trebel_btn.config(bg="grey")
Gn_A_Trebel_indicate.config(bg="#c3c3c3")
Gn_Biru_btn.config(bg="blue")
Gn_Biru_indicate.config(bg="#c3c3c3")
Gn_Cyan_Bawah_btn.config(bg="green")
Gn_Cyan_Bawah_indicate.config(bg="#c3c3c3")
Gn_Cyan_Atas_btn.config(bg="green")
Gn_Cyan_Atas_indicate.config(bg="#c3c3c3")
Gn_A_Bass_btn.config(bg="grey")
Gn_A_Bass_indicate.config(bg="#c3c3c3")
# Initialize the application with the first progress setup
setup_naik_mode(1)
# Initialize the application with the default page
display_gn_cyan_atas_page()
# Main Function
if __name__ == "__main__":
init_db()
init_default_page()
root.mainloop()
Untuk memperbaiki fungsi lambda pada semua tombol di `options_frame`, kita dapat mengubahnya menjadi lebih dinamis dan terstruktur. Berikut adalah modifikasi dari kode yang Anda berikan, di mana setiap tombol akan menjalankan fungsi yang sesuai berdasarkan file yang ingin dieksekusi:
```python
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import sqlite3
from tkinter import Tk, Label, Entry, Button, Frame, StringVar
# Function to initialize database
def init_db():
conn = sqlite3.connect("DATAMUSIK.db")
c = conn.cursor()
# Create Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Gn_Cyan_Atas (
ID INTEGER PRIMARY KEY,
Level_R TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
# Create Sub_Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Sub_Gn_Cyan_Atas (
Sub_ID INTEGER PRIMARY KEY,
Level_D TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
conn.commit()
conn.close()
# Initialize the main window
root = tk.Tk()
root.geometry("690x1390")
root.title("Arah Arus Musik")
# Head Frame
head_frame = tk.Frame(root, bg="#158aff", highlightbackground="white", highlightthickness=1)
head_frame.place()
# Content Frame
content_frame = tk.Frame(root)
content_frame.pack()
# Function to clear content frame
def clear_content_frame():
for widget in content_frame.winfo_children():
widget.destroy()
# Function to switch modes and update pages
def switch_mode(progres, mode):
if mode[progres - 1] == "naik":
mode[progres - 1] = "turun"
setup_turun_mode(progres)
else:
mode[progres - 1] = "naik"
setup_naik_mode(progres)
# Current mode for each toggle button
current_mode = ['naik', 'naik', 'naik']
toggle_btns = []
indicator_labels = []
pages = [
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass'],
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass'],
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
]
# Function to toggle menus
def toggle_menu(progres, mode):
if mode[progres - 1] == 'naik':
mode[progres - 1] = 'turun'
toggle_btns[progres - 1].config(text="X _ T")
pages[progres - 1] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
indicator_labels[progres - 1].config(text="Turun")
else:
mode[progres - 1] = 'naik'
toggle_btns[progres - 1].config(text="☰ _ N")
pages[progres - 1] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
indicator_labels[progres - 1].config(text="Naik")
update_options_frame(progres)
display_pages(progres)
# Function to display pages
def display_pages(progres):
for widget in content_frame.winfo_children():
widget.destroy()
display_gn_cyan_atas_page()
# Function to display default page
def display_gn_cyan_atas_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah Halaman Gn_Cyan_Atas", font=("Helvetica", 10)).pack()
# Function to setup "Naik" mode
def setup_naik_mode(progres):
if progres == 1:
pages[0] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
elif progres == 2:
pages[1] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
elif progres == 3:
pages[2] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
display_pages(progres)
# Function to setup "Turun" mode
def setup_turun_mode(progres):
if progres == 1:
pages[0] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
elif progres == 2:
pages[1] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
elif progres == 3:
pages[2] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
display_pages(progres)
# Toggle Button 1
toggle_btn1 = tk.Button(head_frame, text="☰ _ P 1", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(1, current_mode))
toggle_btn1.pack(side=tk.LEFT)
toggle_btn1.place(x=5, y=20)
toggle_btns.append(toggle_btn1)
title_lb1 = tk.Label(head_frame, text="Progres-1", bg="#158aff", fg="white", font=("bold", 7))
title_lb1.pack(side=tk.LEFT)
title_lb1.place(x=108, y=11)
indicator_label1 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label1.pack(side=tk.LEFT)
indicator_label1.place(x=127, y=55)
indicator_labels.append(indicator_label1)
# Toggle Button 2
toggle_btn2 = tk.Button(head_frame, text="☰ _ P 2", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(2, current_mode))
toggle_btn2.pack(side=tk.LEFT)
toggle_btn2.place(x=232, y=20)
toggle_btns.append(toggle_btn2)
title_lb2 = tk.Label(head_frame, text="Progres-2", bg="#158aff", fg="white", font=("bold", 7))
title_lb2.pack(side=tk.LEFT)
title_lb2.place(x=334, y=5)
indicator_label2 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label2.pack(side=tk.LEFT)
indicator_label2.place(x=368, y=55)
indicator_labels.append(indicator_label2)
# Toggle Button 3
toggle_btn3 = tk.Button(head_frame, text="☰ _ P 3", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(3, current_mode))
toggle_btn3.pack(side=tk.LEFT)
toggle_btn3.place(x=460, y=20)
toggle_btns.append(toggle_btn3)
title_lb3 = tk.Label(head_frame, text="Progres-3", bg="#158aff", fg="white", font=("bold", 7))
title_lb3.pack(side=tk.LEFT)
title_lb3.place(x=565, y=5)
indicator_label3 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label3.pack(side=tk.LEFT)
indicator_label3.place(x=599, y=55)
indicator_labels.append(indicator_label3)
head_frame.pack(side=tk.TOP, fill=tk.X)
head_frame.pack_propagate(False)
head_frame.configure(height=120)
# Options Frame
options_frame = tk.Frame(root, bg="#c3c3c3")
options_frame.place(x=0, y=50)
# Fungsi untuk menjalankan file CRUD_Gn_A_Trebel.py, CRUD_Gn_Biru.py, CRUD_Gn_Cyan_Bawah.py, CRUD_Gn_Cyan_Atas.py, dan CRUD_Gn_A_Bass.py
def run_file(filepath):
try:
with open(filepath, 'r') as file:
exec(file.read(), globals())
messagebox.showinfo("Success", "File executed successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to execute file: {e}")
# Dictionary to map pages to file paths
page_file_mapping = {
'Gn_A_Trebel': "CRUD_Gn_A
Setelah page_file_mapping = , lanjutkan kodenya hingga selesai
Berikut ini adalah kelanjutan dari kode setelah deklarasi `page_file_mapping`:
```python
# Mapping antara nama page dan fungsi yang sesuai
page_file_mapping = {
'Gn_A_Trebel': run_gn_a_trebel_file,
'Gn_Biru': run_gn_biru_file,
'Gn_Cyan_Bawah': run_gn_cyan_bawah_file,
'Gn_Cyan_Atas': run_gn_cyan_atas_file,
'Gn_A_Bass': run_gn_a_bass_file,
}
# Fungsi untuk memperbarui options_frame berdasarkan halaman yang aktif
def update_options_frame(progres):
clear_content_frame()
# Membersihkan options_frame sebelum memperbarui tombol
for widget in options_frame.winfo_children():
widget.destroy()
for page in pages[progres - 1]:
button = tk.Button(
options_frame,
text=page,
command=lambda p=page: open_page(p),
width=10,
height=2,
bg="#158aff",
fg="white",
font=("bold", 8),
bd=0,
activebackground="#0059b3",
activeforeground="white"
)
button.pack(side=tk.TOP, pady=5)
# Fungsi untuk membuka halaman yang sesuai dan menjalankan file yang terkait
def open_page(page_name):
global active_page
clear_content_frame()
# Jalankan fungsi yang sesuai berdasarkan nama page yang ditekan
if page_name in page_file_mapping:
try:
page_file_mapping[page_name]()
active_page = page_name
except Exception as e:
messagebox.showerror("Error", f"Failed to load page: {e}")
else:
messagebox.showerror("Error", f"Page '{page_name}' not found.")
# Inisialisasi opsi untuk progres 1 saat aplikasi dijalankan pertama kali
update_options_frame(1)
# Menjalankan aplikasi tkinter
root.mainloop()
```
**Penjelasan Kode:**
- **`page_file_mapping`:** Ini adalah dictionary yang memetakan setiap nama halaman (misalnya `'Gn_A_Trebel'`, `'Gn_Biru'`) dengan fungsi yang sesuai yang membuka dan menjalankan file terkait (misalnya `run_gn_a_trebel_file`).
- **`update_options_frame`:** Fungsi ini diperbarui untuk membuat tombol-tombol baru di dalam `options_frame` berdasarkan halaman yang aktif untuk setiap progres (`pages[progres - 1]`).
- **`open_page`:** Fungsi ini dipanggil ketika sebuah tombol halaman ditekan. Ini akan membersihkan konten sebelumnya di `content_frame`, menjalankan fungsi yang sesuai dari `page_file_mapping`, dan memperbarui `active_page` dengan nama halaman yang aktif saat ini.
Dengan struktur ini, Anda dapat mengelola file-file CRUD terkait dengan menekan tombol yang sesuai di aplikasi Anda. Setiap tombol di `options_frame` akan membuka dan menjalankan file CRUD yang telah dipetakan.
Apa penulisan kode yang benar untuk fungsi pada perintah command=lambda: run_gn_a_bass_file [highlight_button(Gn_A_Bass_btn, yang dipanggil untuk menjalankan fungsi def run_gn_a_bass_file(), agar supaya ketika tombol Gn_A_Bass_btn ditekan maka akan membuka file CRUD_Gn_A_Bass.py,
berikut ini:
def run_gn_a_bass_file():
filepath = "CRUD_Gn_A_Bass.py"
try:
with open(filepath, 'r') as file:
exec(file.read(), globals())
messagebox.showinfo("Success", "File executed successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to execute file: {e}")
#Fungsi tombol di options_frame
# Button and Indicator for Gn_A_Bass
Gn_A_Bass_btn = tk.Button(options_frame, text="Gn_A_Bass", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: run_gn_a_bass_file [highlight_button(Gn_A_Bass_btn, Gn_A_Bass_indicate), Gn_A_Bass_page()])
Gn_A_Bass_btn.place(x=3, y=0)
Gn_A_Bass_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Bass_indicate.place(x=2, y=5, width=5, height=45)
Berikut adalah penulisan kode yang benar untuk memanggil fungsi `run_gn_a_bass_file()` dan juga menjalankan fungsi `highlight_button()` saat tombol `Gn_A_Bass_btn` ditekan:
```python
def run_gn_a_bass_file():
filepath = "CRUD_Gn_A_Bass.py"
try:
with open(filepath, 'r') as file:
exec(file.read(), globals())
messagebox.showinfo("Success", "File executed successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to execute file: {e}")
# Fungsi untuk mengubah highlight tombol dan indikator
def highlight_button(button, indicator):
# Reset warna semua tombol dan indikator
for widget in options_frame.winfo_children():
if isinstance(widget, tk.Button):
widget.config(bg="grey", fg="yellow")
elif isinstance(widget, tk.Label):
widget.config(bg="#c3c3c3")
# Highlight tombol dan indikator yang dipilih
button.config(bg="blue", fg="white")
indicator.config(bg="blue")
# Fungsi untuk membuka halaman Gn_A_Bass
def Gn_A_Bass_page():
# Tempatkan kode untuk menampilkan halaman atau melakukan aksi lainnya di sini
pass
# Tombol dan Indikator untuk Gn_A_Bass
Gn_A_Bass_btn = tk.Button(options_frame, text="Gn_A_Bass", font=("bold", 6), fg="yellow", bd=0, bg="grey",
command=lambda: [run_gn_a_bass_file(), highlight_button(Gn_A_Bass_btn, Gn_A_Bass_indicate), Gn_A_Bass_page()])
Gn_A_Bass_btn.place(x=3, y=0)
Gn_A_Bass_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Bass_indicate.place(x=2, y=5, width=5, height=45)
```
**Penjelasan Kode:**
1. **`run_gn_a_bass_file()`:** Fungsi ini membuka dan menjalankan file `CRUD_Gn_A_Bass.py` menggunakan `exec()`.
2. **`highlight_button(button, indicator)`:** Fungsi ini me-reset warna semua tombol dan indikator di dalam `options_frame`, lalu mengubah warna tombol yang dipilih menjadi `bg="blue"` dan `fg="white"`, serta mengubah warna indikator menjadi `bg="blue"`.
3. **`Gn_A_Bass_page()`:** Fungsi placeholder untuk mengelola tampilan halaman atau tindakan tambahan saat tombol ditekan.
4. **`Gn_A_Bass_btn`:** Tombol ini terhubung dengan tiga perintah melalui `lambda`:
- Menjalankan `run_gn_a_bass_file()`.
- Menjalankan `highlight_button(Gn_A_Bass_btn, Gn_A_Bass_indicate)` untuk memberikan efek visual pada tombol dan indikator.
- Memanggil `Gn_A_Bass_page()` untuk menjalankan fungsi tambahan yang Anda definisikan.
Dengan pengaturan ini, saat tombol `Gn_A_Bass_btn` ditekan, file `CRUD_Gn_A_Bass.py` akan dijalankan, tombol akan diberi highlight, dan halaman yang relevan dapat dibuka atau diperbarui.
Setelah saya memperbaiki penulisan kode pada fungs perintah command di setiap tombol di options_frame, menjadi seperti contoh berikut: command=lambda: [run_gn_a_bass_file(), highlight_button(Gn_A_Bass_btn, Gn_A_Bass_indicate), Gn_A_Bass_page()]) , maka kode telah berkembang menjadi berikut ini:
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import sqlite3
from tkinter import Tk, Label, Entry, Button, Frame, StringVar
# Function to initialize database
def init_db():
conn = sqlite3.connect("DATAMUSIK.db")
c = conn.cursor()
# Create Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Gn_Cyan_Atas (
ID INTEGER PRIMARY KEY,
Level_R TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
# Create Sub_Gn_Cyan_Atas table
c.execute('''CREATE TABLE IF NOT EXISTS Sub_Gn_Cyan_Atas (
Sub_ID INTEGER PRIMARY KEY,
Level_D TEXT,
Bass_1 TEXT,
Bass_2 TEXT,
Bass_3 TEXT,
Bass_4 TEXT,
Bass_5 TEXT,
Trebel_1 TEXT,
Trebel_2 TEXT,
Trebel_3 TEXT,
Trebel_4 TEXT,
Trebel_5 TEXT
)''')
conn.commit()
conn.close()
# Initialize the main window
root = tk.Tk()
root.geometry("690x1390")
root.title("Arah Arus Musik")
# Head Frame
head_frame = tk.Frame(root, bg="#158aff", highlightbackground="white", highlightthickness=1)
head_frame.place()
# Content Frame
content_frame = tk.Frame(root)
content_frame.pack()
# Function to clear content frame
def clear_content_frame():
for widget in content_frame.winfo_children():
widget.destroy()
# Function to switch modes and update pages
def switch_mode(progres, mode):
if mode[progres - 1] == "naik":
mode[progres - 1] = "turun"
setup_turun_mode(progres)
else:
mode[progres - 1] = "naik"
setup_naik_mode(progres)
# Current mode for each toggle button
current_mode = ['naik', 'naik', 'naik']
toggle_btns = []
indicator_labels = []
pages = [
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass'],
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass'],
['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
]
# Function to toggle menus
def toggle_menu(progres, mode):
if mode[progres - 1] == 'naik':
mode[progres - 1] = 'turun'
toggle_btns[progres - 1].config(text="X _ T")
pages[progres - 1] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
indicator_labels[progres - 1].config(text="Turun")
else:
mode[progres - 1] = 'naik'
toggle_btns[progres - 1].config(text="☰ _ N")
pages[progres - 1] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
indicator_labels[progres - 1].config(text="Naik")
update_options_frame(progres)
display_pages(progres)
# Function to display pages
def display_pages(progres):
for widget in content_frame.winfo_children():
widget.destroy()
display_gn_cyan_atas_page()
# Function to display default page
def display_gn_cyan_atas_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah Halaman Gn_Cyan_Atas", font=("Helvetica", 10)).pack()
# Function to setup "Naik" mode
def setup_naik_mode(progres):
if progres == 1:
pages[0] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
elif progres == 2:
pages[1] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
elif progres == 3:
pages[2] = ['Gn_A_Trebel', 'Gn_Biru', 'Gn_Cyan_Atas', 'Gn_A_Bass']
display_pages(progres)
# Function to setup "Turun" mode
def setup_turun_mode(progres):
if progres == 1:
pages[0] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
elif progres == 2:
pages[1] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
elif progres == 3:
pages[2] = ['Gn_A_Bass', 'Gn_Biru', 'Gn_Cyan_Bawah', 'Gn_A_Trebel']
display_pages(progres)
# Toggle Button 1
toggle_btn1 = tk.Button(head_frame, text="☰ _ P 1", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(1, current_mode))
toggle_btn1.pack(side=tk.LEFT)
toggle_btn1.place(x=5, y=20)
toggle_btns.append(toggle_btn1)
title_lb1 = tk.Label(head_frame, text="Progres-1", bg="#158aff", fg="white", font=("bold", 7))
title_lb1.pack(side=tk.LEFT)
title_lb1.place(x=108, y=11)
indicator_label1 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label1.pack(side=tk.LEFT)
indicator_label1.place(x=127, y=55)
indicator_labels.append(indicator_label1)
# Toggle Button 2
toggle_btn2 = tk.Button(head_frame, text="☰ _ P 2", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(2, current_mode))
toggle_btn2.pack(side=tk.LEFT)
toggle_btn2.place(x=232, y=20)
toggle_btns.append(toggle_btn2)
title_lb2 = tk.Label(head_frame, text="Progres-2", bg="#158aff", fg="white", font=("bold", 7))
title_lb2.pack(side=tk.LEFT)
title_lb2.place(x=334, y=5)
indicator_label2 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label2.pack(side=tk.LEFT)
indicator_label2.place(x=368, y=55)
indicator_labels.append(indicator_label2)
# Toggle Button 3
toggle_btn3 = tk.Button(head_frame, text="☰ _ P 3", bg="#158aff", font=("bold", 6), fg="white", bd=0, activebackground="#158aff", activeforeground="white", command=lambda: toggle_menu(3, current_mode))
toggle_btn3.pack(side=tk.LEFT)
toggle_btn3.place(x=460, y=20)
toggle_btns.append(toggle_btn3)
title_lb3 = tk.Label(head_frame, text="Progres-3", bg="#158aff", fg="white", font=("bold", 7))
title_lb3.pack(side=tk.LEFT)
title_lb3.place(x=565, y=5)
indicator_label3 = tk.Label(head_frame, text="Turun", bg="#158aff", fg="yellow", font=("bold", 8))
indicator_label3.pack(side=tk.LEFT)
indicator_label3.place(x=599, y=55)
indicator_labels.append(indicator_label3)
head_frame.pack(side=tk.TOP, fill=tk.X)
head_frame.pack_propagate(False)
head_frame.configure(height=120)
# Options Frame
options_frame = tk.Frame(root, bg="#c3c3c3")
options_frame.place(x=0, y=50)
#options_frame.pack(side=tk.LEFT, fill=tk.X)
# Fungsi untuk membuka file CRUD_Gn_Cyan_Bawah(gabung).py
active_page = None # Menyimpan halaman aktif
# Fungsi untuk menjalankan file CRUD_Gn_A_Trebel.py, CRUD_Gn_Biru.py, CRUD_Gn_Cyan_Bawah.py, CRUD_Gn_Cysn_Atas.py, dan CRUD_Gn_A_Bass.py
def run_gn_a_trebel_file():
filepath = "CRUD_Gn_A_Trebel.py"
try:
with open(filepath, 'r') as file:
exec(file.read(), globals())
messagebox.showinfo("Success", "File executed successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to execute file: {e}")
def run_gn_biru_file():
filepath = "CRUD_Gn_Biru.py"
try:
with open(filepath, 'r') as file:
exec(file.read(), globals())
messagebox.showinfo("Success", "File executed successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to execute file: {e}")
def run_gn_cyan_bawah_file():
filepath = "CRUD_Gn_Cyan_Bawah(gabung).py"
try:
with open(filepath, 'r') as file:
exec(file.read(), globals())
messagebox.showinfo("Success", "File executed successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to execute file: {e}")
def run_gn_cyan_atas_file():
filepath = "CRUD_Gn_Cyan_Atas(gabung).py"
try:
with open(filepath, 'r') as file:
exec(file.read(), globals())
messagebox.showinfo("Success", "File executed successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to execute file: {e}")
def run_gn_a_bass_file():
filepath = "CRUD_Gn_A_Bass.py"
try:
with open(filepath, 'r') as file:
exec(file.read(), globals())
messagebox.showinfo("Success", "File executed successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to execute file: {e}")
# Function to display the options based on the current mode and progress
def update_options_frame(progres):
# Clear existing buttons
for widget in options_frame.winfo_children():
widget.destroy()
if current_mode[progres - 1] == "turun":
# Button and Indicator for Gn_Cyan_Atas
Gn_Cyan_Atas_btn = tk.Button(options_frame, text="Gn_Cyan_Atas", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: [run_gn_cyan_atas_file(), highlight_button(Gn_Cyan_Atas_btn, Gn_Cyan_Atas_indicate), Gn_Cyan_Atas_page()])
Gn_Cyan_Atas_btn.place(x=3, y=0)
Gn_Cyan_Atas_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Atas_indicate.place(x=2, y=5, width=5, height=45)
# Button and Indicator for Gn_Cyan_Bawah
Gn_Cyan_Bawah_btn = tk.Button(options_frame, text="Gn_Cyan_Bawah", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: [run_gn_cyan_bawah_file(), highlight_button(Gn_Cyan_Bawah_btn, Gn_Cyan_Bawah_indicate), Gn_Cyan_Bawah_page()])
Gn_Cyan_Bawah_btn.place(x=3, y=50)
Gn_Cyan_Bawah_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Bawah_indicate.place(x=2, y=55, width=5, height=45)
# Button and Indicator for Gn_A_Trebel
Gn_A_Trebel_btn = tk.Button(options_frame, text="Gn_A_Trebel", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: [run_gn_a_trebel_file(), highlight_button(Gn_A_Trebel_btn, Gn_A_Trebel_indicate), Gn_A_Trebel_page()])
Gn_A_Trebel_btn.place(x=3, y=100)
Gn_A_Trebel_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Trebel_indicate.place(x=2, y=105, width=5, height=45)
else: # Mode "naik"
Gn_Cyan_Atas_btn = tk.Button(options_frame, text="Gn_Cyan_Atas", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: [run_gn_cyan_atas_file(), highlight_button(Gn_Cyan_Atas_btn, Gn_Cyan_Atas_indicate), Gn_Cyan_Atas_page()])
Gn_Cyan_Atas_btn.place(x=3, y=0)
Gn_Cyan_Atas_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Atas_indicate.place(x=2, y=5, width=5, height=45)
# Button and Indicator for Gn_Biru
Gn_Biru_btn = tk.Button(options_frame, text="Gn_Biru", font=("bold", 7), fg="yellow", bd=0, bg="blue", command=lambda: [run_gn_biru_file(), highlight_button(Gn_Biru_btn, Gn_Biru_indicate), Gn_Biru_page()])
Gn_Biru_btn.place(x=3, y=50)
Gn_Biru_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Biru_indicate.place(x=2, y=55, width=5, height=45)
Gn_A_Bass_btn = tk.Button(options_frame, text="Gn_A_Bass", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: [run_gn_a_bass_file(), highlight_button(Gn_A_Bass_btn, Gn_A_Bass_indicate), Gn_A_Bass_page()])
Gn_A_Bass_btn.place(x=3, y=100)
Gn_A_Bass_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Bass_indicate.place(x=2, y=105, width=5, height=45)
# Call update_options_frame for the initial setup
update_options_frame(1)
# Function to display Gn_A_Trebel page
def Gn_A_Trebel_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_A_Trebel", font=("Helvetica", 10)).pack()
# Function to display Gn_Biru page
def Gn_Biru_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Biru", font=("Helvetica", 10)).pack()
# Function to display Gn_Cyan_Bawah page
def Gn_Cyan_Bawah_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Cyan_Bawah", font=("Helvetica", 10)).pack()
# Function to display Gn_Cyan_Atas page
def Gn_Cyan_Atas_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_Cyan_Atas", font=("Helvetica", 10)).pack()
# Function to display Gn_Biru page
def Gn_A_Bass_page():
clear_content_frame()
tk.Label(content_frame, text="Ini adalah halaman Gn_A_Bass", font=("Helvetica", 10)).pack()
# Initialize the default page
def init_default_page():
update_options_frame(1) # Default to showing options for Progres-1
clear_content_frame()
tk.Label(content_frame, text="Halaman Default Gugus Nada", font=("Helvetica", 10)).pack()
#Fungsi tombol di options_frame
# Button and Indicator for Gn_A_Bass
Gn_A_Bass_btn = tk.Button(options_frame, text="Gn_A_Bass", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: [run_gn_a_bass_file(), highlight_button(Gn_A_Bass_btn, Gn_A_Bass_indicate), Gn_A_Bass_page()])
Gn_A_Bass_btn.place(x=3, y=0)
Gn_A_Bass_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Bass_indicate.place(x=2, y=5, width=5, height=45)
# Button and Indicator for Gn_Biru
Gn_Biru_btn = tk.Button(options_frame, text="Gn_Biru", font=("bold", 7), fg="yellow", bd=0, bg="blue", command=lambda: [run_gn_biru_file(), highlight_button(Gn_Biru_btn, Gn_Biru_indicate), Gn_Biru_page()])
Gn_Biru_btn.place(x=3, y=50)
Gn_Biru_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Biru_indicate.place(x=2, y=55, width=5, height=45)
# Button and Indicator for Gn_Cyan_Atas
Gn_Cyan_Atas_btn = tk.Button(options_frame, text="Gn_Cyan_Atas", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: [run_gn_cyan_atas_file(), highlight_button(Gn_Cyan_Atas_btn, Gn_Cyan_Atas_indicate), Gn_Cyan_Atas_page()])
Gn_Cyan_Atas_btn.place(x=3, y=100)
Gn_Cyan_Atas_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Atas_indicate.place(x=2, y=105, width=5, height=45)
# Button and Indicator for Gn_Cyan_Bawah
Gn_Cyan_Bawah_btn = tk.Button(options_frame, text="Gn_Cyan_Bawah", font=("bold", 5), fg="yellow", bd=0, bg="green", command=lambda: [run_gn_cyan_bawah_file(), highlight_button(Gn_Cyan_Bawah_btn, Gn_Cyan_Bawah_indicate), Gn_Cyan_Bawah_page()])
Gn_Cyan_Bawah_btn.place(x=3, y=150)
Gn_Cyan_Bawah_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_Cyan_Bawah_indicate.place(x=2, y=155, width=5, height=45)
# Button and Indicator for Gn_A_Trebel
Gn_A_Trebel_btn = tk.Button(options_frame, text="Gn_A_Trebel", font=("bold", 6), fg="yellow", bd=0, bg="grey", command=lambda: [run_gn_a_trebel_file(), highlight_button(Gn_A_Trebel_btn, Gn_A_Trebel_indicate), Gn_A_Trebel_page()])
Gn_A_Trebel_btn.place(x=3, y=200)
Gn_A_Trebel_indicate = tk.Label(options_frame, text=" ", bg="#c3c3c3")
Gn_A_Trebel_indicate.place(x=2, y=205, width=5, height=45)
options_frame.pack(side=tk.LEFT)
options_frame.pack_propagate(False)
options_frame.configure(width=170, height=300)
# Function to highlight selected button
def highlight_button(button, indicator):
clear_highlight()
button.config(bg="tomato")
indicator.config(bg="yellow")
def clear_highlight():
Gn_A_Trebel_btn.config(bg="grey")
Gn_A_Trebel_indicate.config(bg="#c3c3c3")
Gn_Biru_btn.config(bg="blue")
Gn_Biru_indicate.config(bg="#c3c3c3")
Gn_Cyan_Bawah_btn.config(bg="green")
Gn_Cyan_Bawah_indicate.config(bg="#c3c3c3")
Gn_Cyan_Atas_btn.config(bg="green")
Gn_Cyan_Atas_indicate.config(bg="#c3c3c3")
Gn_A_Bass_btn.config(bg="grey")
Gn_A_Bass_indicate.config(bg="#c3c3c3")
# Initialize the application with the first progress setup
setup_naik_mode(1)
# Initialize the application with the default page
display_gn_cyan_atas_page()
# Main Function
if __name__ == "__main__":
init_db()
init_default_page()
root.mainloop()
Kodenya sudah terlihat sangat baik dan mengarah pada implementasi yang diinginkan. Dengan beberapa tambahan kecil, kode ini akan lebih optimal dan mudah dikelola. Berikut adalah beberapa hal yang perlu diperhatikan dan mungkin perlu sedikit penyesuaian:
### 1. Penempatan `try-except` di Fungsi File Execution
Setiap fungsi `run_*_file` sudah mengimplementasikan `try-except` dengan baik. Namun, akan lebih efisien jika Anda mengurangi redundansi kode dengan membuat satu fungsi umum untuk menangani eksekusi file dan pemanggilan pesan error atau sukses. Berikut adalah contoh implementasinya:
```python
def run_file(filepath):
try:
with open(filepath, 'r') as file:
exec(file.read(), globals())
messagebox.showinfo("Success", f"{filepath} executed successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to execute {filepath}: {e}")
# Contoh penggunaan
def run_gn_a_trebel_file():
run_file("CRUD_Gn_A_Trebel.py")
def run_gn_biru_file():
run_file("CRUD_Gn_Biru.py")
def run_gn_cyan_bawah_file():
run_file("CRUD_Gn_Cyan_Bawah(gabung).py")
def run_gn_cyan_atas_file():
run_file("CRUD_Gn_Cyan_Atas(gabung).py")
def run_gn_a_bass_file():
run_file("CRUD_Gn_A_Bass.py")
```
### 2. Fungsi `clear_content_frame()`
Fungsi ini sudah bagus untuk menghapus konten pada `content_frame` sebelum menampilkan yang baru. Tidak ada perubahan yang diperlukan di bagian ini.
### 3. Pengaturan Mode Naik/Turun
Implementasi toggle untuk pengaturan mode naik/turun sudah efektif. Hanya saja, pastikan bahwa logika di dalam `toggle_menu` bekerja dengan benar sesuai dengan logika aplikasi Anda, terutama saat mengganti mode dari "naik" ke "turun" atau sebaliknya. Pastikan bahwa data yang ditampilkan juga berubah sesuai dengan mode yang dipilih.
### 4. Efisiensi Fungsi Display Pages
Jika ingin lebih efisien, fungsi `display_pages(progres)` bisa lebih fleksibel dengan menerima nama halaman yang ingin ditampilkan sebagai parameter. Dengan demikian, Anda tidak perlu memanggil `display_gn_cyan_atas_page()` secara eksplisit, tetapi bisa disertakan dalam daftar halaman yang sesuai.
### 5. Pembaruan dan Pengaturan Halaman Aktif
Variabel `active_page` dapat digunakan lebih lanjut untuk menyimpan halaman yang saat ini sedang aktif, dan hanya memperbarui konten jika halaman yang berbeda dipilih. Ini dapat membantu dalam menjaga kinerja aplikasi saat pengguna sering berpindah halaman.
### 6. Tampilan GUI dan Layout
Pastikan bahwa layout dan tampilan GUI (misalnya, posisi tombol, label, dan ukuran frame) tetap konsisten di seluruh aplikasi, terutama ketika ukuran jendela berubah. Anda mungkin ingin menambahkan fitur untuk menangani resize event atau menggunakan grid layout untuk lebih fleksibel.
Dengan penyesuaian ini, aplikasi akan lebih robust, mudah dikelola, dan lebih efisien dalam menjalankan fungsinya. Anda bisa melanjutkan pengujian dan debugging untuk memastikan semua fitur bekerja sesuai dengan yang diharapkan.
Bersambung ke ...
Komentar
Posting Komentar
This Message