wxpython如何使用for循环打印列表中的每个元素,事件是EVT_按钮
我有一个空列表,它从用户的输入中获取其值 a[] 当我点击(按钮)时,我需要打印出列表中的第一个元素 然后,当用户再次单击同一按钮时,我需要打印列表中的第二个元素,以此类推。 这是按钮wxpython如何使用for循环打印列表中的每个元素,事件是EVT_按钮,python,python-2.7,python-3.x,wxpython,wxtextctrl,Python,Python 2.7,Python 3.x,Wxpython,Wxtextctrl,我有一个空列表,它从用户的输入中获取其值 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
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
是的,你说得对。但是我运行它时出错了,因为单词列表不存在。你说的单词表bro@Joel Johnson是什么意思?self.wordlist只是我创建的一个全局变量,用来保存列表“a”。它需要存在于ButtonLop函数之外,否则每次调用它时都会重置。