Is there any way to connect Python tkinter and Turtle?

Asked 2 years ago, Updated 2 years ago, 124 views

Write a program that draws a circle filled with the left polygon and the right red when you press the next 'click' button. In this issue I tried coding according to these conditions.

from tkinter import *
import turtle

window = Tk()
button = Button (window, text = "Click")
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()

I tried it like this, but it didn't work. Is there any way to connect that tkinter and turtleneck?

python tkinter turtle

2022-09-20 15:53

1 Answers

https://snowdeer.github.io/python/2018/09/07/use-turtle-with-tkinter/

It's well explained here. I briefly combined the code of the questioner by referring to the above.

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 = "click", command = draw_turtle)
button.pack()

window.mainloop()



2022-09-20 15:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.