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.
Related
I have an issue with PyQt5 and VS Code. I have installed PyQt5 on my computer but when I write from VS Code it doesn't work. VS Code underlines my imports with a yellow line and when I run the code it says
from PyQt5 import QtWidgets
ImportError: No module named PyQt5.
However, when I run the same program from my terminal, it runs fine. My code is
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication, QWidget,QStackedWidget
class Window(QDialog):
def __init__(self):
super(Window, self).__init__()
loadUi("window.ui",self)
app= QApplication(sys.argv)
window = Window()
widget = QStackedWidget()
widget.addWidget(window)
widget.setFixedHeight(800)
widget.setFixedWidth(1200)
widget.show()
try:
sys.exit(app.exec())
except:
print("Exiting")
There should be more than one python version installed on your computer, please select the correct python interpreter using:
Ctrl+Shift+P command to open the Command Palette
search for and select Python:Select Interpreter
(Or click directly on the python version displayed in the lower right corner)
select the correct interpreter.
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
Is there possible to use PyQt5 in Visual Studio 2015?
I installed PyQt and created python project. I get an error.
No module named 'PyQt5'
If there is a handler for this exception, the
program may be safely continued.
What do I have to do?
import sys
from PyQt5 import QtWidgets
def main():
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
button = QtWidgets.QPushButton("Hello world")
window.setCentralWidget(button)
window.show()
app.exec_()
if __name__ == '__main__':
main()
Try this:
In "Solution Explorer" choose "References"->"Add Reference"->"Browse"- tab, where select *.pyd files in (Python installation folder)\Python34\Lib\site-packages\PyQt4.
Then in code one can use something like this: "from Qt import * "
I am trying to build a very basic executable (Windows) using PySide. The following script runs properly in the interpreter (Python 2.7, PySide 1.1.2)
#!/usr/bin/python
import sys
sys.stdout = open("my_stdout.log", "w")
sys.stderr = open("my_stderr.log", "w")
import PySide.QtGui
from PySide.QtGui import QApplication
from PySide.QtGui import QMessageBox
# Create the application object
app = QApplication(sys.argv)
# Create a simple dialog box
msgBox = QMessageBox()
msgBox.setText("Hello World - using PySide version " + PySide.__version__)
msgBox.exec_()
I tried 3 methods (py2exe, pyinstaller and cx_freeze) and all the 3 generated executables fail to execute. The two stdout/stderr files appear, so I found the first PySide import is making everything fail. (Unhandled exception/Access violation)
I analyzed the executable file with depends (http://www.dependencywalker.com/) and everything looks correctly linked.
Any idea?
You need to add the atexit module as an include. source: http://qt-project.org/wiki/Packaging_PySide_applications_on_Windows
(is also the case for Linux btw)
Thank you for your help. Actually, this did not change anything :/ However, I found a solution to my problem: if I add from PySide import QtCore, QtGui, then the executable (with pyinstaller) does work!