Video inside float layout

85 views
Skip to first unread message

Mohit Narwani

unread,
May 17, 2022, 2:44:18 PM5/17/22
to Kivy users support
I am new to kivy and trying  to play mp4 video inside float layout , but getting error is there any way to play video inside float layout

below is my main file

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager
from kivy.clock import Clock
from kivy.core.text import LabelBase
from kivymd.uix.label import MDLabel
from kivy.properties import StringProperty,NumericProperty
from kivymd.uix.textfield import *
import lk_k
Window.keyboard_anim_args = {"d":.2,"t":"in_out_expo"}
Window.softinput_mode = ""

class Command(MDLabel):
    text = StringProperty()
    size_hint_x = NumericProperty()
    halign = StringProperty()
    font_size=26

class Response(MDLabel):
    text = StringProperty()
    size_hint_x = NumericProperty()
    halign = StringProperty()
    font_size=26

class Chat_Bot(MDApp):
    def change_screen(self, name):
        screen_manager.current = name

    def build(self):
        global screen_manager
        screen_manager = ScreenManager()
        screen_manager.add_widget(Builder.load_file("Chats.kv"))
        return screen_manager

    def bot_name(self):
        if screen_manager.get_screen('main').bot_name.text != "":
           screen_manager.get_screen('chats').bot_name.text = screen_manager.get_screen('main').bot_name.text
           screen_manager.current = "chats"

    def response(self, *args):
        q=lk_k.get_response(message)
        if len(q)<6:
            s=.22
            h="center"
        elif len(q)<11:
            s=.32
            h="center"

        elif len(q) < 16:
            s = .45
            h = "center"
        elif len(q) < 21:
            s = .58
            h = "center"
        elif len(q) < 26:
            s = .71
            h = "center"
        else:
            s = .77
            h = "center"

           
        screen_manager.get_screen('chats').chat_list.add_widget(Response(text=q, size_hint_x=s,halign=h))

    def send(self):
        global message, size, halign
        if screen_manager.get_screen('chats').text_input != "":
            message = screen_manager.get_screen('chats').text_input.text

            if len(message)<6:
                size=.22
                halign="center"
            elif len(message)<11:
                size=.32
                halign="center"

            elif len(message) < 16:
                size = .45
                halign = "center"
            elif len(message) < 21:
                size = .58
                halign = "center"
            elif len(message) < 26:
                size = .71
                halign = "center"
            else:
                size = .77
                halign = "center"
            screen_manager.get_screen('chats').chat_list.add_widget(Command(text=message,size_hint_x=size,halign=halign))
            Clock.schedule_once(self.response, 1)
            screen_manager.get_screen('chats').text_input.text = ""


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

 and here is my .kv file

<Command>
    size_hint_y:None
    pos_hint:{"right": .9}
    height: self.texture_size[1]
    padding: 12,10
    theme_text_color: "Custom"
    text_color:53/255,56/255,60/255,1
    canvas.before:
        Color:
            rgb: (1, 1,1,1)
        RoundedRectangle:
            size:self.width,self.height
            pos:self.pos
            radius:[23,0,23, 23]
<Response>
    size_hint_y:None
    pos_hint:{"left": .30}
    height: self.texture_size[1]
    padding: 10,12
    theme_text_color: "Custom"
    text_color: 53/255,56/255,60/255,1
    canvas.before:
        Color:
            rgb: ( 0,1,1,1)
        RoundedRectangle:
            size:self.width,self.height
            pos:self.pos
            radius:[0,23,23,23]

MDScreen:
    bot_name: bot_name
    text_input: text_input
    chat_list: chat_list
    name: "chats"
    MDFloatLayout:
        canvas :
            Color:
                rgb:1,1,1, 1
            Rectangle:
                source:'emma.mp4'
                size:self.size
                pos:self.pos
        MDFloatLayout:
            md_bg_color:127/255,1, 212/255,1
            size_hint_y:.11
            pos_hint: {"center_y":.96}

            MDLabel:
                id: bot_name
                text:"OLIVIA"
                font_size: "25sp"
                pos_hint: {"center_y": .35}
                halign: "center"
                theme_text_color: "Custom"
                text_color: 53/255,56/255,60/255,1

            MDIconButton:
                icon:"emma.png"
               
                pos_hint:{"center_x":.2,"center_y":.34}
                user_font_size:"15sp"
                theme_text_color:"Custom"
                text_color:127/255,1, 212/255, 1
                md_bg_color: 1,1,1,1
                on_press:app.send()
            MDIconButton:
                icon:"video-outline"          
                pos_hint:{"center_x":.80,"center_y":.35}
                user_font_size:"31sp"
                theme_text_color:"Custom"
                text_color:53/255,56/255,60/255,1

                on_press:app.send()


        ScrollView:
            size_hint_y:.78
            background_color:1,1,1,1
            pos_hint:{"x":0,"y":.116}
            do_scroll_x:False
            do_scroll_y:True
            BoxLayout:
                id:chat_list
                orientation:'vertical'
                size:(root.width,root.height)
                height:self.minimum_height
                size_hint:None, None
                pos_hint:{"top": 1}
                cols:1
                spacing:3
                       
           
        MDFloatLayout:
            size_hint_y:.08
            md_bg_color:127/255,1, 212/255, 1
            MDFloatLayout:
                size_hint:.8, .75
                pos_hint:{"center_x":.43,"center_y":.5}
                md_bg_color:127/255,1, 212/255, 1
                canvas:
                    Color:
                        rgb:1,1,1, 1
                    RoundedRectangle:
                        size:self.size
                        pos:self.pos
                        radius:[0, 23, 0, 23]
                TextInput:
                    id:text_input
                    hint_text:"Type your message"
                    size_hint:1, None
                    pos_hint:{"center_x":.5,"center_y":.5}
                    multiline:False
                    font_size:"18sp"
                    height:self.minimum_height
                    cursor_color:1, 170/255, 23/255, 1
                    cursor_width:"2sp"
                    foreground_color:53/255,56/255,60/255,1
                    background_color:0,0,0,0
                    padding:30
                   
             
            MDIconButton:
                icon:"send-outline"
                pos_hint:{"center_x":.91,"center_y":.5}
                user_font_size:"23sp"
                theme_text_color:"Custom"
                text_color:204,0,204,1
                md_bg_color: 127/255,1, 212/255, 1
                on_press:app.send()

Elliot Garbus

unread,
May 18, 2022, 3:34:12 PM5/18/22
to kivy-...@googlegroups.com

Yes,

You can play a video in a FloatLayout.  I do not see a Video widget in your code below.  I suggest starting with a simple example to get a video playing in a FloatLayout.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/c3ef76ad-7af4-409c-8beb-51efd18bb448n%40googlegroups.com.

 

Mohit Narwani

unread,
May 18, 2022, 6:58:54 PM5/18/22
to Kivy users support
yes I did tried that and here is my code I just don't know how to use it inside floatlayout

from kivy.app import App

from kivy.uix.video import Video



class ScreensApp(App):
    def build(self):
        video = Video(source ="k.mp4" )
        video.state = "play"
        video.options= {'eos':'loop'}
        video.allow_stretch = True
        return video
       

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

Elliot Garbus

unread,
May 18, 2022, 10:09:41 PM5/18/22
to kivy-...@googlegroups.com

Here is an example.  I suspect the issue is setting the state to play at the same time you are creating with widget.

In the example below I create a Boxlayout, put the video in a FloatLayout nested in the BoxLayout (although that is not really needed here.)

I use a BoxLayout at the bottom to create a play and stop button to control the video.

 

The advantage of a FloatLayout is that it honors the position and size of the child widget relative to the window.  If you wanted the video a specific size and a specific window position, a FloatLayout is an appropriate tool. 

 

 

 

from kivy.app import App
from kivy.lang import Builder

kv =
"""
BoxLayout:
    orientation: 'vertical'
    FloatLayout:
        Video:
            id:video
            source: 'sample-mp4-file.mp4'
            allow_stretch: True
    BoxLayout:
        size_hint_y: None
        height: 48
        Button:
            text: 'Play'
            on_release: video.state = 'play'
        Button:
            text: 'Stop'
            on_release: video.state = 'stop'

"""


class ScreensApp(App):
   
def build(self):
       
return Builder.load_string(kv)


Mohit Narwani

unread,
May 19, 2022, 6:17:38 AM5/19/22
to Kivy users support
thanks
Reply all
Reply to author
Forward
0 new messages