0

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()    

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

The term sleep is not used in update processes. Tkinter has an internal process. Processes reach data, certainly not touch GUI items. Consider them for performance and profitability.
hi, thanks for your message. Could you please give me example how to change it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.