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

help with tkinter/sql global error.

Thekid
100+
P: 145
I've hit another snag with tkinter and mysql. After the last post, I'm able to now have a user login that when username and password are entered, a successful mysql connection can be made. The problem now is that "cursor isn't global" and I'm not sure how to make it that way, or how to correct this code. So after logging in and clicking the "Next" button from the root window, I get error messages.


Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import sys
  3. import MySQLdb
  4.  
  5.  
  6.  
  7. # Toplevel window that opens when "NEXT" button is clicked in "NextWindow" widget
  8. def NewWindow():
  9.     win = Toplevel()
  10.  
  11.     # entry boxes put in a frame, text stored in variables k, l
  12.     ent_frame = Frame(win)
  13.     Label(ent_frame, text="ITEM:").pack(side=LEFT)
  14.     k = Entry(ent_frame, width=15)
  15.     k.pack(side=LEFT)
  16.     Label(ent_frame, text="BRAND:").pack(side=LEFT)
  17.     l = Entry(ent_frame, width=15)
  18.     l.pack(side=LEFT)
  19.     ent_frame.pack()
  20.  
  21.     # using get method for entry boxes above
  22.     def SaveIt():
  23.         m = k.get()
  24.         n = l.get()
  25.         # executing a sql command and inserting m,n
  26.         cursor.execute ("""INSERT INTO info (item, brand) VALUES("%s", "%s")"""% (m,n))
  27.         print "Number of rows inserted: %d" % cursor.rowcount
  28.         conn.close()
  29.  
  30.     Button(win, text="Save",command=SaveIt).pack(side=BOTTOM)
  31.     Button(win, text="Next", command=win.destroy).pack(side=BOTTOM)
  32.  
  33.  
  34. # Toplevel window that opens when "Next" is clicked from root window.
  35. def NextWindow():
  36.     win = Toplevel()
  37.  
  38.     # entry boxes put in a frame, text stored in variables c,d
  39.     ent_frame = Frame(win)
  40.     Label(ent_frame, text="CITY:").pack(side=LEFT)
  41.     c = Entry(ent_frame, width=15)
  42.     c.pack(side=LEFT)
  43.     Label(ent_frame, text="STATE:").pack(side=LEFT)
  44.     d = Entry(ent_frame, width=15)
  45.     d.pack(side=LEFT)
  46.     ent_frame.pack()
  47.  
  48.     # using get method for entry boxes
  49.     def SaveThis():
  50.         h = c.get()
  51.         j = d.get()
  52.         # executing a sql command and inserting h,j
  53.         cursor.execute ("""INSERT INTO info (city, state) VALUES("%s", "%s")"""% (h,j))
  54.         print "Number of rows inserted: %d" % cursor.rowcount
  55.         conn.close()
  56.  
  57.     # Click 'save' run 'SaveThis' and executes sql command. 'Next' opens toplevel
  58.     Button(win, text="Save",command=SaveThis).pack(side=BOTTOM)
  59.     Button(win, text="Next", command=NewWindow).pack(side=BOTTOM)
  60.  
  61.  
  62.  
  63. # Takes text entered in root window of user & pass and enters them in sql connect
  64. def SaveData():
  65.     f = a.get()
  66.     g = b.get()
  67.     try:
  68.       conn = MySQLdb.connect (host = "localhost",
  69.                               user = "%s" % (f),
  70.                               passwd = "%s" % (g),
  71.                               db = "maindb") 
  72.     # error if wrong username or password 
  73.     except MySQLdb.Error, e:
  74.       print "Error %d: %s" % (e.args[0], e.args[1])
  75.       sys.exit (1)
  76.  
  77.       cursor = conn.cursor () 
  78.  
  79. root = Tk()
  80.  
  81. # frame containing 2 entries which will be stored in variables a,b
  82. ent_frame = Frame(root)
  83. Label(ent_frame, text="USERNAME:").pack(side=LEFT)
  84. a = Entry(ent_frame, width=15)
  85. a.pack(side=LEFT)
  86. Label(ent_frame, text="PASSWORD:").pack(side=LEFT)
  87. b = Entry(ent_frame, width=15)
  88. b.pack(side=LEFT)
  89. ent_frame.pack()
  90.  
  91. # Clicking 'login' runs 'SaveData' and passes info into sql and connects with
  92. # username and password.
  93. Button(root, text="Login",command=SaveData).pack(side=BOTTOM)
  94. # opens a toplevel window with more entry fields
  95. Button(root, text="Next", command=NextWindow).pack(side=BOTTOM)
  96.  
  97. mainloop()
  98.  
Nov 19 '10 #1

✓ answered by bvdet

Could you post the complete error message including traceback? Could it be as simple as:
Expand|Select|Wrap|Line Numbers
  1. def SaveData():
  2.     global cursor

Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Could you post the complete error message including traceback? Could it be as simple as:
Expand|Select|Wrap|Line Numbers
  1. def SaveData():
  2.     global cursor
Nov 19 '10 #2

Thekid
100+
P: 145
That solved it, thanks! I was trying to set 'global' in "def SaveIt" and elsewhere because of the error. This was the basis of the error:
In SaveIt line 22 NameError: global name 'cursor' is not defined
Nov 19 '10 #3

Post your reply

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