[英]Tkinter - Coordinates
我正在用Pi2Go机器人做一个小项目,它将从超声波传感器获取数据,然后在看到物体的情况下放置一个X,在当前位置放置一个O,我有两个问题:如何设置在tkinter上的坐标位置? 例如,我想在0,0或120,120处插入文本。
其次:如何让tkinter不断更新我正在建立的Cheers地图!
我只是将一些代码拼凑在一起,为您简要介绍了如何使用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。如果您正在使用Python 2,则需要将tkinter
更改为Tkinter
。 如果您需要将我的代码移植到Python 2,那应该可以解决问题。
使用.place函数。
像下面
label = Label(root, text = 'i am placed')
#places the label in the following x and y coordinates
label.place(20,20)
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()
最好的办法是使用X和Y坐标的place
方法whit参数。
label = Label(root, text = 'Placed On X and Y')
label.place(x= X,y = Y)
或另一种方法是使用pack
方法,该方法将接受类似以下内容的参数:
label.pack(padx = W,pady = H)
其中W和H是从中心点到坐标X和Y的距离。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.