简体   繁体   English

showinfo 和 showwarning 出现在 tkinter.messagebox 的背景中

[英]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 ,然后调用showwarningshowinfo 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.

相关问题 tkinter.messagebox中的“ For”循环? - “For” loops within a tkinter.messagebox? tkinter.messagebox 之后如何进入 - How after tkinter.messagebox then entry Python - tkinter.messagebox 在 macOS 中看不到标题和显示错误图标 - Python - tkinter.messagebox cannot see title and showerror icon in macOS “if”语句用于显示错误 tkinter.messagebox 不起作用 - "if" statement used to display an error tkinter.messagebox is not working 导入 tkinter 的“messagebox”模块时,“import tkinter.messagebox”语法是否不起作用? - When importing tkinter's “messagebox” module, does the “import tkinter.messagebox” syntax not work? tkinter.messagebox.showinfo 并不总是有效 - tkinter.messagebox.showinfo doesn't always work 调用 tkinter.messagebox.showwarning() 后未调用条目 validatecommand - Entry validatecommand not called after tkinter.messagebox.showwarning() has been called 如何在此代码中获取 tkinter.messagebox 的名称条目? - How can I get the name's Entry for tkinter.messagebox in this code? 为什么导入tkinter之后,我需要导入tkinter.messagebox但不需要导入tkinter.Tk()? - Why do I need to import tkinter.messagebox but don't need to import tkinter.Tk() after importing tkinter? 在PyCharm中使用“ messagebox.showinfo” - Using “messagebox.showinfo” in PyCharm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM