9

I am using tk in python (3), though I'd assume that this applies to any language. I am looking to get the current x, y coordinates of a tk window outside of the title bar:

import tkinter  
root = tkinter.Tk()

however, using root.winfo_y() gives me the coordinates including the depth of the titlebar. For a window that is in the upper left corner of my screen:

root.winfo_x(), root.winfo_y()          # returns (0, 22)

In other words, running:

root.geometry('+{}+{}'.format(root.winfo_x(), root.winfo_y()))

will move down the window by 22 pixels (the height of the title bar) every time I call it. How can I get the actual coordinates of the entire window?

martineau
  • 119,623
  • 25
  • 170
  • 301
gnr
  • 2,324
  • 1
  • 22
  • 24

1 Answers1

5

In Tkinter, most configuration functions when used without new value arguments returns the current value(s). Thus root.geometry() (plus a few string parsing) can serve your goal.

martineau
  • 119,623
  • 25
  • 170
  • 301
FabienAndre
  • 4,514
  • 25
  • 38
  • 1
    yep this is what I was looking for! As a side note, `root.winfo_x(), root.winfo_y()` i think normally should return the same coordinates as `root.geometry()`, however, the problem I am having is that i use 2 monitors, running OSX, and the one on the right is my 'home' monitor with the file bar that sits at the very top of the screen. Using `root.geometry()` solves my issues, though, thanks! – gnr Nov 17 '12 at 12:38
  • It still works, thanks :-) I got expected values with this code: x,y = (int(s) for s in self.geometry().split("+")[1:]) – ffsedd Aug 25 '21 at 15:19