samedi 24 juillet 2021

Is their a more effective way to Key bind four different letters in Tkinter?

The main problem is when I press two different keys lets say "w" and "o" the two objects stop moving all together. Do I need to define something that will allow for button pressing at the same time or is there a way better way to write this entirely?

def pressing(event):
    x = 0
    y = 0
    if event.char == "w":      #controls first rectangle, upward movement
        y = -50
    if canvas.coords(rectangle)[1] <= 0:   #boundary control, border is a 0, hits it sends it back 50
        y = 50
    if event.char == "s":          #controls first rectangle downward movement
        y = 50
    if canvas.coords(rectangle)[3] >= hh:  #hh is just the border limit of the canvas. 
        y = -50
    canvas.move(rectangle, x, y)

Same comments from the top apply to the bottom segment of code

    x = 0
    y = 0
    if event.char == "o":     
        y = -50
    if canvas.coords(rectangle2)[1] <= 0:
        y = 50
    if event.char == "k":
        y = 50
    if canvas.coords(rectangle2)[3] >= hh:
        y = -50
    canvas.move(rectangle2, x, y)


top.bind("<Key>", pressing)```

Aucun commentaire:

Enregistrer un commentaire