I can't find anything on this and was wondering if it was even possible.
Is there a way to make a Toplevel window follow the root Tk window when you move the Tk window across the screen?
What i do is build a Tk root window root=Tk()
. Then i build the Toplevel window=Toplevel()
and make the toplevel window flush with the root window on the right side. What I am curious is how to anchor the Toplevel window to the root so when i drag the root window, the Toplevel window follows.
You can bind to the <Configure>
event of the root window, which fires when the window is moved or resized. With that you can adjust the location of the toplevel.
import tkinter as tk
class Example:
def __init__(self):
self.root = tk.Tk()
label = tk.Label(self.root, text="Move me around...")
label.pack(side="top", fill="both", expand=True, padx=20, pady=20)
self.top = tk.Toplevel()
label = tk.Label(self.top, text="... and I will follow!")
label.pack(side="top", fill="both", expand=True, padx=20, pady=20)
self.root.bind("<Configure>", self.sync_windows)
def start(self):
self.root.mainloop()
def sync_windows(self, event=None):
x = self.root.winfo_x() + self.root.winfo_width() + 4
y = self.root.winfo_y()
self.top.geometry("+%d+%d" % (x,y))
Example().start()