简体   繁体   中英

Using turtle in tkinter error(while drawing regular polygon)

I wanted to draw regular polygon so I used turtle in tkinter. It works out so I can draw a regular polygon with input data.

However, there are some problems.

1.I want to draw a regular polygon on the canvas in tkinter. However, when you press Run, the turtle graphics window appears and a regular polygon is drawn there.

2.When it finish drawing the regular polygon, the cursor doesn't stop, but it keeps moving.

any help appreciated!!

Exception in Tkinter callback
Traceback (most recent call last):
  File "<string>", line 8, in forward
  File "C:\Users\samsung\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 1638, in forward
    self._go(distance)
  File "C:\Users\samsung\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 1606, in _go
    self._goto(ende)
  File "C:\Users\samsung\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 3182, in _goto
    screen._drawline(self.drawingLineItem, ((0, 0), (0, 0)),
  File "C:\Users\samsung\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 544, in _drawline
    self.cv.coords(lineitem, *cl)
  File "<string>", line 1, in coords
  File "C:\Users\samsung\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2766, in coords
    self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: invalid command name ".!canvas"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\samsung\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:/Users/samsung/Desktop/정다각형.py", line 19, in buttonclick3
    turtle.forward(side)
  File "<string>", line 12, in forward
turtle.Terminator

it's the error code in shell.

from tkinter import*

root=Tk()
root.title("도형 그리기")


def buttonclick3():
    import turtle


    re_po=turtle.RawTurtle(cv)

    n=ent_N.get()
    n=int(n)
    angle=360/n
    side=100

    while n!=0:
        turtle.forward(side)
        turtle.left(angle)
        n=1

    turtle.done()






lb5=Label(root, text="3.정N각형 그리기")
lb6=Label(root, text="N을 입력하세요.")


ent_N=Entry(root, width=70)

bt3=Button(root, text="정다각형 그리기", command=buttonclick3)

cv=Canvas(root, width=700, height=700, bg="white")



lb5.grid(row=8, column=0)
lb6.grid(row=9, column=0, columnspan=3)
ent_N.grid(row=10, column=0, columnspan=3)
bt3.grid(row=11, column=0, columnspan=3)

cv.grid(row=12, column=0, columnspan=3)


root.mainloop()
  • First you should use re_po instead of turtle on drawing.

  • Second n=1 should be n -= 1 instead

  • Final you should not call turtle.done()

def buttonclick3():
    import turtle

    re_po = turtle.RawTurtle(cv)

    n = ent_N.get()
    n = int(n)
    angle = 360 / n
    side = 100

    while n != 0:
        # use re_po instead of turtle
        re_po.forward(side)
        re_po.left(angle)
        # decrease n by 1
        n -= 1

    # don't call turtle.done()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM