gpt4 book ai didi

kivy - 如何在 Kivy 中设置屏幕背景图片

转载 作者:行者123 更新时间:2023-12-02 07:47:58 41 4
gpt4 key购买 nike

我知道如何制作彩色背景,但我似乎找不到任何有用的内容将图像设置为背景,并且非常感谢您对我的代码的任何帮助。

这是我的 .py 文件:

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"

图像是一个小部件,您不能将其放置在 Canvas 上。

您*可以做的就是为矩形设置一个源:

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

...或者将您的图像放置在其他小部件后面,将它们放入容器布局中。

关于kivy - 如何在 Kivy 中设置屏幕背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31179155/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com