Can the auto-placement of tkinter windows be turned off?

Question:

I have this very basic code

from tkinter import *
class GUI(Tk):
    def __init__(self):
        super().__init__()
        self.geometry('600x400')
        Button(self, text="Show new window",          command=self.show_window).pack()

    def show_window(self):
        smallwin = display()
   
class display(Toplevel):
    def __init__(self):
        super().__init__()
        self.geometry('300x300+30+30')
        self.attributes('-topmost',True)

root = GUI()
root.mainloop()

When you click on the button, a child window appears. When you press it again, a second child window appears etc etc, BUT each new window is to the right and further down from the last one.

Screen shot of staggered windows

I would like to know if this automatic behavior can be turned off?

Asked By: Brian

||

Answers:

you can just set the location of the window. Then all windows will open a this exact location.

E.g.

root.geometry('250x150+0+0')

More detailed solutions are described here:

How to specify where a Tkinter window opens?

Answered By: PKL

If you explicitly set the geometry for each window, they will go wherever you tell them to go.

You seem to be setting a geometry, but you aren’t using it. If you pass that value to the geometry method, the window will go to that exact location.

class display(Toplevel):
    def __init__(self):
        super().__init__()
        self.defaultgeometry='300x300+30+30'
        self.wm_geometry(self.defaultgeometry)
        ...
Answered By: Bryan Oakley
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.