简体   繁体   中英

Kivy images without kv language

I wanted to make a small Programm, in order to learn how to add images without using the kv language, but it did not worked. Here is the code:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Rectangle
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.core.image import Image

class Sprite(Image):
    def __init__(self, **kwargs):
        super(Sprite, self).__init__(**kwargs)
        self.size = self.texture_size

class Game(Widget):
    def __init__(self):
        super(Game, self).__init__()
        self.add_widget(Sprite(source='feind.png'))

class GameApp(App):
    def build(self):
        game = Game()
        Window.size = game.size
        return game

if __name__ == '__main__':
    GameApp().run()

This is the error message I get:

   File "g.py", line 10, in __init__
     super(Sprite, self).__init__(**kwargs)
 TypeError: __init__() takes exactly 2 arguments (1 given)

You're using the wrong Image class. You should use:

from kivy.uix.image import Image

instead of

from kivy.core.image import Image

The constructor of the kivy.core.image.Image actually requires a positional argument. That is the cause of the TypeError. But you probably don't want to use that class here. You most likely want to use an image widget, so you should use the kivy.uix.image.Image class. This one doesn't require a positional argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM