2013-03-03 64 views
2

我想创建一个gui控件,它将通过.sql文件进行搜索。我正在使用tkinter,并且我有一个for循环,它不工作。它只会显示一个结果时,有许多5.如何获得for循环与python 2.7中的tkinter一起使用?

def OnPressEnter(self,event): 
    with con: 
     cur = con.cursor() 
     t = self.entryVariable.get() 

     cur.execute("select * from Dict where Def LIKE ? OR word LIKE ? LIMIT 12", ('%'+t+'%', '%'+t+'%')) 
     rows = cur.fetchall() 

     for row in rows: 
      #printing second & third column(See tuples) 

      self.labelVariable.set(row[1]) 

      self.labelVariable.set(row[2]) 

如果我输入“苹果”我只会让我的结果之一即是“499 $”。那么如何让循环工作?

回答

1

这不是for循环,而是你设置两次相同的变量。在这里:

for row in rows: 
     #printing second & third column(See tuples) 
     #sets the variable 
     self.labelVariable.set(row[1]) 
     #sets the variable again to a different value 
     self.labelVariable.set(row[2]) 

,你可以这样做:

label = "" 
for row in rows: 
    label += row[1] + ":" + row[2] + "\n" 
self.labelVariable.set(label) 
#print to check the label string 
print label 

最先生成的字符串,然后设置值。标签值应该包含两个值,在它们之间用冒号隔开行。

+0

我仍然只有一个结果,当有5个。看起来好像有什么与循环。 [这是我的gui](http://imgur.com/AALHCzk)&我的[console正在输出这个](http://imgur.com/gaWtQPw)。 – Bossman759 2013-03-03 02:34:43

+0

你可以查看完整的代码[这里](https://github.com/bossman759/wiki-serach/blob/master/gui.py),如果有帮助。 – Bossman759 2013-03-03 02:50:47

+1

将'label =“”'移出for循环[here](https://github.com/bossman759/wiki-serach/blob/master/gui.py#L78)和其他任何地方。每次迭代都会重置'label'的值,抛弃最后一行值。 – Raufio 2013-03-03 03:05:10