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

Time:05-17

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.defaultgeometry='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 behaviour can be turned off?

CodePudding user response:

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?

CodePudding user response:

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)
        ...
  • Related