Skip to content
Advertisement

How to make pop-up window with force attention in Tkinter

I want to create a window which doesn’t allow the user to access other windows until you give an input. I tried win.attribute("ontop", True) but it allows the user to access other windows. or is there any function like a force_focus_lock() in Tkinter python 3.8 which doesn’t allow other window to get focus until you give a input or the close present window.

Advertisement

Answer

I believe the below is what you are trying to do. Explanation is given in comments.

method #1: (PopOut1)

  • you can still move the main window
  • the new window assumes focus if there is a mouse release on main window

method #2: (PopOut2)

  • the main window is locked in place
  • the new window will assume focus, blink and “ding” if there is a mouse release on main window

import tkinter as tk


#first method
class PopOut1(tk.Toplevel):
    def __init__(self, master, **kwargs):
        tk.Toplevel.__init__(self, master, **kwargs)
        self.geometry('400x300')

        #set focus to this window
        self.focus_set()
        
        #releasing on any other tkinter window, within this process, forces focus back to this window
        self.grab_set()


#second method
class PopOut2(tk.Toplevel):
    def __init__(self, master, **kwargs):
        tk.Toplevel.__init__(self, master, **kwargs)
        self.geometry('400x300')
        
        #set focus to this window
        self.focus_set()

        #disable the main window
        master.attributes('-disabled', True)

        #so this window can't end up behind the disabled window
        #only necessary if this window is not transient
        #self.attributes('-topmost', True)

        #capture close event
        self.protocol("WM_DELETE_WINDOW", self.close)

    #event=None ~ in case you also want to bind this to something
    def close(self, event=None):
        #re-enable the main window
        self.master.attributes('-disabled', False)
        #destroy this window
        self.destroy()


class App(tk.Tk):
    TITLE = 'Application'
    WIDTH, HEIGHT, X, Y = 800, 600, 50, 50

    def __init__(self):
        tk.Tk.__init__(self)
        tk.Button(self, text="open popout 1", command=self.open1).grid()
        tk.Button(self, text="open popout 2", command=self.open2).grid()

    def open1(self):
        PopOut1(self)

    def open2(self):
        #.transient(self) ~ 
        #    flash PopOut if focus is attempted on main
        #    automatically drawn above parent
        #    will not appear in taskbar
        PopOut2(self).transient(self)


if __name__ == '__main__':
    app = App()
    app.title(App.TITLE)
    app.geometry(f'{App.WIDTH}x{App.HEIGHT}+{App.X}+{App.Y}')
    #app.resizable(width=False, height=False)
    app.mainloop()
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement