Repository navigation
Expand file tree
/
Copy pathsql gui.py
More file actions
87 lines (64 loc) · 1.7 KB
/
Copy pathsql gui.py
File metadata and controls
87 lines (64 loc) · 1.7 KB
1
# A GUI Application to retrieve rows from a table in mysqldbimport MySQLdbfrom tkinter import *# creating a root windowroot = Tk()# a function that takes eno and displays the rowdef retrieve_rows(eno): # connect to MySQLdb conn = MySQLdb.connect(host = 'localhost', database = 'world', user = 'root', password = 'root') # prepare a cursor obj cursor = conn.cursor() # prepare a SQL query string to retrieve a row str = "select * from emptab where eno = '%d'" # define the args args = (eno) # execute the SQL query using execute() method cursor.execute(str % args) # get only one row row = cursor.fetchone() # if row exists display it using a lbl if row is not None: lbl = Label(text = row, font = ('Arial', 14)).place(x = 50, y = 200) # close con cursor.close() conn.close()# a func that takes input from entry widgetdef display(self): # retrieve the value from the entry widget str = el.get() # display the values using label lbl = Label(text = 'You entered: '+str, font = ('Arial', 14)).place(x = 50, y = 150) # call the func that retrieves the row retrieve_rows(int(str))# create a frame as child to root windowf = Frame(root, height = 350, width = 600)# let the frame to not shrinkf.propagate(0)# attach the frame to root windowf.pack()# labellab = Label(text = 'Enter employee number: ', font = ('Arial', 14))# create a entry widget for accepting employee noel = Entry(f, width = 15, fg = 'blue', bg = 'yellow', font = ('Arial', 14))# when user presses enter call display methodel.bind('<Return>', display)# place entry and label widgets in framelab.place(x = 50, y = 100)el.place(x = 300, y = 100)# handle the eventsroot.mainloop()