18

I know how to make a color background but i can't seem to find anything useful for setting the image as a background and would be really grateful for any help with my code.

Here is my .py file:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
#from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.core.image import Image
#from kivy.graphics import BorderImage
from kivy.graphics import Color, Rectangle
#from kivy.uix.image import AsyncImage


class StartScreen(Screen):
    pass

class GameScreen(Screen):
    pass

class RootScreen(ScreenManager):
    pass


class MainApp(App):
    def build(self):
        return RootScreen()

if __name__ == "__main__":
    MainApp().run()

And .kv file:

#:import FadeTransition kivy.uix.screenmanager.FadeTransition

<RootScreen>:
    transition: FadeTransition() 
    StartScreen:
    GameScreen:

<StartScreen>:
    name: "start"
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
        Image:
            source: "lights.png"
    FloatLayout:
        Image:     # This part doesn't seem to work
            source: "lights.png"
            allow_stretch: True
            keep_ratio: False
            size_hint: 1, 1
        Button:
            text: "Play!"
            size_hint: 0.4, 0.3
            pos_hint: {'center_x':.5, 'center_y':.5}
            font_size: 70
            on_release: root.manager.current = "game"
<GameScreen>:
    name: "game"
    FloatLayout:
        Button:
            text: "Nazaj!"
            font_size: 70
            on_release: root.manager.current  = "start"
Larisa
  • 781
  • 1
  • 11
  • 27

2 Answers2

21
canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
        Image:
            source: "lights.png"

Image is a widget, you can't place it on the canvas.

What you *can do is either just set a source for the Rectangle:

canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'lights.png'

...or place your Image behind the other widgets by putting them in a container layout.

inclement
  • 29,124
  • 4
  • 48
  • 60
  • Thank you for your answer but unfortunately it doesn't seem to do anything. I tried adding the image directly on screen and as a widget in float layout, but none of them did any change, it doesn't display any image and i don't understand why. – Larisa Jul 02 '15 at 10:35
  • Sorry, it works, thank you! I just didn't have the image saved in the same directory. – Larisa Jul 02 '15 at 10:40
  • Thank you @inclement. This helped me with my app. – Pyzard Oct 22 '20 at 03:23
-1

Now there is a simple way to do it, here it just adds the FitImage

MDScreen:
    md_bg_color: 239/255, 239/255, 239/255, 1
    name:"entrance"
    FitImage:
        source:"images/yourimagepath.jpg"
    MDBoxLayout: