Python 将迭代计数显示为弹出窗口
我希望将长流程的当前迭代显示为弹出窗口,而不是在终端中。计数需要在同一个弹出窗口内更新(或刷新)。据我所知:Python 将迭代计数显示为弹出窗口,python,python-2.7,Python,Python 2.7,我希望将长流程的当前迭代显示为弹出窗口,而不是在终端中。计数需要在同一个弹出窗口内更新(或刷新)。据我所知: from easygui import msgbox msgbox(iterationcount) 启动弹出窗口后不要更新。它还会停止脚本,直到弹出窗口关闭。有人有新的解决方案吗?我不知道msgbox,但如果您不打算使用它,在迭代过程中更新弹出窗口的一种方法可能是通过matplotlib?我知道这是一个解决办法,并不是一个真正的答案,但类似的东西应该可以工作(尽管可以在关闭轴标签等情况
from easygui import msgbox
msgbox(iterationcount)
启动弹出窗口后不要更新。它还会停止脚本,直到弹出窗口关闭。有人有新的解决方案吗?我不知道msgbox,但如果您不打算使用它,在迭代过程中更新弹出窗口的一种方法可能是通过matplotlib?我知道这是一个解决办法,并不是一个真正的答案,但类似的东西应该可以工作(尽管可以在关闭轴标签等情况下进行改进):
我不知道它的性能如何,但如果它是一个长循环,它可能不会有太大影响。弹出窗口永远不会自动刷新。理想情况下,该过程会暂停,直到您关闭窗口。要实现这一点,您需要使用多线程,允许一个python线程运行基本程序,另一个线程在gui中弹出结果 有多种方法可以做到这一点,我在下面强调的是重定向print~stdout。 您所需要做的就是#1定义您的程序——在下面的示例中是its(myprogram)——我将“打印输入”重定向到GUI
希望有帮助。如果您有任何问题,请告诉我:-)您可能可以看看这个:。@WoodBunny-通过快速演示,从中做出答案,您就得到了我的投票。谢谢。无法验证这一点,因为我正在运行osx lion,已知它与matplotlib有缺陷。
from matplotlib import pyplot as pyp
pyp.ion()
pyp.figure() #open the figure
pyp.ylim([0.9,1.1]); pyp.xlim([0.9,1.1]) #set the axis boundaries so the textlabel is central
for i in range(20):
print "in iteration", i
pyp.cla(); #this clears the figure so the text doesn't get overwritten
pyp.text( 1,1,"%7i" %i); #write the number
pyp.draw() #and redraw the image
def myprogram(input):
input = 0
while True:
input = input+1
print input
#Now, prepare your Tkinter window and a thread to execute it.
from Tkinter import *
import threading
import Queue # This is thread safe
import time
class Std_redirector():
def __init__(self, widget):
self.widget = widget
def write(self,string):
self.widget.write(string)
class ThreadSafeText(Text):
def __init__(self, master, **options):
Text.__init__(self, master, **options)
self.queue = Queue.Queue()
self.update_me()
def write(self, line):
self.queue.put(line)
def update_me(self):
while not self.queue.empty():
line = self.queue.get_nowait()
self.insert(END, line)
self.see(END)
self.update_idletasks()
self.after(10, self.update_me)
root = Tk()
text = ThreadSafeText(root)
text.pack()
sys.stdout = Std_redirector(text)
#All you need to do now is pass your function to the thread below,
thread1 = threading.Thread(target=myprogram) # Where myprogram is the function we defined at the beginning
thread1.start()
root.mainloop()