help combining tkinter & mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thekid
    New Member
    • Feb 2007
    • 145

    help combining tkinter & mysql

    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:

    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()
    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 *
    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()
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Use Tkinter's get method for an entry box to return the text entered, and pass it to the MySQL db.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      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.

      Comment

      • Thekid
        New Member
        • Feb 2007
        • 145

        #4
        Ok, so I read about get() and played around with it and I got my code to work:

        Code:
        from Tkinter import *
        import sys
        import MySQLdb
        
        conn = MySQLdb.connect (host = "localhost",
                                   user = "thekid",
                                   passwd = "pwd",
                                   db = "maindb")
        cursor = conn.cursor () 
         
        def SaveData():
            f = a.get()
            g = b.get()
            cursor.execute ("DROP TABLE IF EXISTS maindb")
            cursor.execute ("""CREATE TABLE maindb(fname VARCHAR(20), lname VARCHAR(20))""")
            cursor.execute ("""INSERT INTO info (fname, lname) VALUES("%s", "%s")"""% (f,g))
            print "Number of rows inserted: %d" % cursor.rowcount
            conn.close()
         
        root = Tk()
         
        ent_frame = Frame(root)
        Label(ent_frame, text="FIRST NAME:").pack(side=LEFT)
        a = Entry(ent_frame, width=15)
        a.pack(side=LEFT)
        Label(ent_frame, text="LAST NAME:").pack(side=LEFT)
        b = Entry(ent_frame, width=15)
        b.pack(side=LEFT)
        ent_frame.pack()
         
        Button(root, text="Save",command=SaveData).pack(side=BOTTOM)
         
        mainloop()
        I'm not sure how to incorporate the StringVar() for this but it seems to be working.

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          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:
          Code:
          class EntryWidget(Tkinter.Entry):
              def __init__(self, master, x, y):
                  Tkinter.Entry.__init__(self, master=master)
                  self.value = Tkinter.StringVar()
                  self.config(textvariable=self.value, width=8,
                              relief="ridge", font=textFont1,
                              bg="#ddddddddd", fg="#000000000",
                              justify='center')
                  self.grid(column=x, row=y)
                  self.value.set("")

          Comment

          Working...