简体   繁体   English

如何在Kivy中设置背景图像? (图像继续覆盖所有其他按钮和对象)

[英]How To Set Background Image in Kivy? (Image keeps on covering all other buttons & objects)

I'm new to Kivy and I'm trying to create a simple desktop GUI for a small project. 我是Kivy的新手,我正在尝试为一个小项目创建一个简单的桌面GUI。 I found it confusing however, how to add a background image, and have spent days looking through the Kivy documentations and tutorials and forums trying to find a solution, but for some reason there is no explicit instructions on how to go about it. 然而,我发现它令人困惑,如何添加背景图像,并花了几天时间查看Kivy文档,教程和论坛试图找到解决方案,但由于某种原因,没有明确说明如何去做。 I really need your help. 我真的需要你的帮助。 The instructions here did not help at all, as the Kivy docs were about adding a background color and having a picture over it. 这里的说明根本没有帮助,因为Kivy文档是关于添加背景颜色并在其上面有图片。 That's not what I need, I need to add a background picture for all the buttons and objects. 这不是我需要的,我需要为所有按钮和对象添加背景图片。 I don't know how to send the background image backwards it keeps on surfacing above everything. 我不知道如何向后发送背景图像,它一直在表面上方。 I hope you can help me. 我希望你能帮助我。

Here is my code: 这是我的代码:

# -*- 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()

The resulting kivy output looks like this: 生成的kivy输出如下所示:

在此输入图像描述

All help will be appreciated. 所有帮助将不胜感激。 Thank you. 谢谢。 Thank you very much. 非常感谢你。

Add this after your <MenuScreen>: tag: <MenuScreen>:标记后添加:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM