Reputation: 58
I have tried to embed a YouTube in my app using kivy but the code wasn't working. This the code I wrote.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.webview import WebView
class YouTubePlayer(BoxLayout):
def __init__(self, **kwargs):
super().__init__(orientation="vertical", **kwargs)
self.url_input = TextInput(
hint_text="Enter YouTube Video ID (e.g., dQw4w9WgXcQ)",
size_hint_y=None,
height=40,
)
self.add_widget(self.url_input)
self.play_button = Button(text="Play Video", size_hint_y=None, height=50)
self.play_button.bind(on_press=self.load_video)
self.add_widget(self.play_button)
self.video_label = Label(text="YouTube Video Player", size_hint_y=None, height=40)
self.add_widget(self.video_label)
self.webview = WebView()
self.add_widget(self.webview)
def load_video(self, instance):
video_id = self.url_input.text.strip()
if video_id:
youtube_embed_url = f"https://www.youtube.com/embed/{video_id}?autoplay=1"
self.webview.url = youtube_embed_url
else:
self.video_label.text = "Please enter a valid video ID"
class YouTubeApp(App):
def build(self):
return YouTubePlayer()
if __name__ == "__main__":
YouTubeApp().run()
however, this error happens:
Traceback (most recent call last):
File "c:\python_project\youtube\main.py", line 6, in <module>
from kivy.uix.webview import WebView
ModuleNotFoundError: No module named 'kivy.uix.webview'
my question is is there any solution for this problem? or any other way to embed Youtube video in my app while I am using kivy?
Upvotes: 0
Views: 27