How Can I embed youtube video using PyQt5? I tried doing the following,but it gave me an unresolved error:
DirectShowService:doRender unresolved error code
from PyQt5 import QtWidgets,QtCore,QtGui
import sys, time
from PyQt5.QtCore import Qt,QUrl
from PyQt5 import QtWebKit
from PyQt5 import QtWebKitWidgets
from PyQt5.QtWebKit import QWebSettings
#from PyQt5 import QtWebEngineWidgets #import QWebEngineView,QWebEngineSettings
class window(QtWidgets.QMainWindow):
def __init__(self):
QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled,True)
super(window,self).__init__()
self.centralwid=QtWidgets.QWidget(self)
self.vlayout=QtWidgets.QVBoxLayout()
self.webview=QtWebKitWidgets.QWebView()
self.webview.setUrl(QUrl("https://www.youtube.com/watch?v=Mq4AbdNsFVw"))
self.vlayout.addWidget(self.webview)
self.centralwid.setLayout(self.vlayout)
self.setCentralWidget(self.centralwid)
self.show()
app=QtWidgets.QApplication([])
ex=window()
sys.exit(app.exec_())
You are importing some deprecated modules from PyQt5 (QtWebKit, and QtWebKitWidgets). It seems you have the right paths commented out at the bottom of your imports.
If you resolve these issues and use the proper modules (QtWebEngineCore, QtWebEngineWidgets) it works on my system.
from PyQt5 import QtWidgets,QtCore,QtGui
import sys, time
from PyQt5.QtCore import Qt,QUrl
from PyQt5 import QtWebEngineWidgets
from PyQt5 import QtWebEngineCore
from PyQt5.QtWebEngineWidgets import QWebEngineSettings
class window(QtWidgets.QMainWindow):
def __init__(self):
QWebEngineSettings.globalSettings().setAttribute(QWebEngineSettings.PluginsEnabled,True)
super(window,self).__init__()
self.centralwid=QtWidgets.QWidget(self)
self.vlayout=QtWidgets.QVBoxLayout()
self.webview=QtWebEngineWidgets.QWebEngineView()
self.webview.setUrl(QUrl("https://www.youtube.com/watch?v=Mq4AbdNsFVw"))
self.vlayout.addWidget(self.webview)
self.centralwid.setLayout(self.vlayout)
self.setCentralWidget(self.centralwid)
self.show()
app=QtWidgets.QApplication([])
ex=window()
sys.exit(app.exec_())
The output I get looks like the following (which seems correct):
Related
I had a PyQT Project which was working very well 1-2 months ago. I made some changes (not in the PyQT Project, but in other python files in same directory) and then ran the same PyQT Project again, but now it is giving me error.
The error is as follows,
PySide2/__init__.py: Unable to import shiboken2 from e:\Programming\PyQT\Second_Project,
At the end of the error, I found this ImportError,
ImportError: DLL load failed: The specified procedure could not be found.
I have tried lots of things but still I could not find any solution to this. I tried to re-install the PySide2 but to no use. I tried to change my Python Interpreter from 3.6.0 to 3.8.6 and also 3.9.2 but again, I got the same error. I also tried other different things answered in various Stackoverflow posts but nothing has seemed to work for me.
Can anyone help me how do I resolve this? I am 100% sure that the changes I made in other python files have absolutely nothing to do with this PyQT Project and this problem did not occur because of those changes.
The lines on which I got errors are,
from PySide2 import shiboken2 # Added this line later because shiboken2 wasn't being imported, but still it did not work
import iconify as ico
from iconify.qt import QtGui as IconQtGui, QtWidgets as IconQtWidgets
import PySide2
All the imports that I am making in my main.py file,
import sys
import os
from PyQt5.sip import delete
from PySide2 import shiboken2
# import iconify as ico
from iconify.qt import QtGui as IconQtGui, QtWidgets as IconQtWidgets
import PySide2
import mysql.connector as mc
from qt_material import *
# from PyQt5 import QtWidgets, uic, QtGui
from functools import partial
# from PyQt5.QtCore import Qt
from registered_courses import reg_data
from add_section_page import Ui_Add_Section_Window
from edit_section_page import Ui_Edit_Section_Window
from add_course_page import Ui_Add_Course_Window
from edit_course_page import Ui_Edit_Course_Window
from add_room_page import Ui_Add_Room_Window
from edit_room_page import Ui_Edit_Room_Window
from add_teacher_page import Ui_Add_Teacher_Window
from edit_teacher_page import Ui_Edit_Teacher_Window
from add_registered_course import Ui_Add_Registered_Course_Window
from edit_registered_course import Ui_Edit_Registered_Course_Window
from registered_section_student_details import Ui_Registered_Section_Students_Window
from add_section_student_page import Ui_Add_Section_Student_Window
from add_room_preferences import Ui_Add_Room_Preferences_Teacher_Window
from add_slot_preferences import Ui_Add_Slot_PReferences_Teacher_Window
import sections_timetable
import rooms_timetable
import teachers_timetable
from time_table import *
from student_clashes import *
from teacher_clashes import *
from room_clashes import *
from stylesheet import *
from util import *
#####################################
# Import GUI File
from ui_interface import *
Also, these are all the imports that are being made in my ui_interface file (the python file that is being generated by the Qt Designer)
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'interfaceGufDiZ.ui'
##
## Created by: Qt User Interface Compiler version 5.14.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
QRect, QSize, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
QRadialGradient)
from PySide2.QtWidgets import *
import icons_rc
I'm trying to call a mouse click event in QWebview to get a text by copy in the QWebView page but I don't know how. I have been trying to solve this for sometimes now.
Here is the minor code
import sys
from PyQt5.QtWidgets import *
from PyQt5 import Qt
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWebKitWidgets import *
class sw(QWebView):
def keyPressEvent(self, e):
clipboard=QApplication.clipboard()
data=clipboard.mimeData()
print(data.text)
super(Mainw, self).keyPressEvent(e)
class Mainw(QWidget):
def __init__(self):
super().__init__()
#QTextEdit().__init__()
self.m=QMenuBar(self)
self.file=self.m.addMenu("file")
self.new=QAction("new", self)
self.file.addAction(self.new)
ed=QWebView(self)
ed.setHtml(imported_data)
ed.setGeometry(20,30,400,400)
if __name__=="__main__":
a=QApplication(sys.argv)
app=Mainw()
#sb=app.open()
app.show()
sys.exit(a.exec_())
I know there are alot or errors in this code
in principal I reopen my question some days ago:
where-do-i-write-the-class-for-a-single-promoted-qwidget-from-qt-designer
The solution works, but I run into 2 more questions
if I want to change the text in the class neuLabel, how do i adress the class out of main program?
Additional information for minimal example as required:
I have a qt designer main window, named testpromote.ui. It contains only one label. Text is "MyLabel", the name is neuLabel and it is promoted as neulabel.
I need 2 .py files in one directory.
First the main testpromote.py
import sys
import os
from PyQt5 import uic
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import * #QPainter
import neulabel
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
uifile_1 = os.path.join(CURRENT_DIR, "testpromote.ui")
form_1, base_1 = uic.loadUiType(uifile_1)
class myApp(base_1, form_1):
def __init__(self):
super(base_1,self).__init__()
self.setupUi(self)
# ??????????????
#neuLabel.setText(neuLabel,"from main")
# ??????????????
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = myApp() #Dialog()
ex.show()
sys.exit(app.exec_())
2nd file is neulabel.py
from PyQt5.QtWidgets import *
from PyQt5 import uic
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtCore import *
from PyQt5.QtGui import * #QPainter
class neuLabel(QLabel):
def __init__(self,parent=None):
super().__init__(parent)
self.setAcceptDrops(True)
self.setText("from code")
print("the new pixmap?", self.text())
My aim is to change the text of the label out of the main program testpromote.py
I do not understand, how to import the neulabel.py, so that i could change the text of the label.I tried it with "neuLabel.setText(neuLabel,"from main")", but this doesn't work. I tried it with other code too, but not succesfully. (This is marked within the # ????????? lines)
To see, if the text in the label changes, I added in neulabel.py the
self.setText() statement, but the text does not change in the window.
(i hope, it is clearer now)
I am trying to use function QTreeWidget.findItems to find an exact match (the item is named "Things")
I saw an example using this ... Qt.MatchExactly ... as the 'match flag'.
even though i had imported Qt module from PyQt5, the MatchExactly was not found.
I got it to work by importing ANOTHER Qt module found in QtCore in PyQt5. but that was after many hours over several days of poking, guessing, and reading stackOverFlow posts.
My question is - how to know what module contains (and therefore must be imported) the stuff I need? how would I know that Qt.MatchExactly is in the PyQt5.QtCore.Qt module (and NOT in PyQt5.Qt module)?
This is my code: Note it reads in a QtDesigner .ui file, so its not gonna work for you. but having you run this code is not the point.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QMainWindow, QApplication, QTreeWidgetItem
from PyQt5 import uic, QtWidgets, Qt #<<flags are not here
from PyQt5 import QtCore
from PyQt5.QtCore import Qt #<<<this is where i found the match flag "MatchExactly"
qtCreatorFile = "Main2.ui" # Enter qt Designer file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class MyApp(QMainWindow):
def __init__(self):
super(MyApp, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.InitModelContentTree()
def InitModelContentTree (self):
modelContentTree = self.ui.ModelContentTree
ThingsItemList = modelContentTree.findItems("Things", Qt.MatchExactly)
ThingsItem = ThingsItemList[0]
QTreeWidgetItem(ThingsItem, ["New Thing"])
print("pause")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
i'm hoping there is a decoder ring, some basic thing i'm missing. i'm proving to be a very bad, very inefficient guesser.
Actually Qt.MatchExactly is in Qt but you must import it in another way:
from PyQt5 import Qt
# ...
ThingsItemList = modelContentTree.findItems("Things", Qt.Qt.MatchExactly)
TL; DR
The Qt submodule is a fake module that allows access to any module such as an alias, for example it is similar to:
from PyQt5 import QtCore as Qt
from PyQt5 import QtGui as Qt
from PyQt5 import QtWidgets as Qt
# ...
# The same for all submodules
For example it can be checked for Qt.MatchExactly:
from PyQt5 import Qt, QtCore
assert(QtCore.Qt.MatchExactly == Qt.Qt.MatchExactly)
So in general the following import:
from PyQt5 import somemodule
somemodule.someclass
It is equivalent to:
from PyQt5 import Qt
Qt.someclass
How to know what submodule a class belongs to?: Well, if you have an IDE that can do a self-report like pycharm, the task is simple since the IDE itself does it. But if I do not have access to the IDE the other option is to use the docs of Qt, for example the docs of Qt::MatchExactly is in this link that in the first part is the following table:
And observe Qt += core so all the elements of that docs belong to the core sub-module of Qt that in PyQt/PySide corresponds to QtCore (in general if the docs of Qt indicate Qt += mymodule in PyQt/PySide in QtMyModule).Also the Qt::MatchExactly of C ++ corresponds to Qt.MatchExactly in python. So in conclusion you should use:
from PyQt5 import QtCore
QtCore.Qt.MatchExactly
I've been able to successfully import QtWidgets from PyQt5 and this works fine in code however if I don't use the full QtWidgets name in the call I get an error. Below works...
import sys
from PyQt5 import QtWidgets
app = QtWidgets.QApplication(sys.argv)
Yet if do...
import sys
from PyQt5 import QtWidgets
app = QApplication(sys.argv)
I get ...
NameError: name 'QApplication' is not defined
Your misunderstanding how Python import statements work. When importing a module in Python, only the module directly imported is include in the local symbol table.
Thus, if you have not directly imported a name, it cannot be used as a standalone identifier in the current namespace. If you want this behavior, directly import the QApplication name from the QtWidgets namespace:
from PyQt5.QtWidgets import QApplication
Import modules like this:
import sys
from PyQt5.QtWidgets import *
app = QApplication(sys.argv)