Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tkinter window get x, y, geometry/coordinates without top of window

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?

like image 585
gnr Avatar asked Nov 16 '12 21:11

gnr


People also ask

What is Winfo_x () in tkinter?

DESCRIPTION. The winfo() method is used to retrieve information about windows managed by Tkinter.

What does Tk () do in tkinter?

Tkinter is a Python package which comes with many functions and methods that can be used to create an application. In order to create a tkinter application, we generally create an instance of tkinter frame, i.e., Tk(). It helps to display the root window and manages all the other components of the tkinter application.

What is the difference between tkinter window and frame?

Tk creates the root window. Every tkinter application must have a root window. When you instantiate it you also create a tcl interpreter that is used by tkinter. Frame is just a widget, designed to be a container for other widgets.


1 Answers

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.

like image 101
FabienAndre Avatar answered Oct 29 '22 16:10

FabienAndre