簡體   English   中英

在 tkinter 錯誤中使用海龜(繪制正多邊形時)

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

我想畫正多邊形,所以我在 tkinter 中使用了海龜。 它成功了,所以我可以用輸入數據繪制一個正多邊形。

但是,也存在一些問題。

1.我想在tkinter的canvas上畫一個正多邊形。 但是,當您按下 Run 時,會出現海龜圖形 window 並在那里繪制一個正多邊形。

2.正多邊形繪制完成后,cursor並沒有停止,而是一直在移動。

任何幫助表示贊賞!

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

這是 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()
  • 首先,您應該在繪圖時使用re_po而不是turtle

  • 第二n=1應該是n -= 1

  • 最后你不應該調用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