TypeError: function() argument 1 must be code, not str
TypeError: function() argument 1 must be code, not str
我正在根据自己的喜好重新制作 turtle 模块(fishcode 是我重新制作的名称),但我遇到了一个我无法修复的错误。
TypeError: function() argument 1 must be code, not str
我已经搜索了错误并在 Whosebug 上找到了错误,但这些都没有帮助。
fishcode模块代码:
import turtle
class Window(turtle.Screen):
def __init__(self):
turtle.Screen.__init__(self)
测试模块的.py文件代码:
import fishcode
bob = fishcode.Window()
所以我在导入 fishcode 时遇到错误
我希望它能做一个乌龟屏幕。
The TurtleScreen class defines graphics windows as a playground for the drawing turtles. Its constructor needs a tkinter.Canvas or a ScrolledCanvas as argument. It should be used when turtle is used as part of some application.
The function Screen() returns a singleton object of a TurtleScreen subclass. This function should be used when turtle is used as a standalone tool for doing graphics. As a singleton object, inheriting from its class is not possible.
您正在尝试从函数派生。 You can't do that. 您只能从 classes 派生。
此外,根据上面最后一段加粗的内容,您将无法从 TurtleScreen 派生 class。所以你就是做不到你想做的事。
它不会是 "remake",无论如何,如果您所做的只是包装 Turtle 代码。 ;)
我基本同意@LightnessRacesinOrbit 的回答,但我不同意:
Furthermore, per the final bolded passage above, you will not be able
to derive from the TurtleScreen class. So you just can't do what
you're trying to do.
直到需要时才创建单例实例,因此可以子类化 TurtleScreen
。在 tkinter 下使用 embedded 乌龟时,这可能是最好的做法:
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 模块(fishcode 是我重新制作的名称),但我遇到了一个我无法修复的错误。
TypeError: function() argument 1 must be code, not str
我已经搜索了错误并在 Whosebug 上找到了错误,但这些都没有帮助。
fishcode模块代码:
import turtle
class Window(turtle.Screen):
def __init__(self):
turtle.Screen.__init__(self)
测试模块的.py文件代码:
import fishcode
bob = fishcode.Window()
所以我在导入 fishcode 时遇到错误 我希望它能做一个乌龟屏幕。
The TurtleScreen class defines graphics windows as a playground for the drawing turtles. Its constructor needs a tkinter.Canvas or a ScrolledCanvas as argument. It should be used when turtle is used as part of some application.
The function Screen() returns a singleton object of a TurtleScreen subclass. This function should be used when turtle is used as a standalone tool for doing graphics. As a singleton object, inheriting from its class is not possible.
您正在尝试从函数派生。 You can't do that. 您只能从 classes 派生。
此外,根据上面最后一段加粗的内容,您将无法从 TurtleScreen 派生 class。所以你就是做不到你想做的事。
它不会是 "remake",无论如何,如果您所做的只是包装 Turtle 代码。 ;)
我基本同意@LightnessRacesinOrbit 的回答,但我不同意:
Furthermore, per the final bolded passage above, you will not be able to derive from the TurtleScreen class. So you just can't do what you're trying to do.
直到需要时才创建单例实例,因此可以子类化 TurtleScreen
。在 tkinter 下使用 embedded 乌龟时,这可能是最好的做法:
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,但从一个版本到下一个版本的变化可能更大。