如何在Kivy中添加背景视频

How to add a background video in Kivy

我是 Kivy 的新手,有点卡住了。我正在使用 GridLayout 来制作我的应用程序,但我在将视频放入背景时遇到了一些问题。我将 post 的代码制作一个黑色背景的应用程序。如何用视频(尤其是 mp4)替换黑色背景。如果可能的话,我还想让视频更暗。我想使用 AnchorPoint 但我不太确定如何将两者都放在那里。任何帮助将不胜感激。

from kivy.app import App
from kivy.uix.video import Video
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput

class MZ_Invest(App):
    def build(self):
        self.root_layout = FloatLayout()
        self.window = GridLayout()
        self.window.cols = 1
        self.window.size_hint = (0.6,0.7)
        self.window.pos_hint = {"center_x":0.5, "center_y":0.5}
        #add widgets

        #Video
        video = Video(source='birds.mp4', state='play')
        video.opacity = 0.5


        #Image
        self.window.add_widget(Image(
            source="mzi.png",
            size_hint = (1.5,1.5)
        ))

        #Label
        self.greeting = Label(
            text="How much would you like to invest?",
            font_size = 18,
            color='90EE90'
        )
        self.window.add_widget(self.greeting)

        #User Input
        self.user = TextInput(
            multiline=False,
            padding_y= (20,20),
            size_hint = (1, 0.5)
        )
        self.window.add_widget(self.user)

        #Button
        self.button = Button(
            text="Submit",
            size_hint = (1,0.5),
            bold = True,
            background_color = '90EE90',

        )
        self.button.bind(on_press=self.callback)
        self.window.add_widget(self.button)

        #self.root_layout.add_widget(video)
        self.root_layout.add_widget(self.window)

        return self.root_layout

    def callback(self, instance):
        if self.user.text.isnumeric() and int(self.user.text) >= 10000:
            self.greeting.text = "Calculating: $" + self.user.text
            self.greeting.color = '90EE90'
        else:
            self.greeting.text = "Invalid"
            self.greeting.color = "#FF0000"

if __name__ == "__main__":
    MZ_Invest().run()

大多数 Kivy 小部件都有透明背景,因此您可以只显示视频,然后在其上显示您的 GUI。尝试在 build() 方法的末尾添加类似这样的内容:

    self.root_layout = FloatLayout()
    video = Video(source='BigBuckBunny.mp4', state='play')
    video.opacity = 0.5  # adjust to make the video lighter/darker
    self.root_layout.add_widget(video)  # add the video first
    self.root_layout.add_widget(self.window)  # now add the GUI window so it will be drawn over the video

    return self.root_layout  # return the root_layout instead of the window