如何将文本位置与 Canvas 上随机放置的文本相匹配?
How do I match text location to randomly placed text on my Canvas?
我有这个功能:
StoreItems = random.sample(set(['sword','pickaxe','toothpick','hammer','torch','saw']), 5)
#Selects 5 Random strings from the list. ^
XBASE, YBASE, DISTANCE = 300, 320, 50
for i, word in enumerate(StoreItems):
canvas.create_text(
(XBASE, YBASE + i * DISTANCE),
text=word, activefill="Medium Turquoise", anchor=W, fill="White", font=('Anarchistic',40), tags=word)
canvas.tag_bind('sword', '<ButtonPress-1>', BuySword)
canvas.tag_bind('pickaxe', '<ButtonPress-1>', BuyPick)
canvas.tag_bind('toothpick', '<ButtonPress-1>', BuyTooth)
canvas.tag_bind('hammer', '<ButtonPress-1>', BuyHammer)
canvas.tag_bind('torch', '<ButtonPress-1>', BuyTorch)
canvas.tag_bind('saw', '<ButtonPress-1>', BuySaw)
从列表中随机选择:StoreItems[] 并以随机顺序将其中的 5 个放在我的 canvas 上。如果我希望我的标签绑定在它们旁边创建文本,我该怎么做?
我有事件函数:
def BuySword(event):
if 'sword' in StoreItems:
sword = canvas.create_text((420,350), text="test", fill="White", font=('Anarchistic',40))
但我希望这个创建的文本的位置遵循我列表中相应单词的随机位置。
您可以使用 bbox
method 获取 tagOrId 的位置:
Returns a tuple (x1, y1, x2, y2) describing a rectangle that encloses all the objects specified by tagOrId. If the argument is omitted, returns a rectangle enclosing all objects on the canvas. The top left corner of the rectangle is (x1, y1) and the bottom right corner is (x2, y2).
虽然因为你做的是相对位置,所以你只需要前两个:
def BuySword(event):
if 'sword' in StoreItems:
x,y,_,_ = canvas.bbox("sword")
relative_position = (x+420,y)
sword = canvas.create_text(relative_position, text="test", fill="White", font=('Anarchistic',40),
anchor=N+W) #the anchor is needed to line up with the north west corner of the original text
我有这个功能:
StoreItems = random.sample(set(['sword','pickaxe','toothpick','hammer','torch','saw']), 5)
#Selects 5 Random strings from the list. ^
XBASE, YBASE, DISTANCE = 300, 320, 50
for i, word in enumerate(StoreItems):
canvas.create_text(
(XBASE, YBASE + i * DISTANCE),
text=word, activefill="Medium Turquoise", anchor=W, fill="White", font=('Anarchistic',40), tags=word)
canvas.tag_bind('sword', '<ButtonPress-1>', BuySword)
canvas.tag_bind('pickaxe', '<ButtonPress-1>', BuyPick)
canvas.tag_bind('toothpick', '<ButtonPress-1>', BuyTooth)
canvas.tag_bind('hammer', '<ButtonPress-1>', BuyHammer)
canvas.tag_bind('torch', '<ButtonPress-1>', BuyTorch)
canvas.tag_bind('saw', '<ButtonPress-1>', BuySaw)
从列表中随机选择:StoreItems[] 并以随机顺序将其中的 5 个放在我的 canvas 上。如果我希望我的标签绑定在它们旁边创建文本,我该怎么做?
我有事件函数:
def BuySword(event):
if 'sword' in StoreItems:
sword = canvas.create_text((420,350), text="test", fill="White", font=('Anarchistic',40))
但我希望这个创建的文本的位置遵循我列表中相应单词的随机位置。
您可以使用 bbox
method 获取 tagOrId 的位置:
Returns a tuple (x1, y1, x2, y2) describing a rectangle that encloses all the objects specified by tagOrId. If the argument is omitted, returns a rectangle enclosing all objects on the canvas. The top left corner of the rectangle is (x1, y1) and the bottom right corner is (x2, y2).
虽然因为你做的是相对位置,所以你只需要前两个:
def BuySword(event):
if 'sword' in StoreItems:
x,y,_,_ = canvas.bbox("sword")
relative_position = (x+420,y)
sword = canvas.create_text(relative_position, text="test", fill="White", font=('Anarchistic',40),
anchor=N+W) #the anchor is needed to line up with the north west corner of the original text