【发布时间】:2017-07-20 11:26:30
【问题描述】:
我正在尝试在窗口上打印数据库中的历史记录,但我的程序只显示两行(甚至没有排序)。有什么问题? 打印时,我正确获取了数据,但无法将它们显示在窗口上。 对我来说没有意义。
def his(self, database, progx):
con = sqlite3.connect(database)
with con:
cur = con.cursor()
cur.execute('SELECT * FROM ' + progx + ' ORDER BY id DESC ')
index=3
for row in cur.fetchall():
print(row) #this works just fine
Label(self, text=row[1]).grid(row=index, column=0)
Label(self, text=row[2]).grid(row=index, column=1)
Label(self, text=row[3]).grid(row=index, column=2)
Label(self, text=row[4]).grid(row=index, column=3)
Label(self, text=row[5]).grid(row=index, column=4)
Label(self, text=row[6]).grid(row=index, column=5)
Label(self, text=row[7]).grid(row=index, column=6)
Label(self, text=row[8]).grid(row=index, column=7)
Label(self, text=row[9]).grid(row=index, column=8)
Label(self, text=row[10]).grid(row=index, column=9)
Label(self, text=row[11]).grid(row=index, column=10)
Label(self, text=row[12]).grid(row=index, column=11)
Label(self, text=row[13]).grid(row=index, column=12)
Label(self, text=row[14]).grid(row=index, column=13)
hist.update()
index=+1
【问题讨论】:
-
for n in range(14): Label(self, text=row[n+1]).grid(row=index, column=n)将摆脱所有丑陋的重复代码 -
谢谢,但这并不能解决我的问题。
-
是的,你是对的。只是一个小提示,以备将来使用。对我来说,看起来问题一定是由代码中的其他内容引起的。如果打印出来的数据看起来不错,我看不出有什么不应该起作用的理由。
-
请创建一个minimal reproducible example,最好是不依赖外部数据库的。例如,您可以使用预定义的列表来模拟您的数据。