How to put a tkinter window on top of the others?

梦想与她 提交于 2019-11-27 12:47:26
vdbuilder

If I take the code you give and add the first and last line you get:

from tkinter import *

root = Tk() 
root.title("app")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.geometry("550x250+%d+%d" % (screen_width/2-275, screen_height/2-125))
root.configure(background='gold')
root.lift()

mainloop()

Test it. I get the window as expected. Do you get something else? If this works then somewhere in the code you are telling it to do that. If it does the same thing as your real program then your window manager is doing it. This is the best I can do without more information.

Edit:

On OSX (espicially versions using aqua) tkinter's windows may be displayed behind those that are already open (this has a bug report here: http://bugs.python.org/issue9384 but has been closed as will not fix). The addition of the root.lift() command has been included to bring the window to the front of the stack in those cases and is harmless in all others.

Sarim

I got into same issue today. OSX LION 10.7.2. Add this code before mainloop() solves the issue.

root.call('wm', 'attributes', '.', '-topmost', '1')

but the window always remains on top of the others until you close it. For real solve, we need to make it an app bundle, with py2app.

I know this is an old question but I found it weird that no one came up with the simple solution I had,

app = SampleApp()

app.attributes('-topmost', True)
app.update()
app.attributes('-topmost', False)

app.mainloop()
jjaderberg

For OS X 10.8.3, the combination of the answers provided by vdbuilder and user2435139 did the trick for me, i.e.

self.root.lift()
self.root.call('wm', 'attributes', '.', '-topmost', True)
self.root.after_idle(self.root.call, 'wm', 'attributes', '.', '-topmost', False)

called before

self.root.mainloop()
Arnaud P

More for mac OS users. Although the above solutions seem to display correctly, the app is still put at "the end of the stack" from the Finder's point of view. As can be seen with the Cmd+Tab switcher, or simply observing that python doesn't get the focus.

Solution from username fixing it all (again, for mac OS):

import os
os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')

Maybe surround that with something like

import platform
if "Darwin" in platform.system():
    # apply fix

I modified the above solution and these 2 lines work for me on OSX. It brings the window to the front, but without making the window behave as Always on Top.

root.call('wm', 'attributes', '.', '-topmost', True)
root.after_idle(root.call, 'wm', 'attributes', '.', '-topmost', False)
Debilski

The osascript trick Arnaud P could have problems, if there is more than one process with the application title ‘Python’; additionally, it will not work for Python 3 processes (needs to be called ‘Python3’ then.

However, I found another trick that can solve the problem by using the process id.

import os
script = 'tell application "System Events" \
  to set frontmost of the first process whose unix id is {pid} to true'.format(pid=os.getpid())
os.system("/usr/bin/osascript -e '{script}'".format(script=script))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!