python - Pyqt5:单击事件时在 QlineEdit 框中动态传递值

标签 python python-3.x sqlite pyqt pyqt5

需要将事件结果传递到QlineEdit框(rs_QLineEdit) 如果通过传递图书名称来单击该事件,则会显示图书名称和价格。 数据库连接没有问题,运行良好,并在 python shell 中显示结果。

我想动态传递价格,即将m[2]传递给rs_QLineEdit

from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QRegExpValidator
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3

def fetch(nm):
    store=sqlite3.connect("store.db")
    book=store.cursor()
    book.execute("select * from books where title='"+nm+"';")
    m=book.fetchone()
    if m==None:
        print("Book is not Found")
    else:
        print("the name is {} ".format(nm))  #The Wings of Fire
        print(m[2])                          #200

    store.close()

class Ui_Form(object):
def setupUi(self, Form):
    Form.setObjectName("Form")
    Form.resize(635, 510)
    self.find = QtWidgets.QPushButton(Form)
    self.find.setGeometry(QtCore.QRect(370, 200, 93, 28))
    self.find.setObjectName("find_button")
    self.find.setToolTip("Press this")
    self.name = QtWidgets.QLabel(Form)
    self.name.setGeometry(QtCore.QRect(100, 200, 55, 16))
    self.name.setObjectName("name_label")
    self.price = QtWidgets.QLabel(Form)
    self.price.setGeometry(QtCore.QRect(100, 260, 55, 16))
    self.price.setObjectName("price_label")
    self.rs = QtWidgets.QLineEdit(Form)
    self.rs.setGeometry(QtCore.QRect(230, 260, 55, 16))
    self.rs.setObjectName("rs_QLineEdit")
    self.lineEdit = QtWidgets.QLineEdit(Form)
    self.lineEdit.setGeometry(QtCore.QRect(220, 200, 113, 22))
    self.lineEdit.setObjectName("lineEdit")


    self.retranslateUi(Form)
    QtCore.QMetaObject.connectSlotsByName(Form)

    self.find.clicked.connect(lambda : fetch(str(self.lineEdit.text())))

    reg_ex = QRegExp("[0-9]+.?[0-9]{,2}")
    input_validator = QRegExpValidator(reg_ex, self.rs)
    self.rs.setValidator(input_validator)

def retranslateUi(self, Form):
    _translate = QtCore.QCoreApplication.translate
    Form.setWindowTitle(_translate("Form", "Form"))
    self.find.setText(_translate("Form", "Find"))
    self.name.setText(_translate("Form", "Name"))
    self.price.setText(_translate("Form", "Price"))
    self.rs.setText(_translate("Form", "RS."))

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

最佳答案

虽然 lambda 方法对于某些类型的任务通常很有用,但您不应该滥用它们,因为它们有很多限制,在您的情况下,获取函数应该返回结果,但在 lambda 方法中很难建立(它可以是理论上是这样,但它是不可读的)。

除此之外,我还根据 PyQt 的建议不厌其烦地改进您的代码:http://pyqt.sourceforge.net/Docs/PyQt5/designer.html#using-the-generated-code ,在其中我建议不要修改 Qt Designer 生成的类,而是实现另一个继承自小部件的类,并使用初始类来填充小部件,在该新类中实现逻辑。

代码:

import sqlite3

from PyQt5 import QtCore, QtGui, QtWidgets


def fetch(nm):
    store=sqlite3.connect("store.db")
    book=store.cursor()
    book.execute("select * from books where title='{}';".format(nm))
    m=book.fetchone()
    store.close()
    if m:
        print("the name is {} ".format(nm))
        return m[2] 
    else:
        print("Book is not Found")

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(635, 510)
        self.find = QtWidgets.QPushButton(Form)
        self.find.setGeometry(QtCore.QRect(370, 200, 93, 28))
        self.find.setObjectName("find_button")
        self.find.setToolTip("Press this")
        self.name = QtWidgets.QLabel(Form)
        self.name.setGeometry(QtCore.QRect(100, 200, 55, 16))
        self.name.setObjectName("name_label")
        self.price = QtWidgets.QLabel(Form)
        self.price.setGeometry(QtCore.QRect(100, 260, 55, 16))
        self.price.setObjectName("price_label")
        self.rs = QtWidgets.QLineEdit(Form)
        self.rs.setGeometry(QtCore.QRect(230, 260, 55, 16))
        self.rs.setObjectName("rs_QLineEdit")
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(220, 200, 113, 22))
        self.lineEdit.setObjectName("lineEdit")
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.find.setText(_translate("Form", "Find"))
        self.name.setText(_translate("Form", "Name"))
        self.price.setText(_translate("Form", "Price"))
        self.rs.setText(_translate("Form", "RS."))

class Widget(QtWidgets.QWidget, Ui_Form):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.setupUi(self)

        self.find.clicked.connect(self.onFindClicked)

        reg_ex = QtCore.QRegExp("[0-9]+.?[0-9]{,2}")
        input_validator = QtGui.QRegExpValidator(reg_ex, self.rs)
        self.rs.setValidator(input_validator)

    @QtCore.pyqtSlot()
    def onFindClicked(self):
        title = self.lineEdit.text()
        name = fetch(title)
        if name:
            self.rs.setText(str(name))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

关于python - Pyqt5:单击事件时在 QlineEdit 框中动态传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50575900/

相关文章:

camera - 树莓派 3B+ : Camera V1. 3 不工作

python - 使用 crontab 停止 python 脚本

python - 使用 Python 自动下载嵌入式 PDF 文件

python - Groupby 和 count/sum 与 python 中的元组列表?

python - 尝试使用 MSI 身份验证从 Azure ML 服务连接 Azure SQL 数据库(无需用户名和密码即可连接 Azure 数据库)

python - 从 Access 创建 sqlite 数据库

c++ - 将 numpy 的数组 reshape 转换为等效的 OpenCV

javascript - AVG(COLUMN_SCORE) 未生成平均值

python - 如何制作迭代器集