8

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()
tshepang
  • 12,111
  • 21
  • 91
  • 136
Aelys
  • 241
  • 1
  • 4
  • 19

4 Answers4

16

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

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

See also:

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
3

This may address more current versions.

#TEST AREA forcommands/methods/options/attributes
#standard set up header code 2 
from tkinter import *
from tkinter import messagebox
root = Tk()
root.attributes('-fullscreen', True)
root.configure(background='white')
scrW = root.winfo_screenwidth()
scrH = root.winfo_screenheight()  
workwindow = str(1024) + "x" + str(768)+ "+" +str(int((scrW-1024)/2)) + "+" +str(int((scrH-768)/2))
top1 = Toplevel(root, bg="light blue")
top1.geometry(workwindow)
top1.title("Top 1 - Workwindow")
top1.attributes("-topmost", 1)  # make sure top1 is on top to start
root.update()                   # but don't leave it locked in place
top1.attributes("-topmost", 0)  # in case you use lower or lift
#exit button - note: uses grid
b3=Button(root, text="Egress", command=root.destroy)
b3.grid(row=0,column=0,ipadx=10, ipady=10, pady=5, padx=5, sticky = W+N)
#____________________________
root.withdraw()
mb1=messagebox.askquestion(top1, "Pay attention: \nThis is the message?")
messagebox.showinfo("Say Hello", "Hello World")
root.deiconify()
top1.lift(aboveThis=None)
#____________________________
root.mainloop()
Aelys
  • 241
  • 1
  • 4
  • 19
BigDaddy
  • 41
  • 4
  • I think this may address more current versions. – BigDaddy Jan 07 '18 at 15:00
  • Please edit your post to include your comment. Comments are for asking for clarification and answering other comments. They are second class citizens and are frequently deleted. Your comment should have been part of your answer to start of with. If you forgot to include it, you should have made an edit to your post instead of posting it as a comment. – robinCTS Jan 07 '18 at 18:08
  • Opps, comment should I been, this may address more recent versions: coordinates a root, toplevel and message box - 1 of several possible solutions. – BigDaddy Jan 08 '18 at 19:17
  • Please re-read my previous comment. To [edit your answer](//stackoverflow.com/posts/48138378/edit), click the word [edit](//stackoverflow.com/posts/48138378/edit) at the bottom left of the answer (between the words *share* and *flag*). – robinCTS Jan 08 '18 at 23:04
1
Button(..., command=lambda: messagebox.showinfo("The Title", "A piece of text", parent=topWindow))

This will definitely work. The syntax of the messagebox is

messagebox.Function_Name(title, message [, options]) 

setting the parent argument is one of the options

Pro Chess
  • 831
  • 1
  • 8
  • 23
  • Can you give a little more info about why this solution works and solves the OPs problem? – Matthew Barlowe Jul 08 '20 at 19:04
  • By setting the parent argument to topWindow for the showinfo command the above problem can be solved. By doing this the tkinter messagebox's master will be set to the topWindow. Therefore, the root window will not be shown above the topWindow. – Pro Chess Jul 09 '20 at 21:09
0

add topWindow.attributes("-topmost", 1) after defining the Toplevel, and this should fix the problem

washi
  • 1