By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

help combining tkinter & mysql

Thekid
100+
P: 145
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:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. root = Tk()
  4.  
  5. ent_frame = Frame(root)
  6. Label(ent_frame, text="FIRST NAME:").pack(side=LEFT)
  7. Entry(ent_frame, width=15).pack(side=LEFT)
  8. Label(ent_frame, text="LAST NAME:").pack(side=LEFT)
  9. Entry(ent_frame, width=15).pack(side=LEFT)
  10. ent_frame.pack()
  11.  
  12. Button(root, text="Save").pack(side=BOTTOM)
  13.  
  14. mainloop()
  15.  
  16.  
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?

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import MySQLdb
  3.  
  4. conn = MySQLdb.connect (host = "localhost",
  5.                            user = "thekid",
  6.                            passwd = "pwd",
  7.                            db = "maindb")
  8. cursor = conn.cursor ()
  9.  
  10.  
  11. def SaveData(event):
  12.     sql = """INSERT INTO maindb(f_name, l_name)
  13.              VALUES(somehow grab entered values)"""
  14.  
  15. root = Tk()
  16.  
  17. ent_frame = Frame(root)
  18. Label(ent_frame, text="FIRST NAME:").pack(side=LEFT)
  19. Entry(ent_frame, width=15).pack(side=LEFT)
  20. Label(ent_frame, text="LAST NAME:").pack(side=LEFT)
  21. Entry(ent_frame, width=15).pack(side=LEFT)
  22. ent_frame.pack()
  23.  
  24. b = Button(root, text="Save",command=SaveData)
  25. b.bind("<RETURN>", SaveData)
  26. b.pack(side=BOTTOM)
  27.  
  28. mainloop()
  29.  
  30.  
Nov 5 '10 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 621
Use Tkinter's get method for an entry box to return the text entered, and pass it to the MySQL db.
Nov 5 '10 #2

bvdet
Expert Mod 2.5K+
P: 2,851
One thing I will add - you need to create StringVar() variables for the Entry widgets. Your SaveData callback will access the variables to pass on to your database. You're on the right track.
Nov 5 '10 #3

Thekid
100+
P: 145
Ok, so I read about get() and played around with it and I got my code to work:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import sys
  3. import MySQLdb
  4.  
  5. conn = MySQLdb.connect (host = "localhost",
  6.                            user = "thekid",
  7.                            passwd = "pwd",
  8.                            db = "maindb")
  9. cursor = conn.cursor () 
  10.  
  11. def SaveData():
  12.     f = a.get()
  13.     g = b.get()
  14.     cursor.execute ("DROP TABLE IF EXISTS maindb")
  15.     cursor.execute ("""CREATE TABLE maindb(fname VARCHAR(20), lname VARCHAR(20))""")
  16.     cursor.execute ("""INSERT INTO info (fname, lname) VALUES("%s", "%s")"""% (f,g))
  17.     print "Number of rows inserted: %d" % cursor.rowcount
  18.     conn.close()
  19.  
  20. root = Tk()
  21.  
  22. ent_frame = Frame(root)
  23. Label(ent_frame, text="FIRST NAME:").pack(side=LEFT)
  24. a = Entry(ent_frame, width=15)
  25. a.pack(side=LEFT)
  26. Label(ent_frame, text="LAST NAME:").pack(side=LEFT)
  27. b = Entry(ent_frame, width=15)
  28. b.pack(side=LEFT)
  29. ent_frame.pack()
  30.  
  31. Button(root, text="Save",command=SaveData).pack(side=BOTTOM)
  32.  
  33. mainloop()
  34.  
  35.  
I'm not sure how to incorporate the StringVar() for this but it seems to be working.
Nov 5 '10 #4

bvdet
Expert Mod 2.5K+
P: 2,851
StringVar(), DoubleVar(), and IntVar() are class constructors used to create control variables for various Tkinter widgets. See this documentation, page 104, for a discussion of Tkinter control variables. In your case, you can use the widget get() method to return the widget's current text.

Here's an example of using a control variable:
Expand|Select|Wrap|Line Numbers
  1. class EntryWidget(Tkinter.Entry):
  2.     def __init__(self, master, x, y):
  3.         Tkinter.Entry.__init__(self, master=master)
  4.         self.value = Tkinter.StringVar()
  5.         self.config(textvariable=self.value, width=8,
  6.                     relief="ridge", font=textFont1,
  7.                     bg="#ddddddddd", fg="#000000000",
  8.                     justify='center')
  9.         self.grid(column=x, row=y)
  10.         self.value.set("")
Nov 5 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.