[python-win32] keep tkinter window always on top

Benjamin Rutt rutt at bmi.osu.edu
Sun Aug 21 22:31:49 CEST 2005


Does anyone know how to keep a tkinter window always on top, despite
attempts to focus other windows?  Here is my failed attempt.  I've
tried both tkinter-only and win32-specific solutions.  For this code,
the window does stay in front of the dos box it's launched from if I
try to focus the dos box, but that's the only one it stays in front
of.  Thanks, Benjamin

#!/usr/bin/env python

from Tkinter import *
import win32ui, win32con, win32gui

root = None
reps = 0

def show():
    # try everything to keep this window on top
    root.lift()
    root.focus_force()
    hwnd = root.winfo_id()
    pycwnd = win32ui.CreateWindowFromHandle(hwnd)
    pycwnd.ShowWindow(win32con.SW_RESTORE)
    pycwnd.ShowWindow(win32con.SW_SHOWNORMAL)    
    pycwnd.BringWindowToTop()    
    win32gui.SetFocus(hwnd)
    win32gui.BringWindowToTop(hwnd)

def reshow():
    global reps
    reps += 1
    print 'reshow(%d)' % (reps)
    show()
    if reps < 100:
        root.after(100, reshow)
    else:
        root.destroy()

root = Tk()
root.config(width=400, height=400)
show()
root.after(100, reshow)
root.mainloop()



More information about the Python-win32 mailing list