Python-Qt5:如何从QLineEdit创建变量?

时间:2018-06-12 12:08:16

标签: python qt-designer

我需要在QLineEdit输入中创建一个名为' askkeyword'的变量。所以我可以在函数get_list()中使用它。

#Produced in QtDesigner
import json  # <-- added
import csv  # <-- added
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(QtWidgets.QMainWindow):

    def setupUi(self, MainWindow):
        self.keywordbox = QtWidgets.QLineEdit(self.widget1)
        self.keywordbox.setObjectName("keywordbox")
        self.verticalLayout_5.addWidget(self.keywordbox)

    def get_list():
        data = aliexpress.get_product_list(['productId', 'productTitle', 'salePrice', 'originalPrice', 'imageUrl'],askkeyword, pageSize='40')
        with open('data.csv', 'w', newline='') as csvfile:
            fieldnames = ['productId', 'productTitle', 'salePrice', 'originalPrice', 'imageUrl']
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            writer.writeheader()
            for product in data['products']:
                productId = product['productId']
                productTitle = product['productTitle']
                salePrice = product['salePrice']
                originalPrice = product['originalPrice']
                imageUrl = product['imageUrl']
                writer.writerow({'productId': productId, 'productTitle': productTitle, 'salePrice': salePrice,
                                 'originalPrice': originalPrice, 'imageUrl': imageUrl})

1 个答案:

答案 0 :(得分:0)

只需获取文本并分配给变量 -

var = self.keywordbox.text()

进行全局变量使用

self.var = self.keywordbox.text()

并在代码中的任何位置使用self.var