我将 mysql 数据插入到 pyqt Qtablewidget 但是当我将数据传递给一个变量时,数据只打印到表中的一个单元格中。有什么建议么?
我的代码:
import sys
import MySQLdb as mdb
from PyQt4.QtGui import *
db = mdb.connect('server','user','password','db')
model = db.cursor()
model.execute("""SELECT ip
FROM table""")
rows_pong = (model.fetchall())
data = "\n".join(item[0] for item in rows_pong)
print data
lista = [data]
listb = ['ba', 'bb', 'bc']
mystruct = {'A':lista, 'B':listb}
class MyTable(QTableWidget):
def __init__(self, thestruct, *args):
QTableWidget.__init__(self, *args)
self.data = thestruct
self.setHorizontalHeaderLabels(['IP Address', 'Username'])
self.setmydata()
def setmydata(self):
for n, key in enumerate(self.data):
for m, item in enumerate(self.data[key]):
newitem = QTableWidgetItem(item)
self.setItem(m, n, newitem)
def main(args):
app = QApplication(args)
table = MyTable(mystruct, 100, 3)
table.show()
sys.exit(app.exec_())
if __name__=="__main__":
main(sys.argv)
样本数据(变量中的项目):
10.10.0.171:139
10.10.0.171:139
127.0.0.1:3306
127.0.0.1:20128
127.0.0.1:20129
127.0.0.1:20130
127.0.0.1:20130
127.0.0.1:20131
127.0.0.1:20131
127.0.0.1:61335
127.0.0.1:61561
127.0.0.1:61562
127.0.0.1:61942
127.0.0.1:61943
127.0.0.1:62291
127.0.0.1:62292
127.0.0.1:63093
127.0.0.1:63094