파이썬 tkinter하고 터틀을 연결 할 수 있는 방법이 있을까요?
조회수 1548회
다음 ‘클릭하세요’ 버튼을 누르면 왼쪽 다각형과 오른쪽 빨간색으로 채워진 원을 그리는 프로그램을 작성하여라. 이 문제에서
- 왼쪽 다각형 : 선분의 길이는 처음에 100으로 시작하고 90도 회전할 때마다 길이를 5씩 줄인다.
- 오른쪽 원 : 좌표 (200,100)의 위치로 이동시켜 반지름이 50인 원을 그리고 빨간색을 채운다.
이 조건에 맞게 코딩을 해봤는데
from tkinter import *
import turtle
window = Tk()
button = Button(window, text="클릭하세요")
button.pack()
window.mainloop()
t = turtle.Turtle()
length = 100
while length > 0:
t.forward(length)
t.left(90)
length -= 5
t.penup()
t.goto(200, 100)
t.pendown()
t.begin_fill()
t.fillcolor("red")
t.circle(50)
t.end_fill()
turtle.mainloop()
이런 식으로 해봤는데 역시나 안되더라구요. 저 tkinter하고 터틀을 연결 할 수 있는 방법이 있을까요??
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
1 답변
-
https://snowdeer.github.io/python/2018/09/07/use-turtle-with-tkinter/
여기 잘 설명이 되어 있네요. 위를 참고해서 질문자의 코드를 간단히 합쳐보았습니다.
from tkinter import * import turtle window = Tk() canvas = Canvas(master=window, width=500, height=500) canvas.pack() t = turtle.RawTurtle(canvas) def draw_turtle(): length = 100 while length > 0: t.forward(length) t.left(90) length -= 5 t.penup() t.goto(200, 100) t.pendown() t.begin_fill() t.fillcolor("red") t.circle(50) t.end_fill() button = Button(window, text="클릭하세요", command=draw_turtle) button.pack() window.mainloop()
댓글 입력