1

I am remaking the turtle module (fishcode is the name of my remake) to my taste, but I came across a error I couldn't fix.

TypeError: function() argument 1 must be code, not str

I've searched the error up and found the error here on stackoverflow, but those didn't help.

Code of fishcode module:

import turtle

class Window(turtle.Screen):
    def __init__(self):
        turtle.Screen.__init__(self)

Code of .py file that tests the module:

import fishcode

bob = fishcode.Window()

So I get the error at importing fishcode I expect it to make a turtle screen.

cdlane
  • 40,441
  • 5
  • 32
  • 81
wotman
  • 45
  • 1
  • 2
  • 9
  • 1
    Check what `turtle.Screen`actually is (probably not what you think). – Stop harming Monica Jun 15 '19 at 15:54
  • Possible duplicate of [Error when calling the metaclass bases: function() argument 1 must be code, not str](https://stackoverflow.com/questions/2231427/error-when-calling-the-metaclass-bases-function-argument-1-must-be-code-not) – Stop harming Monica Jun 15 '19 at 16:00

2 Answers2

2

From the Turtle documentation:

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're trying to derive from a function. You can't do that. You can only derive from classes.

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.

It wouldn't be a "remake", anyway, if all you did were to wrap the Turtle code. ;)

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

I generally agree with @LightnessRacesinOrbit's answer but I don't agree with:

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.

The singleton instance isn't created until needed, so it's possible to subclass TurtleScreen. This is probably best done when using embedded turtle under tkinter:

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()

Though I believe it will also work for standalone turtle, though that may be more likely to change from one release to the next.

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • "As a singleton object, inheriting from its class is not possible." This refers to the return type of `Screen()`, not `TurtleScreen`. – Stop harming Monica Jun 18 '19 at 07:54
  • @Goyo, now that I've reread the quote in context, I agree and have removed that particular disagreement with it from my answer. Thanks. – cdlane Jun 18 '19 at 16:03