Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:function()参数1必须是代码,而不是str_Python_Typeerror_Turtle Graphics - Fatal编程技术网

Python TypeError:function()参数1必须是代码,而不是str

Python TypeError:function()参数1必须是代码,而不是str,python,typeerror,turtle-graphics,Python,Typeerror,Turtle Graphics,我正在根据自己的喜好重新制作海龟模块(fishcode是我重新制作的名称),但我遇到了一个无法修复的错误 TypeError:function()参数1必须是代码,而不是str 我已经在stackoverflow上搜索了错误并在这里找到了错误,但是这些都没有帮助 fishcode模块的代码: 导入海龟 类窗口(turtle.Screen): 定义初始化(自): turtle.Screen.\uuuuuu初始化\uuuuuuuuuuuuuuuuu(自我) 测试模块的.py文件的代码: 导入fis

我正在根据自己的喜好重新制作海龟模块(fishcode是我重新制作的名称),但我遇到了一个无法修复的错误

TypeError:function()参数1必须是代码,而不是str

我已经在stackoverflow上搜索了错误并在这里找到了错误,但是这些都没有帮助

fishcode模块的代码:

导入海龟
类窗口(turtle.Screen):
定义初始化(自):
turtle.Screen.\uuuuuu初始化\uuuuuuuuuuuuuuuuu(自我)
测试模块的.py文件的代码:

导入fishcode
bob=fishcode.Window()
所以我在导入fishcode时出错了 我希望它能做一个海龟屏。

来自:

TurtleScreen类将图形窗口定义为绘图海龟的游乐场。它的构造函数需要tkinter.Canvas或ScrolledCanvas作为参数。当海龟被用作某些应用程序的一部分时,应该使用它


函数Screen()返回TurtleScreen子类的单例对象。当turtle用作独立的图形工具时,应使用此功能作为单例对象,无法从其类继承。

您正在尝试从函数派生。只能从类派生

此外,根据上面最后的粗体部分,您将无法从TurtleScreen类派生。所以你不能做你想做的事


无论如何,如果你所做的只是包装海龟代码,那就不会是“翻拍”

我大体上同意@LightnessRacesinOrbit的回答,但我不同意:

此外,根据上面最后的粗体部分,您将无法 派生自TurtleScreen类。你就是不能做什么 你在试着去做

直到需要时才创建singleton实例,因此可以将
TurtleScreen
子类化。最好在tkinter下使用嵌入式turtle:

import tkinter
from turtle import TurtleScreen, RawTurtle

class YertleScreen(TurtleScreen):

    def __init__(self, cv):
        super().__init__(cv)

    def window_geometry(self):

        ''' Add a new method, or modify an existing one. '''

        width, height = self._window_size()
        return (-width//2, -height//2, width//2, height//2)

root = tkinter.Tk()

canvas = tkinter.Canvas(root)
canvas.pack(side=tkinter.LEFT)

screen = YertleScreen(canvas)

turtle = RawTurtle(screen)

print(screen.window_geometry())

turtle.dot(50)

screen.mainloop()

虽然我相信它也适用于独立的turtle,但从一个版本到下一个版本的变化可能更大。

检查turtle.Screen实际上是什么(可能不是您所想的)。可能重复“作为单例对象,从其类继承是不可能的。”这指的是
Screen()
,而不是
TurtleScreen
@Goyo的返回类型,现在我已经在上下文中重新阅读了该引用,我同意并从我的回答中删除了与该引用不同的意见。谢谢