Getting the absolute position of cursor in tkinter
<p>So I have a code from my supervisor that I am facing problems in understanding. I wish to draw a rectangle where my cursor is, using the <code>create_rectangle</code> method for which I give arguments/coordinates as:</p>

<p><code>rect = create_rectangle(x, y, x + 10, y + 10, fill = 'blue', width = 0)</code></p>

<p>I wish <code>x</code> and <code>y</code> here to be the current coordinates of my cursor with respect to my root window.</p>

<p>The way <code>x</code> and <code>y</code> are calculated in my code before passing them to this function is:</p>

<pre><code>x = root.winfo_pointerx() - root.winfo_rootx()
y = root.winfo_pointery() - root.winfo_rooty()
</code></pre>

<p>And I can't for the life of me understand why this has been done. I tried doing just</p>

<pre><code>x = root.winfo_pointerx()
y = root.winfo_pointery()
</code></pre>

<p>and also just</p>

<pre><code>x = root.winfo_rootx()
y = root.winfo_rooty()
</code></pre>

<p>but neither of these draw the rectangle where the cursor is. I also tried looking into the documentation but can't really understand what's happening.</p>

<p>So why is <code>x = root.winfo_pointerx() - root.winfo_rootx()</code> and <code>y = root.winfo_pointery() - root.winfo_rooty()</code> being done here?</p>