如何通过messagebox.showerror创建模态对话框

时间:2018-05-04 05:53:58

标签: python-3.x tkinter

如何通过messagebox.showerror创建模态对话框?

messagebox.showerror("Error", "No downloader.exe found")

当我创建一个消息框时,我发现我可以移动根窗口。 我需要创建一个模态对话框,如filedialog.askopenfilename。

filedialog.askopenfilename(initialdir = self.get_path()+ '/bin', filetypes=[("BIN Files", ".bin")])

这里是代码:

import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os

class Application(Frame):

    def createWidgets(self, main_frame):

        #self.llabel = Label(main_frame, text="Ready", width=20, bg="turquoise", font = ftLabel)
        #self.llabel.grid(row=0, column=0, sticky=W+E) #columnspan=2

        self.frame1 = Frame(main_frame)
        self.frame1.grid(row=0, column=0, columnspan=2, sticky=W+E+N+S)

        self.addr = StringVar()
        self.addrtext = Entry(self.frame1, width=20, textvariable = self.addr)
        self.addrtext.grid(row=0, column=0, sticky=W+E+N+S)
        self.addr.set("0x0")

        self.bfile = Button(self.frame1, text='BIN File', width=20)
        self.bfile.grid(row=0, column=1, sticky=W+E+N+S)

        messagebox.showerror("Error", "No downloader.exe found")

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack(fill=BOTH, expand=1)
        main_frame = Frame(master)
        main_frame.pack(fill="y", expand=1)
        self.createWidgets(main_frame)
        self.dl_thread = 0


if __name__=="__main__":

    root = Tk()

    #lock the root size
    root.resizable(False,False)

    app = Application(master=root)
    app.mainloop()

1 个答案:

答案 0 :(得分:0)

我尝试了您的代码,messagebox.showerror对我来说是模态的。

也许你的代码中有其他东西(线程?)或者可能是它 取决于您的环境。

供参考,我的整个代码:

from tkinter import *
from tkinter import messagebox          

root = Tk()
def do(): messagebox.showerror("Error", "No downloader.exe found")

b = Button(root, text='Dialog', command=do)
b.pack()

root.mainloop()

如果这不起作用,您可能需要查看:Tkinter messagebox not behaving like a modal dialog