我想在Tkinter应用程序中重复一个回调函数,choose_tags。该函数需要两个优先级,并且应该每次重复从每个队列返回/处理一个项目,例如来自tag_queue的“NN”和来自word_queue的“Katter”。重复的总次数与列表的len相同,4。我想知道是否可以通过在return语句中调用self来重复该函数,返回choose_tags(choice)或者是否有repeat()函数可以使用类似于after()函数吗?
import Queue
from Queue import PriorityQueue
from Tkinter import *
import Tkinter as tk
import threading
import time
import thread
from time import time, sleep
class Application(Frame):
def __init__(self, root):
""" init the frame """
Frame.__init__(self)
self.grid()
self.choice_y_n = tk.StringVar()
self.q = tk.StringVar()
self.q2 = tk.StringVar()
self.q3 = tk.StringVar()
self.tag_queue = Queue.PriorityQueue()
self.word_queue = Queue.PriorityQueue()
self.word_ls = ["katter", "jagar"]
self.tag_ls = ["NN","VB"]
self.create_widgets()
def create_widgets(self):
choice_Y = tk.Button(self, text = "Yes", command = self.Y)
choice_Y.grid(row = 14, column = 0, sticky = W)
choice_N = tk.Button(self, text = "No", command = self.N)
choice_N.grid(row = 14, column = 1, sticky = W)
self.w_entry = Entry(self)
self.w_entry.grid(row = 10, column = 0, sticky = W)
self.box_txt = tk.Text(self, width = 65,height = 25, wrap = WORD)
self.box_txt.grid(row = 15, column = 0, columnspan = 5, sticky = W)
def display_contents(self):
str_ = self.w_entry.get()
self.q.set(str_)
self.word_ls = str_.split()
self.tag_ls = ["NN","VB","JJ","ADV"]
return self.q
def Y(self):
self.choice_y_n.set("y")
choice = self.choice_y_n.get()
self.choose_tags(choice)
def N(self):
self.choice_y_n.set("n")
choice = self.choice_y_n.get()
self.choose_tags(choice)
def repeat(self):
choice = None
print self.choose_tags(choice)
root.after(5000,repeat)
def choose_tags(self, choice):
for i,j in enumerate(self.word_ls):
self.word_queue.put((i,j))
for i,j in enumerate(self.tag_ls):
self.tag_queue.put((i,j))
if not self.tag_queue.empty():
word = list(self.tag_queue.get())
if not self.word_queue.empty():
tag = list(self.word_queue.get())
print tag, word
#print self.choose_tags(choice)
#root.after(5000,task)
# Main
root = tk.Tk()
root.title("Maltparser1.0_demo")
root.geometry("900x700")
app = Application(root)
root.mainloop()
答案 0 :(得分:0)
在原始问题评论后修改
因此,既然你想重复列表中所有项目的功能,我会做类似下面的事情。但请记住,只有在choose_tags方法中使用return语句时,这才有效。
processedList = []
For item in ["NN","VB","JJ","ADV"]:
processeditem = self.choose_tags(item)
processedList.append(processeditem)
然后,当您完成后,您可以在每次调用函数时操作/执行您想要对返回项执行的操作。
答案 1 :(得分:0)
after()
方法不会自动为您重新安排内容;这是一次性的。要重复此操作,请在其运行的回调内再次调用after()
。有些情况可以将重新安排在回调中排在第一位或最后一位;两者都有效,但我发现通常最好把它放在最后,以便重新安排只有在其他一切都成功时才会发生。 (重新安排的错误不很好!)