Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to keep a Tkinter-Dialog on top of all other windows?

2,571 views
Skip to first unread message

Thomas Nücker

unread,
Jul 2, 2003, 8:35:12 AM7/2/03
to
Hi!

I am creating a dialog-box within my application, using tkinter. The
problem is the following: After the dialogbox is started, the main
application window comes again to top and the dialogbox is covered by
the window of the main application and must be "fetched" again via the
taskbar to continue. Is there a way to "force" the dialogbox on top of
all other windows? (I'm using MSWindows and Python22)

The source of my dialogbox-class is the following:

class NumberEntry:
def __init__(self):
import Tkinter
from Tkconstants import RIDGE
from Tkconstants import BOTH
from Tkconstants import BOTTOM
from Tkconstants import ACTIVE
from Tkconstants import LEFT
from Tkconstants import W
from Tkconstants import E
self.tk = Tkinter.Tk()
self.tk.title("My Dialog")
frame = Tkinter.Frame(self.tk, borderwidth=2)
frame.pack(fill=BOTH,expand=1)

label=Tkinter.Label(frame, text="Telefonnummer:")
label.pack(fill=BOTH,expand=1)

self.entry = Tkinter.Entry(frame, name="entry")
self.entry.pack(fill=BOTH,expand=1)

box = Tkinter.Frame()
w = Tkinter.Button(box, text="OK", width = 10, command=self.OnOk,
default = ACTIVE)
w.pack(side=LEFT, padx=5, pady=5)
w = Tkinter.Button(box, text="Cancel", width=10,
command=self.OnCancel)
w.pack(side=LEFT, padx=5, pady=5)
box.pack()

# try to keep focus on current dialog box
self.tk.focus_set()
self.tk.mainloop()
def OnOk(self):
self.result = self.entry.get()
self.tk.destroy()
def OnCancel(self):
self.tk.destroy()
result = ""

Joe Fromm

unread,
Jul 2, 2003, 9:04:12 AM7/2/03
to
"Thomas Nücker" <thomas....@web.de> wrote in message
news:4b66f6d2.03070...@posting.google.com...

> Hi!
>
> I am creating a dialog-box within my application, using tkinter. The
> problem is the following: After the dialogbox is started, the main
> application window comes again to top and the dialogbox is covered by
> the window of the main application and must be "fetched" again via the
> taskbar to continue. Is there a way to "force" the dialogbox on top of
> all other windows? (I'm using MSWindows and Python22)
>
<snip>

I had a similar problem using Python/tkinter embedded in a Win32 app, and
never found a truly satisfactory solution. The best I was able to do was
have the tk window call into my app, passing its window handle (which you
can get via winfo_id()). In my app I have a timer running - as long as that
window exists it will keep calling SetWindowPos with the HWND_TOP flag to
move the Tk window on top of all the app windows. When given the Tk window
handle you need to hunt up through it's parents until you find a window with
no parent, and call SetWindowPos on that window.

If the window being watched no longer exists I just kill the timer and clear
the window handle.

I'd love to find a solution that isn't such a grotesque hack.

Joe Fromm


Jeff Epler

unread,
Jul 2, 2003, 11:41:52 AM7/2/03
to
my similar code here use HWND_TOPMOST. This is apparently different
from HWND_TOP:

HWND_TOP
Places the window at the top of the Z order.

HWND_TOPMOST
Places the window above all non-topmost windows. The window maintains
its topmost position even when it is deactivated

as far as I know, this code works right with only one call, not a call
from a timer.

Jeff

Jeff Epler

unread,
Jul 2, 2003, 11:56:28 AM7/2/03
to
On Wed, Jul 02, 2003 at 10:44:43AM -0500, Joe Fromm wrote:
> Windows actually has two z-orders. Topmost windows and normal windows. All
> topmost windows are always above normal windows.
>
> So setting a window into the topmost group will keep it on top of your app
> with one call, but it will remain topmost even if another application is
> activated. If that is acceptable to you then setting it to topmost is
> indeed a simpler solution.

Oh.

If you just want to enforce window stacking between two toplevels in
your application, you can use "wm transient" for this.
t = Tkinter.Tk()
u = Tkinter.Toplevel(t)
u.wm_transient(t)
t.mainloop()

however, "wm transient" has other effects (TRANSIENT_STYLE, does not
appear on task bar, etc).

Jeff

0 new messages