How to Make a Tkinter Window Jump to the Front

How to make a Tkinter window jump to the front?

Assuming you mean your application windows when you say "my other windows", you can use the lift() method on a Toplevel or Tk:

root.lift()

If you want the window to stay above all other windows, use:

root.attributes("-topmost", True)

Where root is your Toplevel or Tk. Don't forget the - infront of "topmost"!

To make it temporary, disable topmost right after:

def raise_above_all(window):
window.attributes('-topmost', 1)
window.attributes('-topmost', 0)

Just pass in the window you want to raise as a argument, and this should work.

How to bring Tkinter window in front of other windows?

Set it as the topmost (but it will always stay in front of the others):

win.attributes('-topmost', True) # note - before topmost

To not make it always in front of the others, insert this code before the mainloop:

win.lift()
win.attributes('-topmost', True)
win.attributes('-topmost', False)

Don't forget win.mainloop() at the end of your code (even if in some cases it's not explicitly required)

Other discussions on the same problem:

  • How to put a Tkinter window on top of the others

  • How to make a Tkinter window jump to the front?

Make Tkinter jump to the front - but only relative to the Tkinter program

At the end the lift() method call did not work well. When I use it and I cover the window I want to remain on top, this call just lets the icon of the window to be highlighted in the taskbar (as it happens when you have some action in a non visible window), but the window is not lifted.

A Linux glitch? I dont know

I finally discovered a way to do it: just call wm_transient from the window that has to stay on top:

secondarywindow.wm_transient(root)

This way, secondarywindow is always above root, but it can be obscured by any other programs I'm using on the desktop

Root window jump to front when opening file dialog in child window tkinter

Below code worked for me. GUI is my toplevel window, and root is my main window.

    GUI.attributes('-topmost', 0)
filename = filedialog.askopenfilename()
GUI.attributes('-topmost', 1)

Once the filedialog opens file, I will bring back my GUI window to front.

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

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.

How to make a window bounce back when it touches edge of screen (tinker)

If you need to modify global variables, then don't pass them as parameters. Instead, add

def movewin(win):
global velx
global vely

at the top of your function.

BIG FOLLOWUP

The more important issue in your app has to do with coordinates. root.winfo_x() and root.winfo_y() do NOT return the upper left corner of your window. Instead, they return the upper left corner of the drawable area, inside the borders and below the title bar. That screws up your drawing, and means you try to position off the bottom of the screen, which Tkinter fixes up.

The solution here is to track the x and y positions yourself, instead of fetching them from Tk.

Tkinter is mostly garbage. You would be better served by looking at pygame for simple games, or at a real GUI system like Qt or wxPython for applications.

from time import sleep
from tkinter import *

class Window(Tk):
def __init__(self):
Tk.__init__(self)
self.width = 300
self.height = 300
self.velx = 1
self.vely = 1
self.pos = (250,250)
self.geometry(f"{self.width}x{self.height}+{self.pos[0]}+{self.pos[1]}")

def moveWin(self):
x = self.pos[0] + self.velx
y = self.pos[1] + self.vely
downx, downy = x+self.width, y+self.height
sWidth = self.winfo_screenwidth() # gives 1366
sHeight = self.winfo_screenheight() # gives 1080
if x <= 0 or downx >= sWidth:
self.velx = -self.velx
if y <= 0 or downy >= sHeight:
self.vely = -self.vely
self.pos = (x,y)
self.geometry(f"+{x}+{y}")
return [x, y, downx, downy]

root = Window()
while True:
root.update()
pos = root.moveWin()
print(pos)
sleep(0.01)


Related Topics



Leave a reply



Submit