0

As the title describes, I am creating a Toplevel tkinter window, but after creation, there is about 300 milliseconds when it is acting normal, but then the main window comes on top of it, blocking out half of it, without taking the focus away from the Toplevel window. Example:

enter image description here

This is manually fixed by bringing the main window into focus, and then clicking back on the Toplevel widget, however is a great inconvenience, as the Toplevel is a notification window, so clicking away from it will destroy it. I have tried using the set_grab() command, however it prevents the main window from coming into focus, disabling the manual solution. The lift() command extends the period in which the Toplevel is in focus, however it still gets pushed into the background within one tick of the mainloop. I also tried focusing the main window with focus() and then focusing the Toplevel, as per the manual solution, but to no avail, as this scenario always arrived, regardless of what I tried.

Why is this happening, and how could I fix it?

MRE:

import tkinter as tk

class Notify(tk.Toplevel):
    def __init__(self, master, title, message, time, **messageArgs):
        super().__init__(master=master)
        self.title(title)
        self.message = tk.Label(self, text=message, **messageArgs)
        self.message.pack(anchor='center')
        self.after(time, self.close)
        self.lift() # The most successful one yet, but still does not solve the issue

    def close(self):
        self.master.focus()
        self.destroy()

root = tk.Tk()
Notify(root, 'Blah Blah Blah', 'Spam Spam Spam', 5000)

tk.mainloop()
User 12692182
  • 927
  • 5
  • 16

1 Answers1

1

This answer here: https://stackoverflow.com/a/31927283/12692182 will solve your problem. You need to specify what widgets it is supposed to be on top of in lift():

self.lift(aboveThis=self.master)

instead of:

self.lift()

Note: this was my own answer that I shared to help out others with a similar problem. I still would like help understanding why it works, as well

User 12692182
  • 927
  • 5
  • 16