python 3.x - How do I match text location to randomly placed text on my Canvas? -


i have function:

storeitems = random.sample(set(['sword','pickaxe','toothpick','hammer','torch','saw']), 5) #selects 5 random strings list. ^  xbase, ybase, distance = 300, 320, 50 i, word in enumerate(storeitems):       canvas.create_text(         (xbase, ybase + * 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) 

which randomly selects list: storeitems[] , puts 5 of them on canvas in random order. if wanted tag binds create text next them how that?

i have event functions:

def buysword(event): if 'sword' in storeitems:     sword = canvas.create_text((420,350), text="test", fill="white", font=('anarchistic',40)) 

but want location of created text follow random placement of corresponding word list.

you can use bbox method position of tagorid:

returns tuple (x1, y1, x2, y2) describing rectangle encloses objects specified tagorid. if argument omitted, returns rectangle enclosing objects on canvas. top left corner of rectangle (x1, y1) , bottom right corner (x2, y2).

although since doing relative position need first two:

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 needed line north west corner of original text 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -