How to set a screen background image in Kivy -
i know how make color background can't seem find useful setting image background , grateful code.
here .py file:
from kivy.app import app kivy.uix.screenmanager import screenmanager, screen, fadetransition kivy.uix.boxlayout import boxlayout kivy.uix.floatlayout import floatlayout kivy.uix.gridlayout import gridlayout #from kivy.uix.label import label kivy.uix.button import button kivy.core.window import window kivy.core.image import image #from kivy.graphics import borderimage 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: # part doesn't seem 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"
canvas.before: rectangle: pos: self.pos size: self.size image: source: "lights.png"
image widget, can't place on canvas.
what *can either set source rectangle:
canvas.before: rectangle: pos: self.pos size: self.size source: 'lights.png'
...or place image behind other widgets putting them in container layout.
Comments
Post a Comment