0

I Have an empty list which get its value from user's input a[] when I click on ( button) I need to print out the first element in the list and then when the user clicked again on the same button i need the second element in the list to be printed out and so forth. this is the button

self.button3 = wx.Button(self.panel, label="add word",size=(100,50),pos=(650,220))
self.button3.Bind(wx.EVT_BUTTON, self.buttonloop)

and this is the function that I want to call it whenever the button is clicked

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

It doesn't work for me so far. any help is appreciated and if you didn't understand me well I will re-edit the post or I will explain more with comments

1 Answers1

0

Why not try a generator?

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
Joel Johnson
  • 186
  • 7
  • yeah you right . but i got error when i run it because the wordlist doesnt exist. what do you mean by wordlist bro @Joel Johnson ?? – Bamo Nabaz Jun 06 '15 at 00:14
  • self.wordlist is just a global variable I created to hold the list 'a'. It needs to exist outside of your buttonloop function or else it will reset everytime you call it. – Joel Johnson Jun 06 '15 at 05:00