1
votes

I have a problem in using Tkinter as follows:

The following simple code is to test the scrolledtext widget. And what I need to realize is that I want the result continously (incrementally?) displayed in the scrolledtext widget, where i can clearly know which step is processing. but not to wait the whole iteration is over. It is ok when the total number of iterations is small (less than 10^5), but if the number is large (e.g. 10^8 or 10^10), it has to wait long time. Can anyone gives me some suggestions?

# -*- coding: utf-8 -*-

import tkinter as tk


win = tk.Tk()

text_area = tk.scrolledtext.ScrolledText(win)

for i in range(10000000):
    text_area.insert(tk.END, "step " + str(i) + "\n")

text_area.see(tk.END)
text_area.pack()


win.mainloop()
1

1 Answers

1
votes

You can use .after():

import tkinter as tk
from tkinter import scrolledtext

win = tk.Tk()
def add_to_frame(i):
    if i<10000000:
        text_area.insert(tk.END, "step " + str(i) + "\n")
        text_area.see(tk.END)
        win.after(1,add_to_frame,i+1)
text_area = scrolledtext.ScrolledText(win)


text_area.see(tk.END)
text_area.pack()
i=0
win.after(1,add_to_frame,i)

win.mainloop()