Skip to content
Advertisement

Turtle Graphics: Onscreenclick doesn’t work

학번, 이름, 전공

def Color():
    import random as r
    color=['yellow','red','blue','black','green'] #5색
    return r.choice(color)

def Shape():
    import turtle as t
    import random as r

    t.up()
    t.onscreenclick(t.goto) #클릭한 곳으로 거북이 이동
    t.pencolor(Color()) #펜 색깔 결정
    t.down()
    t.speed('fastest') #가장 빠른 속도로 거북이 설정
    shape = [0,3,4,5,6]
    result = r.choice(shape)
    line=r.randint(50,100) #50에서 100중에 random으로 반지름/변의 길이 설정


import turtle as t #turtle graphic import
import random as r #random import


t.onscreenclick(t.goto)
Shape()
if (t.onkeypress("Space")) : #Space를 눌렀을 때 채우기 함수 실행
    t.onscreenclick(Fill())
if (t.onkeypress("Space")):
     t.onscreenclick(Shape())
if (t.onscreenclick("c")):
    Clear()

t.mainloop()

it says that Exception in Tkinter callback Traceback (most recent call last): File “C:UserssuyeoAppDataLocalProgramsPythonPython39libtkinter_init_.py”, line 1892, in call return self.func(*args) File “C:UserssuyeoAppDataLocalProgramsPythonPython39libturtle.py”, line 674, in eventfun fun(x, y) TypeError: ‘str’ object is not callable

Advertisement

Answer

onscreenclick and onkeypress setup callbacks – functions that are called when those events happen. You only need to call these functions once for each event or key – unless you need to modify them. AFAIK, they don’t return any values. onscreenclick takes one parameter: a function that takes 2 parameters, the x and y coordinates. onkeypress takes 2 parameters: the callback function and the key.

import turtle as t
import random as r

def fill():
    # TODO: Add your code here
    pass

def clear():
    # TODO: Add your code here
    pass

def color():
    color=['yellow','red','blue','black','green']
    return r.choice(color)

def shape(x, y):
    t.pencolor(color())
    t.goto(x, y)

t.speed('fastest')

# Setup the callbacks. The first parameter to each is a function "pointer"
t.onscreenclick(shape)
t.onkeypress(fill, "space")
t.onkeypress(clear, "c")

shape(0, 0)

t.listen()
t.mainloop()
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement