PyQt5代码在某个功能启动时失败

Posted

技术标签:

【中文标题】PyQt5代码在某个功能启动时失败【英文标题】:PyQt5 Code fails when a certain function starts 【发布时间】:2017-07-16 10:11:06 【问题描述】:

我创建了一个 pyqt 脚本,这个脚本是我自己创建的,但是我昨天开始学习 pyqt 并且刚刚从我的脚本中实现了一个函数,但是在调用函数后它很快就崩溃了。我是 pyqt 的初学者,所以我同意格式可能很差。

代码如下:

# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from lxml import html
import requests

class Ui_ShopifyFeeder(object):
    def started(self):
        print("STARTED...\n")
        resp = requests.get('https://www.google.com/', headers=headers)
        print(resp.content)


    def setupUi(self, ShopifyFeeder):
        ShopifyFeeder.setObjectName("ShopifyFeeder")
        ShopifyFeeder.resize(423, 105)
        self.gridLayout = QtWidgets.QGridLayout(ShopifyFeeder)
        self.gridLayout.setObjectName("gridLayout")
        self.constant_value = QtWidgets.QLineEdit(ShopifyFeeder)
        self.constant_value.setEnabled(True)
        self.constant_value.setObjectName("constant_value")
        self.gridLayout.addWidget(self.constant_value, 1, 0, 1, 1)
        self.percentage_value_selection = QtWidgets.QRadioButton(ShopifyFeeder)
        self.percentage_value_selection.setObjectName("percentage_value_selection")
        self.gridLayout.addWidget(self.percentage_value_selection, 2, 1, 1, 1)
        self.percentage_value = QtWidgets.QLineEdit(ShopifyFeeder)
        self.percentage_value.setObjectName("percentage_value")
        self.gridLayout.addWidget(self.percentage_value, 2, 0, 1, 1)
        self.constant_value_selection = QtWidgets.QRadioButton(ShopifyFeeder)
        self.constant_value_selection.setObjectName("constant_value_selection")
        self.gridLayout.addWidget(self.constant_value_selection, 1, 1, 1, 1)
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.starter = QtWidgets.QPushButton(ShopifyFeeder)
        self.starter.clicked.connect(self.started)
        self.starter.setAutoFillBackground(False)
        self.starter.setObjectName("starter")
        self.verticalLayout.addWidget(self.starter)
        self.gridLayout.addLayout(self.verticalLayout, 3, 0, 1, 2)

        self.retranslateUi(ShopifyFeeder)
        self.starter.clicked['bool'].connect(ShopifyFeeder.setEnabled)
        self.starter.clicked['bool'].connect(self.constant_value.setEnabled)
        self.constant_value_selection.clicked['bool'].connect(self.constant_value.setDisabled)
        self.percentage_value_selection.clicked['bool'].connect(self.percentage_value.setDisabled)
        self.constant_value_selection.clicked['bool'].connect(self.percentage_value_selection.setDisabled)
        self.constant_value_selection.clicked['bool'].connect(self.percentage_value.setDisabled)
        self.percentage_value_selection.clicked['bool'].connect(self.constant_value.setDisabled)
        self.percentage_value_selection.clicked['bool'].connect(self.constant_value_selection.setDisabled)
        QtCore.QMetaObject.connectSlotsByName(ShopifyFeeder)

    def retranslateUi(self, ShopifyFeeder):
        _translate = QtCore.QCoreApplication.translate
        ShopifyFeeder.setWindowTitle(_translate("ShopifyFeeder", "ShopifyFeeder"))
        self.percentage_value_selection.setText(_translate("ShopifyFeeder", "Percentage"))
        self.constant_value_selection.setText(_translate("ShopifyFeeder", "Constent"))
        self.starter.setText(_translate("ShopifyFeeder", "Start"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ShopifyFeeder = QtWidgets.QWidget()
    ui = Ui_ShopifyFeeder()
    ui.setupUi(ShopifyFeeder)
    ShopifyFeeder.show()
    sys.exit(app.exec_())

因此,一旦调用了启动的函数/方法,脚本就会在 print 语句后崩溃。如果我能提供一些关于什么是错误的信息,那就太好了。

注意:这段代码有效,但它会在我的浏览器中打开一个链接,而我希望它完全像请求和基本 python 脚本一样工作。

def started(self):
    print("STARTED...\n")
    url = QtCore.QUrl('https://www.yahoo.com/')
    if not QtGui.QDesktopServices.openUrl(url):
        QtGui.QMessageBox.warning(self, 'Open Url', 'Could not open url')

好的,我再次尝试使用这种新方法,但脚本仍然崩溃。

def loadPage(self):
    print("STARTED...\n")
    page = QtWebKit.QWebPage()
    loop = QtCore.QEventLoop() # Create event loop
    page.mainFrame().loadFinished.connect(loop.quit) # Connect loadFinished to loop quit
    page.mainFrame().load('https://www.yahoo.com/')
    loop.exec_() # Run event loop, it will end on loadFinished
    print( page.mainFrame().toHtml() )

【问题讨论】:

【参考方案1】:

好吧,这是一个相当简单的问题,问题是

.内容

我实际上不得不使用

.文本

然后一切顺利。

r.text 是响应的 unicode 内容,r.content 是响应的内容,以字节为单位。我使用的是 PyQt5 和 python 3.5 以及最新版本的请求。

这是定义/方法:

def loadPage(self):
    print("STARTED...\n")

    resp = requests.get('https://www.yahoo.com/')

    tree = html.fromstring(resp.text)
    print("\nFinished...")

【讨论】:

以上是关于PyQt5代码在某个功能启动时失败的主要内容,如果未能解决你的问题,请参考以下文章

改变QLabel的大小时,PyQt5“定时器无法从另一个线程启动”错误

SpringBoot启动时如何对配置文件进行校验?这种方法才叫优雅~

PyQt5 - 第二个窗口中的按钮在单击时不执行操作

SpringBoot启动时如何对配置文件进行校验?这种方法才叫优雅!(文末福利)

pyqt5通过qt designer 设计方式连接多个UI图形界面

Tomcat启动时载入某个servlet