简体   繁体   中英

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']
 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. Please guide me how to display values into lineEdit without using split() function.

For a lineEdit you can just reference each element in the list, this of course assumes you always have 5 elements in your list. 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] . I would rather do something like my example below.

This is for PyQt5 since I only have it installed on my PC. But why not use something more dynamic than lineedits? Such as a QlistWidget with editable items.

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. 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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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