반응형

 

함수 설명 사용예
onkeypress(함수, "키 이름") 키보드를 눌렀을 때 실행할 함수를 정합니다 def f():
    t.forward(10)
t.onkeypress(f, "Up")
#위쪽 방향키를 누르면 f함수를 호출합니다. (f함수는 거북이를 10만큼 앞으로 이동시킵니다)
onscreenclick(함수) 마우스 버튼을 눌렀을 때 실행할 함수를 정합니다 t.onscreenclick(t.goto)
#마우스 버튼을 누르면 앞에서 정의한 goto함수를 호출합니다(goto 함수는 거북이를 마우스 버튼을 누른 위치로 이동시킵니다)
write("문자열") 현재 거북이 위치에 문자를 출력합니다 t.write("Hello")
t.write("Hello", False, "center", ("", 20))
#현재 거북이 위치에 가운에 정렬로 크기가 20인 'Hello'를 출력합니다

 

[ 파이썬 거북이 - write("문자열")을 이용해서 지정한 위치( t.goto() )에 문자열을 출력해보세요 ]

import turtle as t1
t1.shape("turtle")

t1.up()   # 꼬리를 들고
t1.ht()   # 거북이를 숨기고

t2 = t1.Turtle()
t2.up()
t2.ht()

t1.goto(-200, 200)  # 거북이를 이동시키고
t1.write("거북이", False, "center",("",20))
t2.goto(-200, 100)  # 거북이를 이동시키고
t2.write("토끼", False, "center",("",15))

t1.goto(200, 200)  # 거북이를 이동시키고
t1.write("거북이2", False, "center",("",25))
t2.goto(100, 100)  # 거북이를 이동시키고
t2.write("토끼", False, "center",("",15))

t1.goto(200, -200)  # 거북이를 이동시키고
t1.write("거북이3", False, "center",("",30))
t2.goto(100, -100)  # 거북이를 이동시키고
t2.write("토끼", False, "center",("",15))

t1.goto(-200, -200)  # 거북이를 이동시키고
t1.write("거북이4", False, "center",("",35))
t2.goto(-200, -100)  # 거북이를 이동시키고
t2.write("토끼", False, "center",("",15))

 

[  파이썬 거북이 - onscreenclick(함수)를 사용해서 마우스 왼쪽버튼, 오른쪽 버튼, 휠을 클릭할때마다 문자열을 출력하세요 ]

import turtle as t1
t1.shape("turtle")

t1.up()

def move1(x, y):
    t1.goto(200, 200)
    t1.write("안녕하세요", False, "center", ("", 20))

def move2(x, y):
    t1.goto(-200, 200)
    t1.write("안녕하세요", False, "center", ("", 20))

def move3(x, y):
    t1.goto(-200, -200)
    t1.write("안녕하세요", False, "center", ("", 20))

    
t1.onscreenclick(move1, 1)  # 마우스 좌 클릭
t1.onscreenclick(move2, 2) # 마우스 휠 클릭
t1.onscreenclick(move3, 3) # 마우스 우 클릭
반응형
Posted by 명문코딩컴퓨터
,