简体   繁体   English

在 tkinter 错误中使用海龟(绘制正多边形时)

[英]Using turtle in tkinter error(while drawing regular polygon)

I wanted to draw regular polygon so I used turtle in tkinter.我想画正多边形,所以我在 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. 1.我想在tkinter的canvas上画一个正多边形。 However, when you press Run, the turtle graphics window appears and a regular polygon is drawn there.但是,当您按下 Run 时,会出现海龟图形 window 并在那里绘制一个正多边形。

2.When it finish drawing the regular polygon, the cursor doesn't stop, but it keeps moving. 2.正多边形绘制完成后,cursor并没有停止,而是一直在移动。

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.这是 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.首先,您应该在绘图时使用re_po而不是turtle

  • Second n=1 should be n -= 1 instead第二n=1应该是n -= 1

  • Final you should not call turtle.done()最后你不应该调用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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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