我一直在使用Python 3.3.1編寫「絕對初學者Python」一書。__init __()得到了一個意想不到的關鍵字參數'text'
我想使用下面的代碼將文本添加到屏幕。我需要留在Python 3.3.1中,但我認爲這本書中的代碼是針對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()
然而,當我運行這個程序,我得到一個錯誤(見下文)
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'
我希望你能幫助
games.Text()正在創建一個對象,但定義此對象的類內的__init __()並不期望被稱爲文本的關鍵字參數,因此是例外。我建議你看看games.Text類中的__init __()。 – dannymilsom