I want to display a warning message and an info message using tkinter.messagebox
. I create and withdraw the root
, then I call showwarning
and showinfo
. The root window disappears, but do does the message box. It actually goes into the background, with no button on the task bar. The only way to access it is to alt+tab
If I comment out the root.withdraw()
calling, both the root and the message box appear.
What am I doing wrong?
Code:
import tkinter as tk
from tkinter.messagebox import showinfo, showwarning
def create_database():
root = tk.Tk()
root.withdraw()
if os.path.exists(create_url()):
showwarning('Failure', 'You failed!')
else:
showinfo('Success!', 'Everything went fine')
root.destroy()
This is because Flask
is blocking tkinter
, as stated here. The way to solve it is to put the tkinter
window in separate processes. Thus, the code in the question becomes:
from multiprocessing import Process
from tkinter.messagebox import showinfo, showwarning
def show_warning_window():
root = tk.Tk()
root.withdraw()
showwarning('File exists', 'The database file already exists!')
root.destroy()
def show_info_window():
root = tk.Tk()
root.withdraw()
showinfo('Success!', 'The database was created.')
root.destroy()
def create_database():
if os.path.exists(create_url()):
p = Process(target=show_warning_window)
p.start()
p.join()
else:
engine = create_engine(create_uri(), echo=True)
Base.metadata.create_all(engine)
p = Process(target=show_info_window)
p.start()
p.join()
Later edit: for this to work, is important that the server should not be in development mode. The set_env variable should NOT be set as development