基本上我试图让我的数据库连接到我的GUI和显示一个随机的问题,但它根本不工作,任何想法?
SQL = 'SELECT * FROM tblQuestion'
cursor = Databaseconnector.SELECT(SQL)
rows = cursor.fetchall()
rows = random.choice(rows)
print rows.Question, rows.Hint, rows.A1, rows.A2, rows.A3, rows.A4, rows.CorrectAnswer
#def create_widgets(self):
#create welcome label
label1 = Tkinter.Label(self, text = (rows(1).Question))
label1.grid(row = 0, column = 1, columnspan = 2, sticky = 'W')
ERROR: TypeError: ' pydodbc. '行'对象不是
rows
首先是pydodbc.Row
对象的集合,但随后通过调用random.choice:
pydodbc.Row
对象。rows = cursor.fetchall() # rows is a list
rows = random.choice(rows) # now rows is a single object
然后尝试使用()
:
label1 = Tkinter.Label(self, text = (rows(1).Question))
显示您提供的(部分)错误消息失败:
ERROR: TypeError: ‘pydodbc.Row’ object is not callable
解决这个问题的最好方法是为单行使用一个新变量:
rows = cursor.fetchall()
random_row = random.choice(rows)
...
label1 = Tkinter.Label(self, text = (random_row.Question))