某个函数启动时,PyQt5代码失败

时间:2017-07-16 10:11:06

标签: python python-3.x pyqt5

我创建了一个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 个答案:

答案 0 :(得分:0)

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

  

。内容

我实际上必须使用

  

的.text

之后一切顺利。

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

这是def /方法:

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

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

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