Trying to play audio in a qt gui
I've tried basically every suggestion on Google pertaining to playing audio with pyqt (pyside, pygame, QtMultimedia). It seems pretty straight forward using QSound, but it doesn't work for me for some reason
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
ImportError: No module named QtMultimedia
Everything else works but the multimedia module
Related
Issue
I used Qt Design Studio to create some UI components and screens (.ui.qml format), but no matter what I do, whenever I try to run it in PyQt5 I kept encountering the following error:
module "QtQuick.Studio.Components" is not installed
The .ui.qml file has an import
import QtQuick.Studio.Components 1.0
I looked everywhere online and couldn't find much on the topic on how to fix this problem.
Question
What's the best way to overcome this issue? I've been using it on Windows but I am mainly looking to use the code on a raspberry pi, are there extra dependencies that need to be installed?
Python code
import os
from pathlib import Path
import sys
from PyQt5.QtCore import QCoreApplication, Qt, QUrl
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
if __name__ == '__main__':
CURRENT_DIRECTORY = Path(__file__).resolve().parent
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.addImportPath(os.fspath(CURRENT_DIRECTORY.parents[0]))
url = QUrl.fromLocalFile(os.fspath(CURRENT_DIRECTORY / "Screen01.ui.qml"))
def handle_object_created(obj, obj_url):
if obj is None and url == obj_url:
print("exit")
QCoreApplication.exit(-1)
engine.objectCreated.connect(
handle_object_created, Qt.ConnectionType.QueuedConnection
)
engine.load(url)
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())
Windows
I used print(engine.importPathList()) to see my import paths. This prompted me to go C:/Users/USER/AppData/Local/Programs/Python/Python38/lib/site-packages/PyQt5/Qt5/qml and noticed that QtQuick exists but does not have a Studio folder.
Since Qt Design Studio can run my code with no import issues, I knew that it would have a Studio folder. Therefore, I went into C:\Program Files\Qt\qtdesignstudio-2.2.0-community\qt5_design_studio_reduced_version\qml\QtQuick and copied the Studio folder from there onto the PyQt5 path above.
This fixed the issue.
Linux
I am yet to try it on Linux.
I have instlled PyQtWebEngine module with pip but I get this python error:
No module named 'PyQt5.QtWebEngineWidgets'
my code is :
import sys
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
web = QWebEngineView()
web.load(QUrl(r'C:\Users\Hss\Desktop\hi.html'))
web.show()
sys.exit(app.exec_())
how i solve this problem?(All library is updated)
try the repair option on your python installer or try reinstalling python or the IDE
i have few web based games on html and java-script , want to open it inside pyqt5 window in python.
i was trying it on tkinter but failed to render html in it.
need help
Thanks.
Suppose this game (contra) want to play it inside pyqt5 window
screenshot of pycharm package installer
Screenshots of IDLE
Screenshot pycharm
This isn't using PyQt5 but you could launch a temp web server using SimpleHTTPServer
$ python2 -m SimpleHTTPServer 8000
or
$ python3 -m http.server 8000
It will serve up whatever is in the folder you run the command in. This shouldn't be used for production as it only provides the most basic security checks.
https://www.poftut.com/how-to-run-and-use-simple-http-server-in-python2-and-python3/
A simple solution is to use QWebEngineView that comes in the PyQtWebEngine package
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
view = QtWebEngineWidgets.QWebEngineView()
url = "https://www.retrogames.cz/play_022-NES.php?language=EN"
view.load(QtCore.QUrl(url))
view.show()
sys.exit(app.exec_())
You could load the page in a QWebView:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import QWebEngineView as QWebView, QWebEnginePage as QWebPage
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("https://oldgameshelf.com/contra-208.html"))
web.show()
sys.exit(app.exec_())
If you need to scroll to a particular element, you could pass JavaScript to web.runJavaScript() that does the scrolling.
In the example above, I used the oldgameshelf.com link of the game you linked to. But you could replace this with the URL of any page.
Also, make sure you install PyQtWebEngine if you haven't already:
pip install PyQtWebEngine
Since it's now in a separate package.
I've recently upgraded PyQt5 from 5.5.1 to 5.6.0 using the Windows 32-bit installer here: https://www.riverbankcomputing.com/software/pyqt/download5. I've also upgraded my python from 3.4 to 3.5.
When I run my old code (which used to work) with the latest version I get an exception:
from PyQt5.QtWebKitWidgets import *
ImportError: No module named 'PyQt5.QtWebKitWidgets'
All of my QT calls in my python occur consecutively and are (and I know I shouldn't be importing * but that's beside the issue here I think):
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebKitWidgets import *
So the QtCore, QtGui and QtWidgets imports are all OK.
Also, when I search the source for QtWebKitWidgets there appears several references to this module.
Finally my python path looks like:
C:\PYTHON35;C:\PYTHON35\DLLs;C:\PYTHON35\LIB;C:\PYTHON35\LIB\LIB-TK;
and environment path:
C:\Python35\Lib\site-packages\PyQt5;C:\Python35;C:\Python35\Lib;C:\Python35\Lib\site-packages;C:\Python35\Scripts ....
QtWebKit got deprecated upstream in Qt 5.5 and removed in 5.6.
You may want to switch to PyQt5.QtWebEngineWidgets.QWebEngineView.
For basic use of PyQt5.QtWebKitWidgets.QWebView, it can simply be updated to use PyQt5.QtWebEngineWidgets.QWebEngineView in the source code, but there may be some differences in the new component which require further adjustments.
I was trying to run qutebrowser and it had the same error, the answer is simple, the packages changed.
You have two solutions:
1)
pip install PyQtWebEngine
2)
pip install PyQt5==5.11.3
Hope this helps any future problems
In PyQt5 "QtWebKitWidgets" is Deprecated. I just replace this line
from PyQt5.QtWebKitWidgets import QWebView, QWebPage
from PyQt5.QtWebKit import QWebSettings
With this code:
from PyQt5.QtWebEngineWidgets import QWebEngineView as QWebView,QWebEnginePage as QWebPage
from PyQt5.QtWebEngineWidgets import QWebEngineSettings as QWebSettings
If you really want to use PyQt5.QtWebKitWidgets, you could run this from the command line:
pip install PyQtWebKit
and let it do what it does.
In PyQt5 "QtWebKitWidgets" is no longer available. Instead it is replaced with "QtWebEngineWidgets". So you have to make this change in your code.
For more information: http://doc.qt.io/qt-5/qtwebenginewidgets-qtwebkitportingguide.html
I am trying to run a script with PyQt4 but Spyder gives an ModuleNotFoundError: No module named 'PyQt4' although it is installed. What am I doing wrong? I found a few threads in the net on this but I couldn't resolve the Problem.
Thanks in advance
Edit:
This is how I import it:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
and this is where I got Spyder 3 (with Anaconda):
https://anaconda.org/anaconda/python