本文介绍了如何在 Kivy 中设置屏幕背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何制作彩色背景,但我似乎找不到任何有用的东西来将图像设置为背景,非常感谢我的代码提供任何帮助.
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.
这是我的 .py 文件:
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()
还有.kv文件:
#: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"
推荐答案
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.
您*可以做的只是为 Rectangle 设置一个来源:
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.
这篇关于如何在 Kivy 中设置屏幕背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!