Python 3 Tkinter - Messagebox with a toplevel as master?

Aelys picture Aelys · Jul 28, 2013 · Viewed 16.3k times · Source

I've found that when a toplevel widget calls a messagebox dialog (like "showinfo"), the root window is showed up, over the toplevel. Is there a way to set the Toplevel window as the master of the messagebox dialog ?

Here is a script to reproduce this :

# -*- coding:utf-8 -*-
# PYTHON 3 ONLY

from tkinter import *
from tkinter import messagebox

root = Tk()
root.title('ROOT WINDOW')
Label(root, text = 'Place the toplevel window over the root window\nThen, push the button and you will see that the root window is again over the toplevel').grid()

topWindow = Toplevel(root)
topWindow.title('TOPLEVEL WINDOW')
Label(topWindow, text = 'This button will open a messagebox but will\ndo a "focus_force()" thing on the root window').grid()
Button(topWindow, text = '[Push me !]', command = lambda: messagebox.showinfo('foo', 'bar!')).grid()

# --

root.mainloop()

Answer

Bryan Oakley picture Bryan Oakley · Jul 28, 2013

You can set the parent argument to topWindow for the showInfo command:

Button(..., command=lambda: messagebox.showInfo(parent=topWindow, ...))

See also: