wxpython如何使用for循环打印出列表中的每个元素,事件是EVT_Button

2024-10-03 00:29:50 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

这是我想在每次点击按钮时调用它的函数

^{pr2}$

到目前为止我还没用。任何帮助都是感激的,如果你不理解我很好,我会重新编辑这篇文章或我会解释更多的评论


Tags: 用户posselfadd元素列表sizebutton
1条回答
网友
1楼 · 发布于 2024-10-03 00:29:50

为什么不试试发电机呢?在

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

相关问题 更多 >