[英]showinfo and showwarning appearing in the background in tkinter.messagebox
I want to display a warning message and an info message using tkinter.messagebox
.我想使用
tkinter.messagebox
显示警告消息和信息消息。 I create and withdraw the root
, then I call showwarning
and showinfo
.我创建和撤销
root
,然后调用showwarning
和showinfo
。 The root window disappears, but do does the message box.根 window 消失了,但消息框确实消失了。 It actually goes into the background, with no button on the task bar.
它实际上进入了后台,任务栏上没有按钮。 The only way to access it is to alt+tab
访问它的唯一方法是alt+tab
If I comment out the root.withdraw()
calling, both the root and the message box appear.如果我注释掉
root.withdraw()
调用,根和消息框都会出现。
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 .这是因为
Flask
正在阻止tkinter
,如此处所述。 The way to solve it is to put the tkinter
window in separate processes.解决方法是将
tkinter
window 放在单独的进程中。 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
set_env变量不应设置为开发
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.