How to keep the window focus on new Toplevel() window in Tkinter?

The concept you are looking for is called a "grab". Tkinter supports grabs with several methods. For example, to set a local grab on a toplevel you would do my_window.grab_set(). A local grab is where this window grabs the focus from all other windows in your app, but only for your app.

You can also do a global grab, which effectively freezes your entire display except for your specific window. This is very dangerous since you can easily lock yourself out of your own computer if you have a bug in your code.

Grabs do not deactivate the mainloop function. This must be running for your window to process events. It simply redirects all events to the window that has the grab.

For more information, read about grab_set and other grab commands here: http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.grab_set-method


grab.set() is much to aggressive for my needs and blocks normal window operations. This answer is to make one Tkinter Window pop up overtop of other Tkinter windows.

In my app I have a large window toplevel which calls a much smaller window top2 which initially appears on top of toplevel.

If user clicks within toplevel window it gains focus and smothers much smaller top2 window until toplevel window is dragged off of it.

The solution is to click the button in toplevel to launch top2 again. The top2 open function knows it is already running so simply lifts it to the top and gives it focus:

def play_items(self):
    ''' Play 1 or more songs in listbox.selection(). Define buttons:
            Close, Pause, Prev, Next, Commercial and Intermission
    '''

    if self.top2_is_active is True:
        self.top2.focus_force()     # Get focus
        self.top2.lift()            # Raise in stacking order
        root.update()
        return                      # Don't want to start playing again

None of the above suggestions worked for me, on Mac OS El Capitan. But based on those hints, this does:

class Window(Tk.Toplevel):
    ...
    def setActive(self):
        self.lift()
        self.focus_force()
        self.grab_set()
        self.grab_release()
    ...