Afternoon,
I got my script setup and working as I had hoped for the most part. It makes the 3 button gui, the popup displays the mysql table data as I wand it. Now Im stuck on how to make it searchable. I have the search box up top in the popup, but I have not found a way to make it actually search as of yet. I have tried a few different online examples and none have worked. Being this is my second week of attempting this, I am no where near proficient with understanding many of the more complex examples I found. Any help would be appreciated. Attached is a screenshot of what Im trying to search, as well as my code.
import tkinter as tk
from tkinter import ttk
import mysql.connector
def create_popup():
popup = tk.Toplevel(root)
popup.title("Search")
# Create a frame for the search bar and scrollbar
search_frame = tk.Frame(popup)
search_frame.pack(fill="both", expand=True)
# Create the search bar
search_bar = tk.Entry(search_frame)
search_bar.pack(side="left", fill="x", expand=True)
# Create the scrollbar
scrollbar = ttk.Scrollbar(search_frame, orient="vertical")
scrollbar.pack(side="right", fill="y")
# Create a listbox to display search results
results_listbox = tk.Listbox(popup, yscrollcommand=scrollbar.set)
results_listbox.pack(fill="both", expand=True)
# Configure the scrollbar to work with the listbox
scrollbar.config(command=results_listbox.yview)
# Add some sample results to the listbox
#for i in range():
# results_listbox.insert(tk.END, f"Result {i}")
# Fetch data from MySQL (replace with your actual query)
mydb = mysql.connector.connect(
host="xxxxx",
user="xxxxx",
password="xxxxxx",
database="xxxxx"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT USERNAME,FRAMEDIPADDRESS FROM ACCOUNTING")
myresult = mycursor.fetchall()
# Display data in the frame
for row in myresult:
results_listbox.insert(tk.END, f"{row[0], row[1]}")
#Function to clear Table
def run_command2():
mydb = mysql.connector.connect(
host="xxxxx",
user="xxxxx",
password="xxxxxxx",
auth_plugin='mysql_native_password',
database="xxxxx"
)
mycursor = mydb.cursor()
mycursor.execute("TRUNCATE TABLE ACCOUNTING")
print("Table Cleared")
mycursor.close()
mydb.close()
# Start the GUI
root = tk.Tk()
root.title("Radiator Accounting Lookup and Clear")
root.geometry("480x480")
# Create a button to open the popup
button = tk.Button(root, text="Read Table", command=create_popup)
button.pack(pady=20)
# Create a clear button
btn = tk.Button(root, text="Clear Table", command=run_command2)
btn.pack(pady=20)
# Create a quit button
quit_button = tk.Button(root, text="Quit", command=root.quit)
quit_button.pack(pady=20)
# Start the main GUI
root.mainloop()