Tkinter坐标

2024-06-08 11:05:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个Pi2Go机器人的小项目,它将从超声波传感器中获取数据,然后在它看到的是物体时,在它当前所在的位置画一个X,我有两个问题: 如何在tkinter上设置坐标位置?例如,我想在0、0或120120处插入文本。在

其次: 我如何让tkinter不断更新我正在构建的地图 干杯!在


Tags: 项目文本tkinter地图机器人传感器物体超声波
3条回答

我只是拼凑了一些代码来简要介绍一下如何使用place几何管理器。如需进一步解释,请参阅代码中的注释:

#!/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()

我使用的是python3。如果您使用的是python2,则需要将tkinter更改为Tkinter。如果您需要将我的代码移植到python2,那么这应该可以实现。在

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()

使用.place函数。在

像下面这样

label = Label(root, text = 'i am placed')
#places the label in the following x and y coordinates
label.place(20,20)

相关问题 更多 >