In this article we will search the MySql Database. There will be 2 Labels, 1 Entry and Button in our form.
import mysql.connector
import tkinter as tk
from tkinter import *
con1 = mysql.connector.connect(
host="localhost",
user="root",
password="2344",
database="book"
)
cur = con1.cursor()
root= tk.Tk()
root.geometry("800x100")
root.title("search...bs")
# add one label.
lbl = tk.Label(root, text='Enter Author ID: ', width=25,font='arial 12 bold')
lbl.grid(row=1,column=1)
# add one text box.
tbox = tk.Text(root, height=2, width=20,bg='white',font='arial 14 bold')
tbox.grid(row=1,column=2)
btn = tk.Button(root, text='Show Details (Goster)',height=2, width=25,bg='deepskyblue',fg="white",font='arial 12 bold',
command=lambda: details(tbox.get('1.0',END)))
btn.grid(row=1,column=3)
str1 = tk.StringVar()
# add one label.
lbl2 = tk.Label(root, textvariable=str1, width=30,fg='red',font='arial 12 bold' )
lbl2.grid(row=3,column=1,columnspan=2)
str1.set("")
def details(id):
try:
val = int(id) # check input is integer or not.
try:
cur.execute("Select * From worldclassics Where Id="+id)
author = cur.fetchone()
str1.set(author)
except :
str1.set("Database error! ")
except:
str1.set("Check input!")
root.mainloop()
Hope to see you in the next article. Happy coding
No comments:
Post a Comment