PYTHON And MySQL - How To Search And Display Data In MySQL Database Using Python Tkinter

PYTHON Code - How To Search Record In MySQL Database And Show The Results Python Tkinter


How To Search And Display Data In MySQL Database Using Python Tkinter

In this Python Tutorial we will learn How To Find Data In MySQL Database With A Specific 
Value From TextField And Display The Result In a Texboxes In Python .
we will use the Tkinter library to create the GUI that incorporates a textbox (Entry) components.


Project Source Code:
import tkinter as tk
from tkinter import *
from tkinter import ttk
import mysql.connector

root = Tk()
root.title("Search Data By ID")

connection = mysql.connector.connect(host='localhost', user='root', password='',
port='3306', database='test_py')
c = connection.cursor()

bkg = "#dddddd"


frame = tk.Frame(root, bg=bkg)

label_id = tk.Label(frame, text="ID: ", font=('verdana',12), bg=bkg)
entry_id = tk.Entry(frame, font=('verdana',12))

label_firstname = tk.Label(frame, text="First Name: ", font=('verdana',12), bg=bkg)
entry_firstname = tk.Entry(frame, font=('verdana',12))

label_lastname = tk.Label(frame, text="Last Name: ", font=('verdana',12), bg=bkg)
entry_lastname = tk.Entry(frame, font=('verdana',12))

label_email = tk.Label(frame, text="Email: ", font=('verdana',12), bg=bkg)
entry_email = tk.Entry(frame, font=('verdana',12))

label_age = tk.Label(frame, text="Age: ", font=('verdana',12), bg=bkg)
entry_age = tk.Entry(frame, font=('verdana',12))


def searchData():

# clear fields
entry_firstname.delete(0,END)
entry_lastname.delete(0,END)
entry_email.delete(0,END)
entry_age.delete(0,END)

# search
user_id = entry_id.get()
search_query = "SELECT * FROM `users_2` WHERE `id` = "+user_id
c.execute(search_query)
userdata = c.fetchone()
entry_firstname.insert(0,userdata[1])
entry_lastname.insert(0,userdata[2])
entry_email.insert(0,userdata[3])
entry_age.insert(0,userdata[4])



button_search = tk.Button(frame, text="Search", font=('verdana',14),fg='#ffffff',
bg='orange', command = searchData)

label_id.grid(row=0, column=0, sticky='e')
entry_id.grid(row=0, column=1, pady=10, padx=10)

label_firstname.grid(row=1, column=0)
entry_firstname.grid(row=1, column=1, pady=10, padx=10)

label_lastname.grid(row=2, column=0)
entry_lastname.grid(row=2, column=1, pady=10, padx=10)

label_email.grid(row=3, column=0, sticky='e')
entry_email.grid(row=3, column=1, pady=10, padx=10)

label_age.grid(row=4, column=0, sticky='e')
entry_age.grid(row=4, column=1, pady=10, padx=10)

button_search.grid(row=5,column=0, columnspan=2, pady=10, padx=10, sticky='nsew')

frame.grid(row=0, column=0)


root.mainloop()


// OUTPUT:
How To Search Values From MySQL Database And Set It Into Textboxes In Python Tkinter




Share this

Related Posts

Previous
Next Post »