Python

Peer

Well-Known Member
Joined
May 17, 2018
Messages
587
Reaction score
305
Credits
402
Why do i get this output:
Code:
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/home/arch/python/spaceacard.py", line 10, in UpKey
    Ship.sety(x+10)
UnboundLocalError: local variable 'x' referenced before assignment
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/home/arch/python/spaceacard.py", line 14, in DownKey
    Ship.sety(x-10)
UnboundLocalError: local variable 'x' referenced before assignment
running this:
Code:
#!/usr/bin/python3
import turtle
from tkinter import *

e=0
x=0
Ship=turtle.Turtle()
Ship.penup()
def UpKey(event):
    Ship.sety(x+10)
    x=x+10

def DownKey(event):
    Ship.sety(x-10)
    x=x-10

main = Tk()

while e==0:  
    main.bind('<Up>', UpKey)
    main.bind('<Down>', DownKey)
   
    main.mainloop()
and pressing arrow-keys
 


Never forget to global your variables a dirty solution is `global X` next you can use it
 
  • Like
Reactions: Rob

Members online


Latest posts

Top