I can't seem to find anything that shows how to use tkinter with mysql. I do know how to connect to mysql with python scripting but I need to know how to enter information into a tkinter window and have it store into mysql. Here's an example:
I know I need to 'import MySQLdb' which I do have and I also have MYSQL installed. I have a database I created named 'maindb' and it has 2 tables in it, 'f_name' and 'l_name'. Do I need to create an event and bind it to the "Save" button? The following code isn't correct but is it in the right direction?
Code:
from Tkinter import * root = Tk() ent_frame = Frame(root) Label(ent_frame, text="FIRST NAME:").pack(side=LEFT) Entry(ent_frame, width=15).pack(side=LEFT) Label(ent_frame, text="LAST NAME:").pack(side=LEFT) Entry(ent_frame, width=15).pack(side=LEFT) ent_frame.pack() Button(root, text="Save").pack(side=BOTTOM) mainloop()
Code:
from Tkinter import *
import MySQLdb
conn = MySQLdb.connect (host = "localhost",
user = "thekid",
passwd = "pwd",
db = "maindb")
cursor = conn.cursor ()
def SaveData(event):
sql = """INSERT INTO maindb(f_name, l_name)
VALUES(somehow grab entered values)"""
root = Tk()
ent_frame = Frame(root)
Label(ent_frame, text="FIRST NAME:").pack(side=LEFT)
Entry(ent_frame, width=15).pack(side=LEFT)
Label(ent_frame, text="LAST NAME:").pack(side=LEFT)
Entry(ent_frame, width=15).pack(side=LEFT)
ent_frame.pack()
b = Button(root, text="Save",command=SaveData)
b.bind("<RETURN>", SaveData)
b.pack(side=BOTTOM)
mainloop()
Comment