Miguel
Miguel

Reputation: 131

How do I make Python open a Youtube video?

I'm building a simple GUI in python with some text and a button inside. I want to click on that button and open a youtube video.

How would I do that? Mouse event? is there some kind of onclick function like in javascript?

here is my code

from tkinter import*

window = Tk()
window.title("DdP Program")
window.geometry("300x100")

def onClick(event):

label = Label(text="some text")
label.pack()

labelOne = Label(text="A little bit more text")
labelOne.pack()

click = Button(text = "Clica Aqui!", command = onClick)
click.pack()

window.mainloop()

Upvotes: 1

Views: 5205

Answers (1)

mehmetö
mehmetö

Reputation: 133

You can use webbrowser module from standard library

import webbrowser
url = "http://docs.python.org/library/webbrowser.html"
webbrowser.open(url,new=1)

so

import webbrowser
from tkinter import *

window = Tk()
window.title("DdP Program")
window.geometry("300x100")


def onClick(x):
    webbrowser.open(x,new=1)


label = Label(text="some text")
label.pack()

labelOne = Label(text="A little bit more text")
labelOne.pack()


url = "http://docs.python.org/library/webbrowser.html"

click = Button(text="Clica Aqui!", command=lambda: onClick(url))
click.pack()

window.mainloop()

We need to give argument but we can't use command=onClick(url) because it will invoke the function instantly (not when the button clicked) and assign its value to command so we can wrap the Onclick function with lambda function like this command=lambda: onClick(url). Lambda function will be invoked when button clicked and it will invoke onClick function with its argument

Upvotes: 6

Related Questions