当我首先运行 python-pyside2 项目服务器时,它运行良好。
而且该站点也运行良好,但是如果我按 F5 btn 在浏览器中刷新。
站点显示错误页面 Runtime at/

import sys
from urllib.request import urlopen
from bs4 import BeautifulSoup
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWebKitWidgets import *
from PySide2.QtWidgets import QApplication
class dynamic_render(QWebPage):
def __init__(self, url):
self.frame = None
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self._loadFinished)
self.mainFrame().load(QUrl(url))
QTimer.singleShot(0, self.sendKbdEvent)
QTimer.singleShot(100, app.quit)
self.app.exec_()
def _loadFinished(self, result):
self.frame = self.mainFrame()
self.app.quit()
self.app = None
Run Code Online (Sandbox Code Playgroud)
下面,使用 pyside2 转义代码:
我不知道我该如何解决?
此致。
谢谢。
我使用PySide2作为我的GUI框架和PyInstaller构建了一个小的gui应用程序,以制作一个文件exe文件。
该应用程序可以在某些PC(Windows 7和10)上完美运行,但是在其他PC上则无法启动-向我显示错误消息:
“致命错误!:无法执行脚本pyi_rth_qt5plugins”
我相信它可能不会捆绑Qt5Gui.dll,Qt5Core.dll和Qt5Widgets.dll,但是我不知道如何使用PyInstaller做到这一点。
有谁知道如何解决这个问题?
这是我的.spec文件:
# -*- mode: python -*-
block_cipher = None
a = Analysis(['main.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='Application',
debug=True,
strip=False,
upx=True,
runtime_tmpdir=None,
console=True , icon='Images\\Application_icon.ico')
Run Code Online (Sandbox Code Playgroud)
编辑
这是从cmd运行且debug = True时的完整输出。我发现奇怪的是,它是指我在(例如File "c:\users\jake\Projects\Application\venv\lib\site-packages\PyInsta
ller\loader\pyimod03_importers.py", line 714, in load_module)上开发应用程序的PC,而不是我正在测试并从中获得此错误消息的PC。
[4588] PyInstaller Bootloader 3.x
[4588] LOADER: executable is C:\Users\User\Desktop\Application.exe
[4588] LOADER: homepath is C:\Users\User\Desktop
[4588] LOADER: _MEIPASS2 is NULL
[4588] …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种在 Qt Designer 中有效地使用用 Qt for Python (PySide2) 编写的自定义小部件的方法。
我发现,可以使用基本小部件设计 GUI,然后只需将类交换到 UI 文件中的自定义小部件并通知QUiLoader有关子类的信息loader.registerCustomWidget(MyMainWindow),然后在 Qt 设计器中再次打开它不起作用那好。
我在PyQt 的这个类似问题中读到你必须为自定义小部件编写一个插件。PySide2 是否也存在这种可能性?
一些示例代码:
custom_widget.py:
import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication, QMainWindow, QAction, QMessageBox, QFileDialog, QTextBrowser
from PySide2.QtCore import QFile
class MyMainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle("Demo QtWidget App")
def closeEvent(self, event):
msgBox = QMessageBox()
msgBox.setWindowTitle("Quit?")
msgBox.setText("Exit application?")
msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
msgBox.setDefaultButton(QMessageBox.No)
ret = msgBox.exec_()
if ret == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if …Run Code Online (Sandbox Code Playgroud) 我正在尝试找到一种从 PySide2.QtCore.Slot 返回 python 字典的方法。
主要.py
import sys
from PySide2.QtCore import QObject, Slot
from PySide2.QtGui import QGuiApplication, QQmlApplicationEngine
class Backend(QObject):
def __init__(self, parent=None):
return super().__init(parent)
@Slot(result=QObject)
def get_data(self):
data = {}
data["info1"] = "some information"
data["info2"] = "some more information"
data["info3"] = 42
return data
if __name__ == '__main':
BACKEND = Backend()
APP = QGuiApplication(sys.argv)
ENGINE = QQmlApplicationEngine(APP)
ENGINE.rootContext().setContextProperty('backend', BACKEND)
ENGINE.load("main.qml")
sys.exit(APP.exec_())
Run Code Online (Sandbox Code Playgroud)
主.qml:
import QtQuick 2.4
import QtQuick.Controls 1.4
ApplicationWindow {
id: root
width: 640
height: 480
visible: true …Run Code Online (Sandbox Code Playgroud) 该QSplashScreen.showMessage()方法接受对齐标志,即Qt.AlignLeft。设置水平和垂直对齐标志的语法是什么?所有尝试都会产生“变量太多”错误。
我使用的QThread使用阅读的QThread应如何使用扩展的讨论和通过继承是不正确的方法重写它的run方法后,一个工人对象。但是,在我打算使用的方法中,我需要传递一个附加的函数参数,该参数在启动线程并将工作器使用推送到线程时不可用moveToThread。该信息(参数)在按下按钮时可用,并传达有关要移动的对象的信息。
在我的代码的完整版中,有3个单独的控制器,用于3个单独的对象,您可以在下面找到一个最小的工作示例,以说明我尝试传递参数的内容。该代码也可以在pastebin上使用,感兴趣的行号为10-28、46-50和133-135。
到目前为止,我已经尝试在连接到worker中实际函数的行中使用lambda构造函数。那行self.thread.started.connect(self.obj.moveLeftIncrement)尝试使用slot,但是我不太了解它们。此外,尽管使用了QThread,但有时GUI仍然挂起,并且在程序退出后仍会出现错误,其中之一如下:
进程完成,退出代码-1073740791(0xC0000409)
我的问题如下:
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtGui import *
import sys
import time
class Worker(QObject):
finished = Signal(int)
@Slot(str)
def moveLeftIncrement(self, controller_name):
# controller_name = "Controller 1"
print("Controller name is ", controller_name)
if controller_name == "Controller 1":
print("Starting controller 1")
time.sleep(2)
print("Finishing sleep")
elif controller_name == "Controller 2":
print("Starting controller 2")
time.sleep(2)
print("Finishing sleep")
elif controller_name == "Controller 3": …Run Code Online (Sandbox Code Playgroud) 我想为QWebEngineView带有大图像的页面截取屏幕截图。当在红色背景下运行代码时正确呈现但图像不是。
在截取屏幕截图之前,如何确保页面已完全呈现?
from PySide2 import QtCore, QtWidgets, QtWebEngineWidgets
html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
img { max-width:100%; max-height:100%; }
div { height: 270px; width: 540px; }
body { margin: 0; background-color: red; }
</style>
</head>
<body>
<div>
<img src="https://eoimages.gsfc.nasa.gov/images/imagerecords/73000/73751/world.topo.bathy.200407.3x5400x2700.png">
</div>
</body>
</html>
"""
class Capture(QtWebEngineWidgets.QWebEngineView):
def __init__(self, parent=None):
super(Capture, self).__init__(parent)
self.loadFinished.connect(self.save)
self.show()
self.resize(540, 270+5)
@QtCore.Slot(bool)
def save(self, isOk):
if not isOk:
print('Error')
return
self.grab().save('page.png', b'PNG')
if __name__ == '__main__':
app = QtWidgets.QApplication()
capture …Run Code Online (Sandbox Code Playgroud) TL;DR:我想要一个直接替换 PyQt5 的loadUiType()功能的uic模块,它可以与 PySide2 和 Python 3.6+ 一起使用。
我想将 PyQt5 应用程序迁移到 PySide2。我使用的一个常见模式是,我在 Qt Designer 中创建用户界面设计,并动态加载生成的.ui文件作为扩展 Python 代码中 Qt 小部件的混合类,例如主窗口本身:
from PyQt5 import QtWidgets, uic
class Window(QtWidgets.QMainWindow, uic.loadUiType('design.ui')[0]):
def __init__(self):
super().__init__()
self.setupUi(self)
print(self.label.text())
app = QtWidgets.QApplication([])
window = Window()
window.show()
app.exec_()
Run Code Online (Sandbox Code Playgroud)
这意味着我可以放弃在命令行上将.ui设计编译为.pyPython 模块。更重要的是,混合模式让我可以通过self.name导入窗口小部件的范围内访问设计中定义的所有 Qt 窗口小部件,name在 Qt 设计器中指定的位置。
为了提供可重现的示例,这里有一个与上述 Python 代码一起使用的最小 Qt 设计文件,其中引用为design.ui:
from PyQt5 import QtWidgets, uic
class Window(QtWidgets.QMainWindow, uic.loadUiType('design.ui')[0]):
def __init__(self):
super().__init__()
self.setupUi(self) …Run Code Online (Sandbox Code Playgroud) 自从今天发布 Big Sur 以来,我的 QT 应用程序停止工作,没有任何错误,项目运行,只是没有显示 Windows。
在 Pyside2 上使用 Python,使用 Pycharms 编写和启动。昨天功能齐全 - 升级前。即使使用最简单的空窗口也进行了测试:
app = QApplication(sys.argv)
window = QWidget()
window.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
同样没有显示。
谁能建议我可以检查什么?从哪里开始?
我正在尝试使用 Python 创建新的 QML 类型,但在注册 QML 类型时遇到问题。但是,我收到一个错误:
TypeError: 'PySide2.QtQml.qmlRegisterType' called with wrong argument types:
PySide2.QtQml.qmlRegisterType(module, str, int, int, str)
Supported signatures:
PySide2.QtQml.qmlRegisterType(type, str, int, int, str)
Run Code Online (Sandbox Code Playgroud)
所以我知道它期待一种类型,但是,在这篇博文中它做了类似的事情:
qmlRegisterType(PieChart, 'Charts', 1, 0, 'PieChart')
Run Code Online (Sandbox Code Playgroud)
在我的 main.py 中,我有这个:
...
if __name__ == '__main__':
# Declare QApplication
app=QApplication([])
qmlRegisterType(CamFeed, 'CFeed', 1, 0, 'CamFeed')
...
Run Code Online (Sandbox Code Playgroud)
CamFeed.py 看起来像这样:
from PySide2.QtQuick import QQuickPaintedItem
from PySide2.QtGui import QPainter
from PySide2.QtCore import QObject
class CamFeed(QQuickPaintedItem):
def __init__(self, parent=None):
super().__init__(parent)
# Re-implementation of the virtual function
def …Run Code Online (Sandbox Code Playgroud) pyside2 ×10
python ×8
python-3.x ×5
pyqt5 ×3
qt ×3
pyside ×2
qml ×2
qt-designer ×2
c++ ×1
pyinstaller ×1
pyqt ×1
qthread ×1