繁体   English   中英

如何在没有kv语言的Kivy中添加背景图片

[英]How to add background image in Kivy without kv language

我正在为桌面创建一个 Kivy 应用程序。 我已经创建了大部分应用程序,但我想向应用程序添加背景图像。 我没有使用 KV 语言,而是仅使用 Python 代码创建了所有小部件。 谁能帮我使用 Python 在 kivy 应用程序中添加背景图像。

您可以使用with canvas:来绘制背景图像。 这是一个简单的例子:

from kivy.app import App
from kivy.clock import Clock
from kivy.graphics.vertex_instructions import Rectangle
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label


class TestApp(App):
    def build(self):
        theRoot = FloatLayout()

        # draw the background
        with theRoot.canvas:
            self.rect = Rectangle(source='background.png')

        # use binding to insure that the background stay matched to theRoot
        theRoot.bind(on_size=self.update)
        theRoot.add_widget(Label(text="Hi", size_hint=(None, None), size=(100, 50), pos=(100,100)))

        # need to call update() to get background sized correctly at start
        Clock.schedule_once(self.update, -1)
        return theRoot

    def update(self, *args):
        # set the size and position of the background image
        self.rect.size = self.root.size
        self.rect.pos = self.root.pos


TestApp().run()

暂无
暂无

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

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