我应该使用哪些 Tkinter 函数来查找随机放置(在 canvas 中)文本条目的坐标以及如何查找?
Which Tkinter functions should i use to find the coordinates of a randomly placed(in the canvas) text entry and how?
感谢您调查我的问题。我会尽量给你一张大图和小图,说明我在这里要做的事情。
大图:
所以,基本上我正在尝试制作一个简单的思维导图程序,在第一个条目之后,我输入到条目小部件的每个文本都通过行小部件 linked 到前一个文本。所以像这样:你好----那里,然后你好----那里----哟。实际上,我希望将来有更多的修改,比如能够通过一些我尚未探索过的指标重新排列 links,但基本上就是这样。
Small/Specific 图片:
我意识到,为了做到这一点,我必须找到一种方法来获取 canvas 上绘制的每个文本的所有 xy 坐标(我使用随机函数在 canvas 上绘制的文本) .我需要第一个文本的坐标和第二个文本的坐标,这样我就可以使用它们来画线以直观地 link 这两个文本。我想用一个数组来列出所有输入的文本,但我意识到它只存储文本而不是文本在 canvas 上的位置。我探索过使用标签、使用坐标函数或使用 bbox 函数,但都无济于事。关于如何解决这个问题的任何线索?我将不胜感激,谢谢。 :)
import Tkinter
import random
a = Tkinter.Tk()
b = Tkinter.Canvas(a, width=1000, height=500)
b.pack()
def c(event):
b.create_text(random.randint(50,940), random.randint(50,480), anchor="center", text=d.get())
f.append(d.get())
d.delete(0, 'end')
print f
#this function creates a randomly located text taken from the entry widget below and, at the same time, appends the text in the list known as f''
d = Tkinter.Entry(a)
d.pack()
d.bind("<Return>", c)
d.focus()
b.create_line(300, 250, 600, 290)
#this is my very early attempt at linking text inputted and drawn on the Canvas
f = []
a.mainloop()
在使用它在 canvas 上创建文本之前,只需将随机值分配给变量,并在列表中保留对象 ID 和文本。
x = random.randint(...)
y = random.randint(...)
obj_id = b.create_text(x, y, ...)
f.append([x, y, obj_id, d.get()])
顺便说一句: 如果你有 obj_id
那么你也可以
x,y = b.coords(obj_id)
感谢您调查我的问题。我会尽量给你一张大图和小图,说明我在这里要做的事情。
大图: 所以,基本上我正在尝试制作一个简单的思维导图程序,在第一个条目之后,我输入到条目小部件的每个文本都通过行小部件 linked 到前一个文本。所以像这样:你好----那里,然后你好----那里----哟。实际上,我希望将来有更多的修改,比如能够通过一些我尚未探索过的指标重新排列 links,但基本上就是这样。
Small/Specific 图片: 我意识到,为了做到这一点,我必须找到一种方法来获取 canvas 上绘制的每个文本的所有 xy 坐标(我使用随机函数在 canvas 上绘制的文本) .我需要第一个文本的坐标和第二个文本的坐标,这样我就可以使用它们来画线以直观地 link 这两个文本。我想用一个数组来列出所有输入的文本,但我意识到它只存储文本而不是文本在 canvas 上的位置。我探索过使用标签、使用坐标函数或使用 bbox 函数,但都无济于事。关于如何解决这个问题的任何线索?我将不胜感激,谢谢。 :)
import Tkinter
import random
a = Tkinter.Tk()
b = Tkinter.Canvas(a, width=1000, height=500)
b.pack()
def c(event):
b.create_text(random.randint(50,940), random.randint(50,480), anchor="center", text=d.get())
f.append(d.get())
d.delete(0, 'end')
print f
#this function creates a randomly located text taken from the entry widget below and, at the same time, appends the text in the list known as f''
d = Tkinter.Entry(a)
d.pack()
d.bind("<Return>", c)
d.focus()
b.create_line(300, 250, 600, 290)
#this is my very early attempt at linking text inputted and drawn on the Canvas
f = []
a.mainloop()
在使用它在 canvas 上创建文本之前,只需将随机值分配给变量,并在列表中保留对象 ID 和文本。
x = random.randint(...)
y = random.randint(...)
obj_id = b.create_text(x, y, ...)
f.append([x, y, obj_id, d.get()])
顺便说一句: 如果你有 obj_id
那么你也可以
x,y = b.coords(obj_id)