Search code examples
pythontkinterpython-3.5toplevel

Tkinter Toplevel() positioning without static geometry


Im using Toplevel() for popup windows and I want the popup to be displayed to the right of the mouse when it comes up. I found how to do this but only by specifying the geometry of the window. How can I control where the window comes up without specifying the size. I want the window to be the size it needs to be for whatever data is is going to display.

This is what im using right now:

helpwindow = Toplevel()
helpwindow.overrideredirect(1)
helpwindow.geometry("662x390+{0}+{1}".format(event.x_root - 1, event.y_root - 12))

How can I put only the format settings in the window geometry? Or is their a better way?


Solution

  • Use "+{}+{}" without size

    helpwindow.geometry("+{}+{}".format(event.x_root - 1, event.y_root - 12))
    

    ie. moving window :)

    import tkinter as tk
    
    def move():
        global pos_x
    
        helpwindow.geometry("+{}+200".format(pos_x))
        pos_x += 10
    
        root.after(100, move)
    
    root = tk.Tk()
    
    pos_x = 0    
    helpwindow = tk.Toplevel()
    move()
    
    root.mainloop()