Last active
April 11, 2023 12:05
-
-
Save melardev/235cabb0d0497ea581ad181b1689da46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
How to pop a message box without blocking the script. It's running in the background, so it may not be in best practices | |
but it works which is what I wanted. | |
""" | |
import threading | |
import time | |
import tkinter as tk | |
def show_dialog(title: str, message: str): | |
# Create a new Tkinter window | |
root = tk.Tk() | |
root.withdraw() # Hide the window | |
# Create a popup message box | |
popup = tk.Toplevel(root) | |
popup.title(title) | |
label = tk.Label(popup, text=message) | |
label.pack(padx=40, pady=40) | |
# Set the position of the popup message box on the screen | |
screen_width = root.winfo_screenwidth() | |
screen_height = root.winfo_screenheight() | |
x = 1200 # Specify the x-coordinate of the top-left corner of the popup window | |
y = 600 # Specify the y-coordinate of the top-left corner of the popup window | |
popup.geometry(f'+{x}+{y}') | |
# Show the popup message box | |
popup.focus_set() | |
popup.grab_set() | |
popup.wait_window() | |
# Close the Tkinter window | |
root.destroy() | |
def show_popup(title, description: str): | |
thread = threading.Thread(target=show_dialog, args=[title, description]) | |
thread.start() | |
show_popup('Title', 'Description') | |
for i in range(0, 10): | |
time.sleep(1) | |
print(f'Tick {i}') | |
# os._exit(0) # to exit the program even if the Dialog has not been closed yet. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment