Python How To Filter Data In Tkinter Treeview Using Python And MySQL Database
In this Python Tutorial we will see How To Use Search Data In Tkinter Table (Treeview) Using MySQL Database And Python .
In This Tutorial We Will Use:
- Python Programming Language.
- Tkinter Graphical User Interface.
- VsCode IDE .
- XAMPP .
- PhpMyAdmin .
- VsCode IDE .
- XAMPP .
- PhpMyAdmin .
Python Source Code:
import tkinter as tk
from tkinter import *
from tkinter import ttk
import mysql.connector
root = Tk()
root.title("Search")
bckg = 'grey'
connection = mysql.connector.connect(host='localhost', user='root', password='',
port='3306', database='test_py')
c = connection.cursor()
frame = tk.Frame(root, bg=bckg)
label_search = tk.Label(frame, text = "Search By First or Last Name:", bg=bckg,
fg='gold', font=('verdana',12))
entry_search = tk.Entry(frame, font=('verdana',12))
button_search = tk.Button(frame, text='Search', font=('verdana',12), bg='#0984e3',
fg="#ffffff")
trv = ttk.Treeview(frame, columns=(1,2,3,4,5), height=15, show="headings")
trv.column(1, anchor=CENTER, stretch=NO, width=100)
trv.column(2, anchor=CENTER, stretch=NO, width=100)
trv.column(3, anchor=CENTER, stretch=NO, width=100)
trv.column(4, anchor=CENTER, stretch=NO, width=100)
trv.column(5, anchor=CENTER, stretch=NO, width=100)
trv.heading(1, text='ID')
trv.heading(2, text='First Name')
trv.heading(3, text='Last Name')
trv.heading(4, text='Email')
trv.heading(5, text='Age')
def find():
# clear treeview
for item in trv.get_children():
trv.delete(item)
val = entry_search.get()
c.execute("SELECT * FROM `users_2` WHERE `firstname` LIKE %s or `lastname`
like %s",("%"+val+"%","%"+val+"%"))
users = c.fetchall()
for row in users:
trv.insert('',END, values=row)
button_search['command'] = find
find()
label_search.grid(row=0, column=0, padx=10, pady=10)
entry_search.grid(row=0, column=1, padx=10, pady=10)
button_search.grid(row=0, column=2, padx=10, pady=10)
trv.grid(row=1, column=0, columnspan=3)
frame.grid(row=0, column=0, sticky="nswe")
root.mainloop()
Download Projects Source Code