I just cobbled some code together to give you a short introduction in how to use the place geometry manager. For further explanation please see comments in code:
#!/usr/bin/env python3
# coding: utf-8
from tkinter import *
# init our application's root window
root = Tk()
root.geometry('480x480')
# let's provide same sample coordinates with the desired text as tuples in a list
coords = [(30,30,'X'), (90,90,'X'), (120,120,'X'), (240,240,'X'), (270,270,'X'), (360,360,'O')]
# interate through the coords list and read the coordinates and the text of each tuple
for c in coords:
l = Label(root, text=c[2])
l.place(x=c[0], y=c[1])
# start a loop over the application
root.mainloop()
I am using Python 3. If you are using Python 2, you need to change tkinter to Tkinter. That should do the trick if you need to port my code to Python 2.
I just cobbled some code together to give you a short introduction in how to use the place geometry manager. For further explanation please see comments in code:
#!/usr/bin/env python3
# coding: utf-8
from tkinter import *
# init our application's root window
root = Tk()
root.geometry('480x480')
# let's provide same sample coordinates with the desired text as tuples in a list
coords = [(30,30,'X'), (90,90,'X'), (120,120,'X'), (240,240,'X'), (270,270,'X'), (360,360,'O')]
# interate through the coords list and read the coordinates and the text of each tuple
for c in coords:
l = Label(root, text=c[2])
l.place(x=c[0], y=c[1])
# start a loop over the application
root.mainloop()
I am using Python 3. If you are using Python 2, you need to change tkinter to Tkinter. That should do the trick if you need to port my code to Python 2.
use the .place function.
like in the following
label = Label(root, text = 'i am placed')
#places the label in the following x and y coordinates
label.place(20,20)
python - tkinter window get x, y, geometry/coordinates without top of window - Stack Overflow
python - How to get the widget's current x and y coordinates? - Stack Overflow
python - Tkinter: How to determine (x,y) coordinates to place multiple frames on canvas? - Stack Overflow
Python Tkinter how to adjust the x,y default 0,0 coordinates - Stack Overflow
winfo_rootx() and winfo_rooty() return the coordinates relative to the screen's upper left corner. winfo_x and winfo_y return the coordinates of a window relative to its parent.
You can try winfo_rootx() and winfo_rooty().
From effbot.com:
winfo_rootx()
Get the pixel coordinate for the widgetโs left edge, relative to the screenโs upper left corner.
Returns: The root coordinate.
winfo_rooty()
Get the pixel coordinates for the widgetโs upper edge, relative to the screenโs upper left corner.
Returns: The root coordinate.
An example from nullege.com:
# get self position & height
lv_x = self.winfo_rootx()
lv_y = self.winfo_rooty()
I have no idea how cartesian coordinates work to be honest, I don't remember doing them in school either. I've been trying to draw a line that goes at the bottom of a tkinter screen for quite a while now, this is the closest I've got.
from tkinter import * window = Tk() canvas1 = Canvas(window, width = 1000, height = 600, bg='white') canvas1.pack() line1 = canvas1.create_line(485, 601, 430, 601, fill='green', width = 2) line1.pack()
The problem is I have no idea how I got there and it's just going to be hard to create lines with different positions later on. Could anyone explain it a little to me please? I just want to be able to draw efficiently.
I have some mixed inputs that Im arranging with a grid so Id like to access a widget by its coordinate position. Is there a way of targeting a location and a probing what widget it is?
The method place_info will return all of the information used by place for that widget.
>>> print(label_0.place_info())
{'in': <tkinter.Tk object .>, 'x': '10', 'relx': '0', 'y': '15', 'rely': '0', 'width': '', 'relwidth': '', 'height': '', 'relheight': '', 'anchor': 'nw', 'bordermode': 'inside'}
If you just want the x/y coordinate no matter how it was added to the window, you can use the winfo_x() and winfo_y() methods.
This should give you the x or y coordinates:
.place_info().get("y")
or:
.place_info().get("x")