학번, 이름, 전공
JavaScript
x
34
34
1
def Color():
2
import random as r
3
color=['yellow','red','blue','black','green'] #5색
4
return r.choice(color)
5
6
def Shape():
7
import turtle as t
8
import random as r
9
10
t.up()
11
t.onscreenclick(t.goto) #클릭한 곳으로 거북이 이동
12
t.pencolor(Color()) #펜 색깔 결정
13
t.down()
14
t.speed('fastest') #가장 빠른 속도로 거북이 설정
15
shape = [0,3,4,5,6]
16
result = r.choice(shape)
17
line=r.randint(50,100) #50에서 100중에 random으로 반지름/변의 길이 설정
18
19
20
import turtle as t #turtle graphic import
21
import random as r #random import
22
23
24
t.onscreenclick(t.goto)
25
Shape()
26
if (t.onkeypress("Space")) : #Space를 눌렀을 때 채우기 함수 실행
27
t.onscreenclick(Fill())
28
if (t.onkeypress("Space")):
29
t.onscreenclick(Shape())
30
if (t.onscreenclick("c")):
31
Clear()
32
33
t.mainloop()
34
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.
JavaScript
1
31
31
1
import turtle as t
2
import random as r
3
4
def fill():
5
# TODO: Add your code here
6
pass
7
8
def clear():
9
# TODO: Add your code here
10
pass
11
12
def color():
13
color=['yellow','red','blue','black','green']
14
return r.choice(color)
15
16
def shape(x, y):
17
t.pencolor(color())
18
t.goto(x, y)
19
20
t.speed('fastest')
21
22
# Setup the callbacks. The first parameter to each is a function "pointer"
23
t.onscreenclick(shape)
24
t.onkeypress(fill, "space")
25
t.onkeypress(clear, "c")
26
27
shape(0, 0)
28
29
t.listen()
30
t.mainloop()
31