0

我有一个空列表,当我单击(按钮)时,它从用户输入 a[] 中获取其值我需要打印出列表中的第一个元素,然后当用户再次单击同一个按钮时,我需要第二个元素要打印的列表等等。这是按钮

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

到目前为止,它对我不起作用。感谢您提供任何帮助,如果您不太了解我,我将重新编辑该帖子,或者我将通过评论进行更多解释

4

1 回答 1

0

为什么不试试发电机?

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
于 2015-06-05T22:57:18.310 回答