1
votes

I'm using Tkinter to create a basic GUI. I have a main window (let's say window A) that opens a second window (window B). Window B has a file dialog message, and a few messageboxes. Whenever these open, they automatically lower window B behind window A. How do I prevent this?

Here are two scripts relating to the two windows. a.py creates window A, and opens b.py:

# a.py
import tkinter as tk
import b


def open_b():
    b.open_me()

window = tk.Tk()
window.title('a')
button = tk.Button(window, text='open b', command=open_b)
button.pack()

window.mainloop()

and b.py creates window B, which can create a messagebox.

# b.py
import tkinter as tk
from tkinter import messagebox


def open_me():

    def warning():
        messagebox.showwarning('Warning', f"I just lowered window B below window A")

    top_window = tk.Toplevel()
    top_window.title('b')
    warning_button = tk.Button(top_window, text='open dialog', command=warning)
    warning_button.pack()

    top_window.mainloop()

As you can see, when the "open b" button is clicked on window A, window B is created on top of it. Then if you click the "open dialog" button on window B, a messagebox is created, but it pushes window B below window A.

1

1 Answers

1
votes

That happens because you haven't specified the parent of the messagebox, by default it assumes the root (window in your case) and hence will push other toplevels down when called. To get around this specify the parent option.

messagebox.showwarning('Warning', f"I just lowered window B below window A",parent=top_window)