I'm making a program in Python with Turtle and encountered an error. I have no idea what I've done wrong and searched it up but could not pin point exactly were I messed up. I've dumbed down the code below so It's easier to understand.
Error:
File "h:\turtletest.py", line 22, in onclick
x = pos[0]
TypeError: 'NoneType' object is not subscriptable
Code:
import turtle
tr = turtle.Turtle()
wn = turtle.Screen()
wn.setup(width=600,height=600)
wn.bgcolor("black")
wn.title("Turtle OnClick test")
class ExampleClass:
def __init__(self, type_, width, height):
self.width = width
self.type_ = type_
self.height = height
def onclick(self):
def get_xy(x, y):
return (x, y)
if self.type_ == "rect":
pos = wn.onclick(get_xy)
x = pos[0]
y = pos[1]
if x >= self.width and x <= self.width:
print("Clicked!")
else:
pass
f = ExampleClass("rect", 25, 25)
f.onclick()
turtle.done()
I figured it out. I just modded the code slightly so that it's now in the actual function and not outside of it
def onclick(self):
def main_func(x, y):
print(x, y)
if not x >= self.width and not x <= self.width:
print("Clicked!")
else:
pass
if self.type_ == "rect":
scr.onclick(main_func)