我有一个空列表,从用户的输入得到它的值一个[]当我点击按钮时,我需要打印出列表中的第一个元素然后当用户再次点击同一个按钮时,我需要将列表中的第二个元素打印出来,以此类推。这是按钮
self.button3 = wx.Button(self.panel, label="add word",size=(100,50),pos=(650,220))
self.button3.Bind(wx.EVT_BUTTON, self.buttonloop)
这是我想在点击按钮时调用的函数
def buttonloop(self,event):
os.chdir('d:/KKSC')
dic = getDic()
print dic[0], dic[1], dic[2]
text = tokenize_editor_text(self.control.GetValue())
a =[]
for word in text:
if word not in dic:
misspelled = word
a.append(misspelled)
for item in a:
print(item + " is an ape")
currentitem = item
b=a[item]
c=item.index
nextitem = a[c + 1]
print nextitem
# here I want to call the function again if the button clicked again
到目前为止,它对我不起作用。任何帮助都是感激的,如果你没有很好地理解我,我会重新编辑帖子,或者我会用评论解释更多
为什么不试试生成器呢?
def buttonloop(self,event):
os.chdir('d:/KKSC')
dic = getDic()
print dic[0], dic[1], dic[2]
text = tokenize_editor_text(self.control.GetValue())
try: ##Exception handler for first occurence(will also cause the list to loop)
print self.wordlist.next()
except:
self.wordlist = getwordlist(text,dic)
print self.wordlist.next()
def getwordlist(self,text,dic):
a = []
for word in text:
if word not in dic:
misspelled = word
a.append(misspelled)
for item in a:
yield item