Skip to content Skip to sidebar Skip to footer

Force Set Tkinter Window To Always Have Focus

Is there a way to tell Tkinter that I want some widget to always remain focused? I've created a minimal example that can be run to show my issue , here's an example window with sma

Solution 1:

change this:

if dialogs:# if there are any dialogs left:for widget in dialogs:# go through each widgetwidget.lift(aboveThis=self.root)# and lift it above the root

to this:

if dialogs:# if there are any dialogs left:for widget in dialogs:# go through each widgetwidget.lift()# and lift it above the root

the widgets will stay above the main window.

EDIT:

Sorry that only half worked... the widows will stay above sometimes with that code

:-X

It was keeping the widgets on top until you closed one of them.... this code does keep the widgets on top

it uses the self.attributes("-topmost", True) when you spawn the windows.

Sorry again.

from Tkinter import *

classPropertyDialog(Toplevel):
    def__init__(self, root, string):
        Toplevel.__init__(self)
        self.wm_overrideredirect(1)
        self.root = root 
        self.\
             geometry('+%d+%d' %
                      (root.winfo_pointerx(),
                       root.winfo_pointery()))
        try:
            self.tk.call('::Tk::unsupported::MacWindowStyle',
                                         'style', self._w,
                                         'help', 'noActivates')
        except TclError:
            pass
        window_frame = Frame(self)
        window_frame.pack(side=TOP, fill=BOTH, expand=True)
        exit_frame = Frame(window_frame, background='#ffffe0')
        exit_frame.pack(side=TOP, fill=X, expand=True)
        button = Button(exit_frame, text='x', width=3, command=self.free,
               background='#ffffe0', highlightthickness=0, relief=FLAT)
        button.pack(side=RIGHT)
        text_frame = Frame(window_frame)
        text_frame.pack(side=TOP, fill=BOTH, expand=True)
        label = Label(text_frame, text=string, justify=LEFT,
                      background='#ffffe0',
                      font=('tahoma', '8', 'normal'))
        label.pack(ipadx=1)
        self.attributes("-topmost", True)

    deffree(self):
        self.destroy() # first we destroy this onedefbind():
    """
    toggle property window creation mode
    """
    root.bind('<ButtonPress-1>', create)


defcreate(event):
    """
    Create actual window upon mouse click
    """
    dialogs.append(PropertyDialog(root, 'help me'))

root = Tk()
dialogs = []
root.geometry('%dx%d' % (300,400))

Button(root, text='create', command=bind).pack()

root.mainloop()

Solution 2:

I recommend moving away from Toplevel widgets, since those are separate windows and you're suppressing their window-like behavior. This version makes PropertyDialog inherit from Frame instead of Toplevel, using the place() geometry manager. When you click the main window, it first checks whether the widget clicked was the main window or a popup window to prevent a new popup from appearing when you close an existing one. Changed areas are marked with #CHANGED#.

from Tkinter import *

classPropertyDialog(Frame): #CHANGED#def__init__(self, root, string, event): #CHANGED#
        Frame.__init__(self) #CHANGED#
        self.root = root 
        try:
            self.tk.call('::Tk::unsupported::MacWindowStyle',
                                         'style', self._w,
                                         'help', 'noActivates')
        except TclError:
            pass
        exit_frame = Frame(self, background='#ffffe0') #CHANGED#
        exit_frame.pack(side=TOP, fill=X, expand=True)
        button = Button(exit_frame, text='x', width=3, command=self.free,
               background='#ffffe0', highlightthickness=0, relief=FLAT)
        button.pack(side=RIGHT)
        text_frame = Frame(self) #CHANGED#
        text_frame.pack(side=TOP, fill=BOTH, expand=True)
        label = Label(text_frame, text=string, justify=LEFT,
                      background='#ffffe0',
                      font=('tahoma', '8', 'normal'))
        label.pack(ipadx=1)
        self.place(x=event.x, y=event.y, anchor=NW) #CHANGED#deffree(self):
        self.destroy()
        # other things you want to do - if there's nothing else,# just bind the close button to self.destroydefbind():
    """
    toggle property window creation mode
    """
    root.bind('<ButtonPress-1>', create)


defcreate(event):
    """
    Create actual window upon mouse click
    """if event.widget is root: #CHANGED#
        dialogs.append(PropertyDialog(root, 'help me', event))

root = Tk()
dialogs = []
root.geometry('%dx%d' % (300,400))

Button(root, text='create', command=bind).pack()

root.mainloop()

Post a Comment for "Force Set Tkinter Window To Always Have Focus"