How to open the youtube link from a youtube video embed in a PyQt5 Widget in Python?
-
Environment:
Python 3.7
PyQT5 5.15.2I have a GUI with some links and Youtube videos embed inside a QWidget.
A GUI is a user interface. I've done mine for my software with Python PyQT5.
I want to show some video tutorials from YOutube inside my GUI.So I incorporate the youtube iframe HTML code inside a Qwidget (vlayout & webview, see code below).
The GUI is loading fine, and the videos are playing well. But they are too small. So my users will click on the youtube link over the video:
When I click on this Youtube link when playing the video, it supposes to open a youtube video page from my browser. It is useful to watch it directly from youtube instead of from my GUI.
But the link doesn't work. it doesn't do anything. Something is wrong.SO I tried to play with 'setOpenExternalLinks' & 'centralwid', but it doesn't work. The widget for my youtube doesn't have this attribute.
AttributeError: 'QWidget' object has no attribute 'setOpenExternalLinks'
Why?
Here is the code of some youtube videos and labels:
# Code for 1 Youtube video and its QtWidget # ====================================================================================== self.centralwid = QtWidgets.QWidget(self.tab_run) self.centralwid.setGeometry(QtCore.QRect(60, 40, 410, 258)) self.centralwid.setObjectName("centralwid") self.label_loading_browsers = QtWidgets.QLabel(self.centralwid) # ===================== HERE IS THE CODE FOR IFRAM YOUTUBE ============================ self.vlayout = QtWidgets.QVBoxLayout() self.webview = QtWebEngineWidgets.QWebEngineView() self.webview.settings().setAttribute(QWebEngineSettings.FullScreenSupportEnabled, True) self.webview.page().fullScreenRequested.connect(lambda request: request.accept()) baseUrl = "local" htmlString = """ <iframe width="350" height="212" src="https://www.youtube.com/embed/g8NVwN0_mks?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe> """ self.webview.setHtml(htmlString, QUrl(baseUrl)) self.vlayout.addWidget(self.webview) self.centralwid.setLayout(self.vlayout)
I search for all the properties and methods of my object qwidget.
I added these 2 lines but it didn't change anything:self.webview.settings().setAttribute(QWebEngineSettings.LocalContentCanAccessRemoteUrls, True) self.webview.settings().setAttribute(QWebEngineSettings.AllowRunningInsecureContent, True)
Here is MiniMum Reproductable COde (just copy paste in a py file and execute, you may need to resize window of gui to see the video)
import sys from PyQt5 import QtWidgets from PyQt5 import QtCore from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow from PyQt5.QtCore import QUrl, Qt from PyQt5 import QtWebEngineWidgets from PyQt5.QtWebEngineWidgets import QWebEngineSettings # Subclass QMainWindow to customise your application's main window class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setWindowTitle("My Awesome App") label = QLabel("This is a PyQt5 window!") # The `Qt` namespace has a lot of attributes to customise # widgets. See: http://doc.qt.io/qt-5/qt.html label.setAlignment(Qt.AlignCenter) # Set the central widget of the Window. Widget will expand # to take up all the space in the window by default. self.setCentralWidget(label) # Code for 1 Youtube video and its QtWidget # ====================================================================================== self.centralwid = QtWidgets.QWidget(self) self.centralwid.setGeometry(QtCore.QRect(60, 40, 410, 258)) self.centralwid.setObjectName("centralwid") self.label_loading_browsers = QtWidgets.QLabel(self.centralwid) # ===================== HERE IS THE CODE FOR IFRAM YOUTUBE ============================ self.vlayout = QtWidgets.QVBoxLayout() self.webview = QtWebEngineWidgets.QWebEngineView() self.webview.settings().setAttribute(QWebEngineSettings.FullScreenSupportEnabled, True) self.webview.settings().setAttribute(QWebEngineSettings.LocalContentCanAccessRemoteUrls, True) self.webview.settings().setAttribute(QWebEngineSettings.AllowRunningInsecureContent, True) self.webview.page().fullScreenRequested.connect(lambda request: request.accept()) baseUrl = "local" htmlString = """ <iframe width="350" height="212" src="https://www.youtube.com/embed/g8NVwN0_mks?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe> """ self.webview.setHtml(htmlString, QUrl(baseUrl)) self.vlayout.addWidget(self.webview) self.centralwid.setLayout(self.vlayout) app = QApplication(sys.argv) window = MainWindow() window.show() app.exec_()
-
Environment:
Python 3.7
PyQT5 5.15.2I have a GUI with some links and Youtube videos embed inside a QWidget.
A GUI is a user interface. I've done mine for my software with Python PyQT5.
I want to show some video tutorials from YOutube inside my GUI.So I incorporate the youtube iframe HTML code inside a Qwidget (vlayout & webview, see code below).
The GUI is loading fine, and the videos are playing well. But they are too small. So my users will click on the youtube link over the video:
When I click on this Youtube link when playing the video, it supposes to open a youtube video page from my browser. It is useful to watch it directly from youtube instead of from my GUI.
But the link doesn't work. it doesn't do anything. Something is wrong.SO I tried to play with 'setOpenExternalLinks' & 'centralwid', but it doesn't work. The widget for my youtube doesn't have this attribute.
AttributeError: 'QWidget' object has no attribute 'setOpenExternalLinks'
Why?
Here is the code of some youtube videos and labels:
# Code for 1 Youtube video and its QtWidget # ====================================================================================== self.centralwid = QtWidgets.QWidget(self.tab_run) self.centralwid.setGeometry(QtCore.QRect(60, 40, 410, 258)) self.centralwid.setObjectName("centralwid") self.label_loading_browsers = QtWidgets.QLabel(self.centralwid) # ===================== HERE IS THE CODE FOR IFRAM YOUTUBE ============================ self.vlayout = QtWidgets.QVBoxLayout() self.webview = QtWebEngineWidgets.QWebEngineView() self.webview.settings().setAttribute(QWebEngineSettings.FullScreenSupportEnabled, True) self.webview.page().fullScreenRequested.connect(lambda request: request.accept()) baseUrl = "local" htmlString = """ <iframe width="350" height="212" src="https://www.youtube.com/embed/g8NVwN0_mks?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe> """ self.webview.setHtml(htmlString, QUrl(baseUrl)) self.vlayout.addWidget(self.webview) self.centralwid.setLayout(self.vlayout)
I search for all the properties and methods of my object qwidget.
I added these 2 lines but it didn't change anything:self.webview.settings().setAttribute(QWebEngineSettings.LocalContentCanAccessRemoteUrls, True) self.webview.settings().setAttribute(QWebEngineSettings.AllowRunningInsecureContent, True)
Here is MiniMum Reproductable COde (just copy paste in a py file and execute, you may need to resize window of gui to see the video)
import sys from PyQt5 import QtWidgets from PyQt5 import QtCore from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow from PyQt5.QtCore import QUrl, Qt from PyQt5 import QtWebEngineWidgets from PyQt5.QtWebEngineWidgets import QWebEngineSettings # Subclass QMainWindow to customise your application's main window class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setWindowTitle("My Awesome App") label = QLabel("This is a PyQt5 window!") # The `Qt` namespace has a lot of attributes to customise # widgets. See: http://doc.qt.io/qt-5/qt.html label.setAlignment(Qt.AlignCenter) # Set the central widget of the Window. Widget will expand # to take up all the space in the window by default. self.setCentralWidget(label) # Code for 1 Youtube video and its QtWidget # ====================================================================================== self.centralwid = QtWidgets.QWidget(self) self.centralwid.setGeometry(QtCore.QRect(60, 40, 410, 258)) self.centralwid.setObjectName("centralwid") self.label_loading_browsers = QtWidgets.QLabel(self.centralwid) # ===================== HERE IS THE CODE FOR IFRAM YOUTUBE ============================ self.vlayout = QtWidgets.QVBoxLayout() self.webview = QtWebEngineWidgets.QWebEngineView() self.webview.settings().setAttribute(QWebEngineSettings.FullScreenSupportEnabled, True) self.webview.settings().setAttribute(QWebEngineSettings.LocalContentCanAccessRemoteUrls, True) self.webview.settings().setAttribute(QWebEngineSettings.AllowRunningInsecureContent, True) self.webview.page().fullScreenRequested.connect(lambda request: request.accept()) baseUrl = "local" htmlString = """ <iframe width="350" height="212" src="https://www.youtube.com/embed/g8NVwN0_mks?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe> """ self.webview.setHtml(htmlString, QUrl(baseUrl)) self.vlayout.addWidget(self.webview) self.centralwid.setLayout(self.vlayout) app = QApplication(sys.argv) window = MainWindow() window.show() app.exec_()
@CentreFF said in How to open the youtube link from a youtube video embed in a PyQt5 Widget in Python?:
SO I tried to play with 'setOpenExternalLinks' & 'centralwid', but it doesn't work. The widget for my youtube doesn't have this attribute.
AttributeError: 'QWidget' object has no attribute 'setOpenExternalLinks'Why?
I cannot comment on whether this is the right thing to do. But the error is because
QWidget
does not have methodsetOpenExternalLinks()
. That exists on aQLabel
or aQTextBrowser
only. So that is the answer to "why?".