0

基本上我试图让我的数据库连接到我的 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')

错误:TypeError:'pydodbc.Row' 对象不是

4

1 回答 1

1

rows首先是对象的集合,然后通过调用 random.choicepydodbc.Row将其更改为单个对象: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))
于 2013-02-26T12:10:11.807 回答