I am working on my gui with tkinter, and I have to place stuff in precise places, i can't just use pack. Currently i write almost random numbers for width
and height
in place()
, then run, adjust the numbers, repeat the program and check the position until the button or label fits just perfectly.
Isn't there a simpler way? Is there an option of the python launcher that allows me to point somewhere in the window and find out the coordinates? Or maybe an extention for vcscode or a setting for pycharm?
This is about the simplest way of getting mouse coordinates
from tkinter import Tk
root = Tk()
root.bind('<Button-1>', lambda e: print(e.x, e.y))
root.mainloop()
basically .bind()
binds an event (as the name suggests) to the widget and when the event is triggered .bind()
method calls the given function while also passing an event
argument so that has to be dealt with but it can also be used as in this case (and in most cases actually)
P.S. e
is just a preference of mine when using lambda
in this case and if I were to define a function as usually I would use event
as argument name