473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help with tkinter/sql global error.

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

2 1660
bvdet
2,851 Expert Mod 2GB
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
145 100+
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

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

Similar topics

1
by: VSK | last post by:
Hi all, For a web application if we are using web farm, and if i want to do Global Error handling can i use Applicatio_Error() method in global.asax. Now in this method i will call business...
1
by: Kemal Taskin | last post by:
hi all, i have a question about asp.net's global error handling mechanisms. Using customErrors in web.config seems to be fine, it redirects you where you want. But how to display different...
2
by: Win, Pats | last post by:
Sorry if this is OT, I'm not sure where else to research this... I recently added a "global error handler" via an HTTP Module so I could log all otherwise unhandled exceptions. Every day I get...
5
by: OHM | last post by:
Hi everyone, I have a problem with error handling and wondered if anyone has managed to implement a global exception handling model. Is it possible to ensure that you see all exceptions before...
2
by: ===Steve L.=== | last post by:
does anyone know any site or ariticle provides exmaple for building a Global Error Handling Module in VB.Net? the module should be able to 1. prouduce friendly generic error msg for the users and...
5
by: UJ | last post by:
Is there a way to do a global error handler much the way you can in asp.net ? Essentially I want to set up a routine that if there is any unhandled exception it will call that routine so I can...
2
by: yeghia \(sosy\) | last post by:
Hi Guys I have two questions. 1. Is there a way to set a handler to .NET application so that in case of not caught exception my handler is called before application shutdown. I want to send an...
0
by: jefftheman812 | last post by:
I would like to use a global error handler via the web config file. Setting the error page is easy but I seem to be having issues catching the error in my customized page. Here is the offending...
7
by: Jason Kester | last post by:
Best I can tell, there are three basic ways you can deal with global error handling in ASP.NET. Namely: 1. Derive all your pages from a custom Page class, and override OnError() 2. Specify a...
1
by: John | last post by:
Hi I have a winform app with try/catch error handling implemented in key areas. Problem is errors can occur in other areas in situations that I may not foresee. Is there a way to implemented a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.