Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__init__() got an unexpected keyword argument 'text'

I have been programming from the book Python for Absolute Beginners, using Python 3.3.1.

I am trying to add text to a screen using the following code. I need to stay in Python 3.3.1 but the code from the book I think is for Python 2.X.

from livewires import games, color

class Pizza(games.Sprite):
    """A falling pizza"""
    def __init__(self, screen, x,y, image, dx, dy):
        """Initialise pizza object"""
        self.init_sprite(screen = screen, x = x, y = y, image = image, dx = dx, dy = dy)

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

#main

my_screen = games.Screen(SCREEN_WIDTH, SCREEN_HEIGHT)

wall_image = games.load_image("wall.jpg", transparent = False)
pizza_image = games.load_image("pizza.jpg")
my_screen.set_background(wall_image)
games.Text(screen = my_screen, x = 500, y = 30, text = "Score: 1756521", size = 50, color = 

my_screen.mainloop()

However, when I run this program I get an error (see below)

  games.Text(screen = my_screen, x = 500, y = 30, text = "Score: 1756521", size = 50, color = color.black)
TypeError: __init__() got an unexpected keyword argument 'text'

I hope you can help

like image 581
user3375363 Avatar asked Mar 03 '14 15:03

user3375363


1 Answers

I replied with a comment, but I thought I would elaborate with a full answer.

I've just looked at the source code for the livewires games.py module as I suggested

class Text(Object, ColourMixin):
    """
    A class for representing text on the screen.

    The reference point of a Text object is the centre of its bounding box.
    """

    def __init__(self, screen, x, y, text, size, colour, static=0):
        self.init_text (screen, x, y, text, size, colour, static)
        .........

So as you can see, the __init__() for the Text class does not expect a keyword argument called text. Instead it expects a series of positional arguments.

So your code should look like this

games.Text(my_screen, 500, 30, "Score: 1756521", 50, color.black)

Edit:

As noted by 2rs2ts, you can provider keywords arguments to positional arguments if you specify all the argument names. However as you used the keyword color, not colour, your code failed. So the following should also work (but I would recommend the use positional arguments)

games.Text(screen=my_screen, x=500, y=30, text="Score: 1756521", size=50, colour=color.black)

According to PEP8 you should also note - "Don't use spaces around the = sign when used to indicate a keyword argument or a default parameter value."

like image 167
dannymilsom Avatar answered Sep 28 '22 03:09

dannymilsom