1

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?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Brian
  • 25
  • 4
  • What do you mean, turned off? What do you want to happen when a new window is created? – Aran-Fey May 16 '22 at 11:40
  • The full code saves the geometry of each window as moved and resized by the user. These geometries are used when the program is restarted to place the children windows back where the user had them, except they are all getting the 'stagger' added to their respective geometries. Just annoying, not earth shattering – Brian May 16 '22 at 11:44
  • So... you didn't even include the code where the bug is? – Aran-Fey May 16 '22 at 12:22
  • All, I appreciate your comments, but I fear that my point has been missed. In the code snippet there is one obvious mistake and thanks for pointing it out. That mistake is that the line self.defaultgeometry(300x300+30+30) should actually read self.geometry(300x300+30+30). with that changed you would expect that those child windows would all appear on top of each other. I beg you to try this. On my MAC they are created at the staggered positions as the screen shot depicts. This was what I was trying to explain and find an explanation for. – Brian May 17 '22 at 13:03

2 Answers2

0

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?

PKL
  • 95
  • 7
  • 1
    Well, now that is what you would expect and that is certainly what I have done, however as I mentioned in my comment above, that is not what is happening. The child windows are going to the nominated positions, but are getting an offset added to those setting. Where does the offset come from? Python? Tkinter? MacOS? You see the same effect on a MAC when you open images in Preview, they are staggered so you can see them. Can I add the full code here somewhere? – Brian May 16 '22 at 11:55
  • you can add a link to the repo (if you have it on github somewhere) – PKL May 16 '22 at 12:13
0

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)
        ...
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks, you are absolutely correct, but if I change that faulty geometry line to a normal one ie self.geometry(300x300+30+30) and run the code, I still get the staggered windows. I assume its a MAC OS thing and would just like to know. Please see my other comment above. – Brian May 17 '22 at 13:10
  • Ah, no sorry everyone. I'm closing the question, thanks for all your help – Brian May 17 '22 at 13:15
  • @brian: I tested this code on my Mac and it works fine. – Bryan Oakley May 17 '22 at 14:56
  • hanks for your effort. I've had to move on to another can of worms (GPS mapping) for the moment. When I get clear I'll retest the full code and maybe post again. Thanks anyway, Brian – Brian May 18 '22 at 22:03