Paul Brennan
Paul Brennan

Reputation: 73

Tkinter - Coordinates

I'm making a little project from a Pi2Go robot, where it will get data from the ultrasonic sensor, then place an X if it see's a object and O where it currently is, I've got two questions: How do I set a coordinate position on tkinter? For example I wanted to insert text at 0,0, or 120,120.

Secondly: How do I get tkinter to constantly update the map I'm building Cheers!

Upvotes: 1

Views: 13426

Answers (4)

Ayu_Sh_Arma
Ayu_Sh_Arma

Reputation: 80

The best thing you can do is to use place method whit arguments of your X and Y coordinates.

label = Label(root, text = 'Placed On X and Y')
label.place(x= X,y = Y)

or another way is to use pack method which will take arguments like:

label.pack(padx = W,pady = H)

where W and H are the distances from center points to your coordinates X and Y.

Upvotes: 0

omgimdrunk
omgimdrunk

Reputation: 478

from tkinter import *
from PIL import Image, ImageTk

class buildWorld:
    def __init__(self, root):
        self.canvas = Canvas(root, width=1000, height=800)

        self.canvas.pack()
        self.tmp = Image.new('RGBA', (1000,800), color=(0,0,0) )
        self.img = ImageTk.PhotoImage(image=self.tmp)
        self.Land = self.canvas.create_image(0, 0, anchor='nw', image=self.img)

        self.tmp = Image.new('RGBA', (50, 50), color=(255, 0, 0))
        self.img1 = ImageTk.PhotoImage(image=self.tmp)
        self.mob1 = self.canvas.create_image(125, 125, anchor='nw', image=self.img1)


        self.tmp = Image.new('RGBA', (50, 50), color=(0, 255, 0))
        self.img2 = ImageTk.PhotoImage(image=self.tmp)
        self.mob2 = self.canvas.create_image(300, 300, anchor='nw', image=self.img2)

root = Tk()
world = buildWorld(root)
mainloop()

Upvotes: 0

yo halbe
yo halbe

Reputation: 33

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)

Upvotes: 1

albert
albert

Reputation: 8583

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.

Upvotes: 2

Related Questions