Fixed ui_print, #6. Calls now processed in main thread.

This commit is contained in:
2021-10-22 21:11:58 +02:00
parent 9e56937252
commit 33331e829a
2 changed files with 29 additions and 6 deletions
+23
View File
@@ -1760,6 +1760,9 @@ class OutputConsole(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, relief=SUNKEN, bd=1)
# Queue for messages to print when this class currently has main thread control
self.print_queue = Queue()
# configure Tkinter grid:
self.grid_rowconfigure(ALL, weight=1)
self.grid_columnconfigure(0, weight=1, minsize=60) # console needs to have a minimum width
@@ -1773,3 +1776,23 @@ class OutputConsole(Frame):
# link scrollbar to the console
scrollbar.config(command=self.console.yview)
self.console.config(yscrollcommand=scrollbar.set)
# Start the main thread for printing queued messages
self.print_thread()
def print_thread(self):
""" Continuous thread that checks if messages are present, and prints them if they are. """
try:
while True:
msg = self.print_queue.get(block=False)
# print to console
self.console.insert(END, "\n" + msg)
except Empty:
pass
self.console.see(END) # scroll console to bottom
# Print messages every 100 ms in the main Tkinter loop
self.after(100, self.print_thread)
def put(self, message):
self.print_queue.put(message)
+6 -6
View File
@@ -7,9 +7,9 @@ def ui_print(*content):
"""prints text to built-in console, use exactly like normal print(). Requires the ui to be initialized"""
output = " ".join([str(c) for c in content])
if not g.exit_flag and g.app is not None: # application is still running --> output window is visible
output = "".join(("\n", output)) # begin new line each time
g.app.OutputConsole.console.insert(END, output) # print to console
g.app.OutputConsole.console.see(END) # scroll console to bottom
else: # if window is not open, do normal print
print(output)
if not g.exit_flag and g.app is not None:
# application is still running --> output window is visible
g.app.OutputConsole.put(output)
else:
# if window is not open, do normal print
print(output)