Reputation: 4411
i am trying to make non-blocking gui while there is code execution in while loop with this code but without success - window stops responding. What should I change in this code please?
from Tkinter import *
import time
ROOT = Tk()
# create a Frame for the Text and Scrollbar
txt_frm = Frame(ROOT, width=600, height=600)
txt_frm.pack(fill="both", expand=True)
# ensure a consistent GUI size
txt_frm.grid_propagate(False)
# implement stretchability
txt_frm.grid_rowconfigure(0, weight=1)
txt_frm.grid_columnconfigure(0, weight=1)
# create a Text widget
txt = Text(txt_frm, borderwidth=3, relief="sunken")
txt.config(font=("consolas", 12), undo=True, wrap='word')
txt.grid(row=0, column=0, sticky="nsew", padx=2, pady=2)
# create a Scrollbar and associate it with txt
scrollb = Scrollbar(txt_frm, command=txt.yview)
scrollb.grid(row=0, column=1, sticky='nsew')
txt['yscrollcommand'] = scrollb.set
txt.insert(END, "hello\n")
txt.pack()
while True:
ROOT.update()
time.sleep(10)
txt.insert(END, "hello here\n")
txt.pack()
Upvotes: 0
Views: 649
Reputation: 4411
i found workable solution
#from Tkinter import *
from Tkinter import Tk, Text, Frame, Scrollbar, END
import time
# while True:
# ROOT.update()
#
# time.sleep(10)
# txt.insert(END, "hello here\n")
# txt.pack()
import threading
class ThreadingExample(object):
""" Threading example class
The run() method will be started and it will run in the background
until the application exits.
"""
def __init__(self, interval=1):
""" Constructor
:type interval: int
:param interval: Check interval, in seconds
"""
self.interval = interval
thread = threading.Thread(target=self.run, args=())
thread.daemon = True # Daemonize thread
thread.start() # Start the execution
def run(self):
""" Method that runs forever """
time.sleep(1)
global ROOT
while True:
ROOT.update()
time.sleep(2)
txt.insert(END, "hello here\n")
txt.pack()
print "aa"
global ROOT
ROOT = Tk()
# create a Frame for the Text and Scrollbar
txt_frm = Frame(ROOT, width=600, height=600)
txt_frm.pack(fill="both", expand=True)
# ensure a consistent GUI size
txt_frm.grid_propagate(False)
# implement stretchability
txt_frm.grid_rowconfigure(0, weight=1)
txt_frm.grid_columnconfigure(0, weight=1)
# create a Text widget
txt = Text(txt_frm, borderwidth=3, relief="sunken")
txt.config(font=("consolas", 12), undo=True, wrap='word')
txt.grid(row=0, column=0, sticky="nsew", padx=2, pady=2)
# create a Scrollbar and associate it with txt
scrollb = Scrollbar(txt_frm, command=txt.yview)
scrollb.grid(row=0, column=1, sticky='nsew')
txt['yscrollcommand'] = scrollb.set
txt.insert(END, "hello\n")
txt.pack()
example = ThreadingExample()
Upvotes: 1