我是Kivy的新手,我正在尝试为一个小项目创建一个简单的桌面GUI。但是,我发现如何添加背景图像令人困惑,并且花了几天时间浏览Kivy文档,教程和论坛,试图找到解决方案,但是由于某些原因,没有明确的说明。我真的需要你的帮助。说明here完全没有帮助,因为Kivy文档是关于添加背景色并在其上铺有图片的。那不是我所需要的,我需要为所有按钮和对象添加背景图片。我不知道如何向后发送背景图片,使背景图片始终覆盖所有内容。我希望你能帮助我。
这是我的代码:
# -*- coding: cp1252 -*-
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.animation import Animation
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.clock import Clock
from kivy.graphics import Color, Rectangle
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.floatlayout import FloatLayout
# Create both screens. Please note the root.manager.current: this is how
# you can control the ScreenManager from kv. Each screen has by default a
# property manager that gives you the instance of the ScreenManager used.
Builder.load_string("""
<MenuScreen>:
FloatLayout:
Button:
text: "LEARN"
font_name: 'DK Lemon Yellow Sun.otf'
font_size: '50sp'
pos: 60, 180
size_hint: .4, .4
Button:
text: "TRANSLATE"
font_name: 'DK Lemon Yellow Sun.otf'
font_size: '50sp'
pos: 410, 180
size_hint: .4, .4
Button:
text: "QUIT"
font_size: '50sp'
font_name: 'DK Lemon Yellow Sun.otf'
pos: 60, 50
size_hint: .1, .1
Label:
text: 'Education App'
pos: -10, 200
font_size: '70sp'
font_name: 'DK Lemon Yellow Sun.otf'
AsyncImage:
source: 'blackboard.png'
size_hint: 1, .7
pos_hint: {'center_x':.5, 'center_y': .5}
<SettingsScreen>:
BoxLayout:
Button:
text: 'My settings button'
Button:
text: 'Back to menu'
on_press: root.manager.current = 'menu'
""")
class MenuScreen(Screen):
pass
class SettingsScreen(Screen):
pass
sm = ScreenManager(transition=FadeTransition())
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
所得的kivy输出如下:
所有帮助将不胜感激。谢谢。非常感谢。
答案 0 :(得分:0)
在您的<MenuScreen>:
标签之后添加此标签:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'yourImage.png'