[英]How to add background image in Kivy without kv language
I'm creating an Kivy App for Desktop.我正在为桌面创建一个 Kivy 应用程序。 I've created most of the app but I want to add a background image to the app.
我已经创建了大部分应用程序,但我想向应用程序添加背景图像。 I've not use the KV language but created all the widgets using Python code only.
我没有使用 KV 语言,而是仅使用 Python 代码创建了所有小部件。 Can anybody please help me adding a background image in the kivy app using Python.
谁能帮我使用 Python 在 kivy 应用程序中添加背景图像。
You can use with canvas:
to draw a background image.您可以使用
with canvas:
来绘制背景图像。 Here is a simple example:这是一个简单的例子:
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.