0

I try to build a gui with tkinter to put words on a map for games like League of Legends etc. It's amazing how fast you can create such a gui with tkinter but I run into a problem that I can't solve alone as it seems:

Function for the click event (Create word picture on Canvas/map):

def sightstone(event):
 i = Image.open('Sightstone_item.png')
 i = i.resize((10, 10), Image.ANTIALIAS)
 img = ImageTk.PhotoImage(i)
 sword = tk.Label(panel, image=img, compound=tk.CENTER)
 sword.place(x=event.x, y=event.y, width=10, height=10)
 sword.image = img
 n = len(words) + 1
 string = 'sword '
 words.update({string + '%d' % n: [(event.x, event.y)]})
 dump.update({string + '%d' % n: [sword, img, i]})
 print('clicked at', event.x, event.y)

Rest to set it up:

window = tk.Tk()
Canvas = tk.Canvas(window, width=768, height=810)
img = Image.open('srift.jpg')
img = img.resize((768, 810), Image.ANTIALIAS)
mapimg = ImageTk.PhotoImage(img)
panel = tk.Label(window, image=mapimg)
panel.bind("<Control-Button-1>", sightstone)
panel.place(x=0, y=0, relwidth=1, relheight=1)
i = Image.open('Sightstone_item.png')
i = i.resize((10, 10), Image.ANTIALIAS)
img = ImageTk.PhotoImage(i)
sword1 = tk.Label(panel, image=img, compound=tk.CENTER)
sword1.place(x=384, y=405, width=10, height=10)
sword1.image = img
sword2 = tk.Label(panel, image=img, compound=tk.CENTER)
sword2.place(x=405, y=405, width=10, height=10)
sword2.image = img
Canvas.pack()
words = {}; dump = {}; string = ''
Canvas.find_all()
window.mainloop()

Note that I create sword1 and 2 only to show how I want it to be. But if I ctrl+left click to create another word sword1 and 2 disappear. It's very important that I get the event.x and event.y or coordinates of the words as I want to do some stuff with it.

martineau
  • 119,623
  • 25
  • 170
  • 301
TheDude
  • 55
  • 1
  • 9

1 Answers1

1

Credits going to joaquin who answered on here! What I changed is: I created a Canvas and created the background image on the canvas instead of generating a label.

#in sightstone(event) change following:
sward = tk.Label(Canvas, image=img, compound=tk.CENTER)
#then in main:
Canvas.create_image(384, 405, image=mapimg)
Canvas.bind("<Control-Button-1>", sightstone)
Canvas.pack(expand = 'yes', fill = 'both')

Now it's working all fine. =^)

TheDude
  • 55
  • 1
  • 9