My aim is to load a new "arrow" image every time the user presses the shift button. I thought, that I could make a class for the arrow so I can create a new instance for them where each instance has their own coordinates and so on. But obviously I did that wrong. I tried to make a list for the instances so I can iterate of it and then use the moveArrow method in each instance. I just got 5 entries because I want a limit of 5 arrows at the time. I hope this code doesn't give you eyecancer or something like that :P I am a newb and I am sorry, if the code is really bad or the approach on how to handle my problem. Thank you for your help :)
#!/usr/bin/python
from Tkinter import *
from PIL import ImageTk, Image
class frameApp(Frame):
def __init__(self, master=None):
Frame.__init__(self, master, height = 400, width = 400)
self.charImg = ImageTk.PhotoImage(Image.open("Archer.gif"))
self.arrowImage = ImageTk.PhotoImage(Image.open("arrow.gif"))
self.charLabel = Label(self, image = self.charImg) #Loading character label
self.charLabel.pack()
self.arrow = Label(self, image = self.arrowImage)
self.arrow.pack()
self.shift = False
self.down = False
self.right = False
self.left = False
self.up = False
self.x_coord = 200
self.y_coord = 200
self.pack_propagate(0)
self.pack()
self.arrowList = ["Inst1", "Inst2", "Inst3", "Inst4", "Inst5"]
self.counter = -1
def moveableImage(self):
self.charLabel.place(y=self.y_coord, x=self.x_coord)
def createArrow(self):
self.arrowList[counter] = arrow()
def moveArrow(self):
for arrowList in self.arrowList:
arrowList.moveArrow
def keyPressed(self, event):
if event.keysym == 'Down':
self.down = True
elif event.keysym == 'Right':
self.right = True
elif event.keysym == 'Left':
self.left = True
elif event.keysym == 'Up':
self.up = True
elif event.keysym == 'Shift_L':
self.shift = True
def keyReleased(self, event):
if event.keysym == 'Down':
self.down = False
elif event.keysym == 'Right':
self.right = False
elif event.keysym == 'Left':
self.left = False
elif event.keysym == 'Up':
self.up = False
elif event.keysym == 'Shift_L':
self.shift = False
def task(self):
if self.down and self.y_coord < 360:
self.y_coord = self.y_coord + 10
elif self.right and self.x_coord < 370:
self.x_coord = self.x_coord + 10
elif self.left and self.x_coord > 10:
self.x_coord = self.x_coord - 10
elif self.up and self.y_coord > 10:
self.y_coord = self.y_coord - 10
elif self.shift:
self.counter += 1
self.createArrow()
root.after(20,self.task)
root.after(20,self.moveArrow)
self.moveableImage()
class arrow(object):
def __init__(self):
self.x_coordArrow = app.x_coord
self.y_coordArrow = app.y_coord
def moveArrow(self):
self.x_coordArrow = self.x_coordArrow + 20
root = Tk()
root.title("Frametitel")
app = frameApp(master=root)
root.bind_all('<Key>', app.keyPressed)
root.bind_all('<KeyRelease>', app.keyReleased)
root.after(20, app.task)
app.mainloop()