wotman
wotman

Reputation: 55

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

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.

Upvotes: 1

Views: 8014

Answers (2)

cdlane
cdlane

Reputation: 41905

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.

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385305

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. ;)

Upvotes: 2

Related Questions