Placing child window relative to parent in Tkinter python
Asked Answered
D

3

8

I have a parent widget which contains a button. When the button is pressed I would like to open a borderless (i.e. no Windows decoration buttons) window directly underneath the parent widget aligned to the left hand side of it. I'm puzzled that the only way (it seems) of setting the position of a window is using .geometry() but worse, I can't seem to get the absolute coordinates of the parent widget - which I need for .geometry(), only the offsets from the parent's parent. So far my code is:

# This is the child which appears when the button is pressed.
class ChildPopUpWindow(Frame):
    def __init__(self, parentWgdt):
        win = Toplevel(parentWgdt)
        geom = str(parentWgdt.winfo_x()) + '+' + str(parentWgdt.winfo_y() + parentWgdt.winfo_height())
        win.overrideredirect(1) # No win decoration.
        win.bd = 10
        win.relief = GROOVE
        win.geometry( geom )
        Frame.__init__(self, win)
        # etc. etc.

     # ... and this is the handler for the button being pressed.
     def onDropDown(self):
        popUp = ChildPopUpWindow(self)

This does pop up a window but relative to the desktop, not to the parent widget. It also seems to take no account of the border thickness and relief as far as I can see. Can anyone offer a way that this can be done? Is .geometry() the way to go or are there better ways?

Doggett answered 22/6, 2012 at 13:5 Comment(3)
You say you want to create a popup window, and your class name says PopUpWindow, yet you inherit from Frame rather than Toplevel. Is there a reason for that? I don't quite understand how you're using this. Is the button to show the popup window inside the ChildPopUpWindow frame?Kamin
Sorry this wasn't very clear: onDropDown is a handler from the frame in which the button resides. On clicking the button, the idea is to pop up a frameless (i.e. no Windows maximise / minimise / close buttons etc.) directly underneath the frame with the button. This would act essentially in a modal way except that either a selection on the pop-up, or pressing the button again, would close it. In this way it would work a bit like say a calendar or colour picker dropdown.Doggett
@BryanOakley: The Toplevel / Frame confusion is due to me making numerous different attempts to get this to work, I'm afraid. Most examples I've found seem to create the Toplevel window and then pass this as a parameter to a Frame-inhereting class. I wanted to follow this and tried to set the parameters of the Toplevel window prior to the Frame using them in it's constructor though I wanted to keep these encapsulated in the ChildPopUpWindow class. Please feel ffree to comment / correct.Doggett
K
9

The short answer is, use winfo_rootx and winfo_rooty to get the coordinates relative to the screen. And yes, wm_geometry is the way to place a toplevel window precisely.

For example:

    x = parentWgdt.winfo_rootx()
    y = parentWgdt.winfo_rooty()
    height = parentWgdt.winfo_height()
    geom = "+%d+%d" % (x,y+height)

As a bit of friendly advice, I recommend against abbrev var nms. It makes the code hard to read, especially when the abbreviation is wrong (Wgdt should at least be Wdgt). The difference in code size between geom and geometry, and Wgdt and Widget are tiny, but the difference in readability is huge.

Kamin answered 22/6, 2012 at 18:57 Comment(1)
Thanks for the confirmation. That looks to be essentially what I've got so I'm guessing there must be some other reason that it's not working. Agree on abbreviations: I obfuscated the code (which is client IP) in a hurry before posting.Doggett
P
0

According to Tk manual "https://www.tcl.tk/man/tcl8.4/TkCmd/winfo.htm#M52" If you need the true width immediately after creating a widget, invoke update to force the geometry manager to arrange it, or use winfo reqwidth to get the window's requested width instead of its actual width.

# This code works perfectly
self.update()

self.geometry("+%d+%d" % (self.parent.winfo_rootx()+50,
                          self.parent.winfo_rooty()+50
                         )
             ) 
Peltier answered 8/12, 2016 at 1:20 Comment(0)
P
0

to centring a modal window about a its parent window, I do so:

    alto_modal = 100
    ancho_modal = 250
    alto_parent = parent.winfo_height()
    ancho_parent = parent.winfo_width()
    x = (ancho_parent - ancho_modal) // 2
    y = (alto_parent - alto_modal) // 2
    self.geometry('{}x{}+{}+{}'.format(ancho_modal, alto_modal, x, y))
Planimetry answered 23/9, 2020 at 21:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.