hey there i've been stumped simple error past day , haven't been able passed it. part of code i'm getting error message:
exception in tkinter callback traceback (most recent call last): file "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__ return self.func(*args) file "/home/ppppwn3d/workspace/python/jailbreakbob/jailbreakbob.py", line 110, in buttonclick_mainscreen gamescreen() file "/home/ppppwn3d/workspace/python/jailbreakbob/jailbreakbob.py", line 58, in gamescreen if pressed == 8 , e1 == answerlistx[randomimage] , e2 == answerlisty[randomimage]: nameerror: global name 'pressed' not defined
inside
def gamescreen(): imagelist = ["1.gif","2.gif","3.gif","4.gif","5.gif","6.gif","7.gif","8.gif","9.gif","10.gif","11.gif","12.gif","13.gif","14.gif","15.gif","16.gif","17.gif","18.gif","19.gif","20.gif","21.gif","22.gif","23.gif","24.gif","25.gif","26.gif","27.gif","28.gif","29.gif","30.gif","31.gif","32.gif","33.gif","34.gif","35.gif","36.gif","37.gif","38.gif","39.gif","40.gif","41.gif","42.gif","43.gif","44.gif","45.gif","46.gif","47.gif","48.gif","49.gif","50.gif"] answerlistx = [-1, -3, 3, 4, -7, 8, 9, 10, -10, 2, -7, 7, -1, -5, -6, -9, -7, 10, -4, 8, 1, -8, -10, -1, -3, -7, -3, 7, 3, -4, 1, -8, -4, 9, -5, -10, 10, 2, 2, -10, 4, 9, -3, 6, 10, -6, 4, 9, -10, -10] answerlisty = [3, -6, -5, 3, -2, 4, -4, -3, 4, -6, 1, 2, 2, 3, 2, -1, -5, 1, -3, 1, -2, -2, -5, -3, -2, -6, -3, 6, 2, 0, -5, 6, -4, 4, 1, -6, 0, -6, 5, 2, 4, -4, -2, 0, -3, -6, -4, 1, -3, 1] canvas.bind("<button-1>", buttonclick_gamescreen) canvas.pack(expand = yes, fill = both) photo = photoimage(file="gamescreen.gif") canvas.create_image(1, 1, image = photo, anchor = nw) e1 = entry(canvas, width = 11) e2 = entry(canvas, width = 11) canvas.create_window(390, 501, window=e1, anchor = nw) canvas.create_window(551, 501, window=e2, anchor = nw) canvas.after(1, countdowntimer) while cdtimer != 0: randomimage = random.randrange(0,49+1) game = photoimage(file=imagelist[randomimage]) images = canvas.create_image(30, 65, image = game, anchor = nw) if pressed == 8 , e1 == answerlistx[randomimage] , e2 == answerlisty[randomimage]: canvas.delete(images) randomimage = random.randrange(0,49+1) scorecounter = scorecounter + 1 game = photoimage(file=imagelist[randomimage]) images = canvas.create_image(30, 65, image = game, anchor = nw) elif pressed == 8 , e1 != answerlistx[randomimage] or e2 != answerlisty[randomimage]: wronganswer = canvas.create_text(400, 200, text="incorrect", font="ubuntu 29 bold", fill=red, anchor=nw) e1.delete(0.0,end) e2.delete(0.0,end) canvas.after(1500,(canvas.delete(wronganswer)))
but have defined in
def buttonclick_gamescreen(event): global pressed pressed = "" if event.x >853 , event.x <957 , event.y > 8 , event.y < 56 : pressed = 7 if event.x >666 , event.x <947 , event.y > 491 , event.y < 534 : pressed = 8 if pressed == 7 : window.destroy() if pressed == 8: print("next button")
which confusing me. please explain me why happening , should fix it? in advance
ps: i'm trying here when 'next' button pressed check e1 , e2 , if string there matches answer in list randomly load image onto canvas.
though have declared global variable pressed in def buttonclick_gamescreen(event): problem def gamescreen(): getting called before def buttonclick_gamescreen(event):, , hence python runtime cannot lookup reference named pressed in memory, since line of code has never been executed. demonstrate how can effect, consider this:
def do_something(): global name name = 10 print name def do_somehting_else(): if name == 10: print "valid"
here name defined global inside function body. so, when call:
do_something() do_somehting_else()
we get,
>>> 10 >>> valid
but, if change order of function call to:
do_somehting_else() do_something()
the python runtime complains with:
traceback (most recent call last): file "/home/rahul/workspace/pytest/func.py", line 94, in <module> do_somehting_else() file "/home/rahul/workspace/pytest/func.py", line 91, in do_somehting_else if name == 10: nameerror: global name 'name' not defined
to solve this, if function call in control, execute them in right sequence. if not, create module level name pressed , access directly in def gamescreen():, way did in def buttonclick_gamescreen(event):. basically, this:
global pressed
hope helps :).
Comments
Post a Comment