简体   繁体   English

如何将值从数据库显示到QlineEdit pyqt4 python

[英]How to display values from Database into QlineEdit pyqt4 python

myresult = ('sandeep pawar','1234','haveri','581110','karnatak') I want display each of these values into separate QlineEdit. myresult =('sandeep pawar','1234','haveri','581110','karnatak')我想将每个这些值显示到单独的QlineEdit中。

 myresult = ['sandeep pawar','1234','haveri','581110','karnatak']
 for i in myresult:
     value = ' '.join(map(str,x))
     a,b,c,d,e = value.split(" ")
     self.lineEdit.setText(a)
     self.lineEdit_2.setText(b)
     self.lineEdit_3.setText(c)
     self.lineEdit_4.setText(d)
     self.lineEdit_5.setText(e)

I have tried with this method but i get this following error a,b,c,d,e = value.split(" ") ValueError too many values to unpack. 我尝试使用此方法,但是出现以下错误a,b,c,d,e = value.split(“”)ValueError太多值无法解包。 Please guide me how to display values into lineEdit without using split() function. 请指导我如何不使用split()函数将值显示到lineEdit中。

For a lineEdit you can just reference each element in the list, this of course assumes you always have 5 elements in your list. 对于lineEdit,您可以仅引用列表中的每个元素,这当然假设您列表中始终有5个元素。 Which is okay but likely could be done more dynamic (see below). 没关系,但是可能可以更动态地进行(请参见下文)。

myresult = ['sandeep pawar','1234','haveri','581110','karnatak']
self.lineEdit.setText(myresult[0])
self.lineEdit_2.setText(myresult[1])
self.lineEdit_3.setText(myresult[2])
self.lineEdit_4.setText(myresult[3])
self.lineEdit_5.setText(myresult[4])

The above code would work and get you a line edit for each element in your list. 上面的代码将起作用,并使您可以对列表中的每个元素进行行编辑。 But if that list were to grow, you would need to add a new lineEdit to the GUI and then populate it with myresult[5] . 但是,如果该列表增加,则需要向GUI添加新的lineEdit,然后使用myresult[5]进行填充。 I would rather do something like my example below. 我宁愿做类似下面的例子。

This is for PyQt5 since I only have it installed on my PC. 这是用于PyQt5的,因为我只在PC上安装了它。 But why not use something more dynamic than lineedits? 但是,为什么不使用比lineedits更动态的东西呢? Such as a QlistWidget with editable items. 例如带有可编辑项目的QlistWidget。

from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtWidgets import *

import sys



if __name__ == '__main__':
    app = QApplication(sys.argv)
    listWidget = QListWidget()
    listWidget.show()
    myresult = ['sandeep pawar','1234','haveri','581110','karnatak']
    listWidget.addItems(myresult)
    for index in range(listWidget.count()):
        item = listWidget.item(index)
        item.setFlags(item.flags() | QtCore.Qt.ItemIsEditable)
    sys.exit(app.exec_())

You can then bind the itemChanged event which will let you know when one of the items in the list has changed. 然后,您可以绑定itemChanged事件,该事件将在列表中的一项更改时通知您。 So you can then go in and update your list after the edits are made. 这样,您就可以在进行编辑后进入并更新列表。 I hope this helps, please let me know if you have questions or need a push in the right direction. 希望对您有所帮助,如果您有任何疑问或需要朝正确的方向发展,请告诉我。

This is my opinion: 这是我的意见:

myresult = ['sandeep pawar','1234','haveri','581110','karnatak']
line_edit = [self.lineEdit,self.lineEdit_2,self.lineEdit_3,self.lineEdit_4,self.lineEdit_5]
for i in range(len(line_edit)):
    line_edit[i].setText(myresult[i])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM