tkinter tk wiget turtle python

时间:2017-03-23 09:46:27

标签: python python-3.x tkinter tk

我正在尝试使用tkinter.Tk()类进行海龟游戏。

以下是代码:

from tkinter import *
import turtle

main = Tk(className="Castle Game")

s=turtle.TurtleScreen(cv=main,mode='standard', colormode=1.0, delay=10)
cover=turtle.RawTurtle(s)

尝试返回以下内容:

>>> ================================ RESTART ================================
>>> 
Traceback (most recent call last):
  File "H:\2 Computer science\Year 9\4Python\Castle.py", line 7, in <module>
    s=turtle.TurtleScreen(cv=main,mode='standard', colormode=1.0, delay=10)
  File "C:\Python33\lib\turtle.py", line 989, in __init__
    TurtleScreenBase.__init__(self, cv)
  File "C:\Python33\lib\turtle.py", line 488, in __init__
    self.cv.config(scrollregion = (-w//2, -h//2, w//2, h//2 ))
  File "C:\Python33\lib\tkinter\__init__.py", line 1263, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Python33\lib\tkinter\__init__.py", line 1254, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-scrollregion"
>>> 

1 个答案:

答案 0 :(得分:1)

在turtle图形中,cv选项代表画布,但你给它一个Tk。这就是为什么会有错误的原因。要解决这个问题,您需要这样做:

from tkinter import *
import turtle
root=Tk(className="Castle Game")
main = Canvas(root)
main.pack()
s=turtle.TurtleScreen(cv=main,mode='standard', colormode=1.0, delay=10)
cover=turtle.RawTurtle(s)

main更改为Canvas并将其打包到根目录。