M
M
Mikhail Emelyanov2021-11-26 14:26:34
Python
Mikhail Emelyanov, 2021-11-26 14:26:34

How to display a graphical window without blocking the main console thread?

In addition to the main flow of the Python program, I want to display a graphical warning window:

from datetime import datetime
import threading
from time import sleep
from tkinter import *

def show_notification_window():
    root = Tk()
    root.geometry("300x300")
    warning_message = Message(root, text="Cthulhu fhtagn!")
    warning_message.pack()
    root.mainloop()


while True:
    print(datetime.now().strftime("%H:%M:%S"))
    
    gui_thread = threading.Thread(target=show_notification_window())
    gui_thread.start()
    
    sleep(1)


The window is displayed, but blocks the main thread until it is closed.

How to make a non-blocking graphics window? The graphics window does not interact with the main thread in any way.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LXSTVAYNE, 2021-11-26
@sepulkary

You do not need to call the window drawing function, you need to pass the function to target without calling it, and if you need to pass parameters to it, then you also pass args as a tuple.

gui_thread = threading.Thread(target=show_notification_window)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question