To achieve your goal of creating a custom GUI that displays outputs from two threads in two different parts of the window, you can use Python's tkinter library. This GUI library allows you to create a simple user interface where you can direct the output from each thread to a specific part of the window. In this example, I will show you how to create a basic GUI with two text areas, each dedicated to displaying the output from one of your two functions.
Please note, updating tkinter GUI elements from threads other than the main thread requires careful handling to avoid potential conflicts. You can use the queue module to safely pass messages from your worker threads to the GUI's main loop, which will then update the respective text areas.
Here is how you could implement it:
import tkinter as tk
from tkinter import scrolledtext
import threading
import time
import queue
# Function to periodically send numbers through the queue
def print_numbers(output_queue):
for i in range(1, 6):
time.sleep(1)
output_queue.put(f"Number: {i}\n")
# Function to periodically send letters through the queue
def print_letters(output_queue):
for letter in ["A", "B", "C", "D", "E"]:
time.sleep(1.5)
output_queue.put(f"Letter: {letter}\n")
# Main application class
class Application(tk.Tk):
def __init__(self):
super().__init__()
self.title("Thread Output")
# Create a queue for thread-safe communication
self.output_queue = queue.Queue()
# Create two scrolled text areas
self.text_area_letters = scrolledtext.ScrolledText(self, state='disabled', height=10, width=50)
self.text_area_letters.grid(row=0, column=0, padx=10, pady=10)
self.text_area_numbers = scrolledtext.ScrolledText(self, state='disabled', height=10, width=50)
self.text_area_numbers.grid(row=0, column=1, padx=10, pady=10)
# Start the threads
threading.Thread(target=print_letters, args=(self.output_queue,), daemon=True).start()
threading.Thread(target=print_numbers, args=(self.output_queue,), daemon=True).start()
# Start the update process
self.update_output()
def update_output(self):
try:
# Try to get an output message from the queue
while not self.output_queue.empty():
message = self.output_queue.get_nowait()
# Decide where to put the message based on its content
if message.startswith("Letter:"):
text_area = self.text_area_letters
else:
text_area = self.text_area_numbers
# Ensure the text area is in a normal state (so we can insert text)
text_area.configure(state='normal')
text_area.insert(tk.END, message)
text_area.configure(state='disabled')
# Auto-scroll
text_area.yview(tk.END)
except queue.Empty:
pass
# Schedule the update_output function to be called after 100ms
self.after(100, self.update_output)
if __name__ == "__main__":
app = Application()
app.mainloop()
Here's a breakdown of how it works:
- Two worker functions,
print_numbers and print_letters, send messages (strings) through a shared queue.Queue() instance, which is a thread-safe way to communicate between threads.
- The
Application class creates a simple window using tkinter with two ScrolledText widgets for displaying text.
- Two threads are started, each running one of the worker functions and passing messages to the GUI through the queue.
- The
update_output method in the Application class checks the queue for new messages every 100ms and updates the appropriate text area with the received messages.
This way, you maintain a responsive GUI and correctly handle updating GUI elements across threads using tkinter.