Use of multiple keys in Tkinter

Asked

Viewed 207 times

4

Okay, I edited the question, this is the most simplified example, I wish that while the '6' key is being pressed, even using the '8' key, it continues to interact with the program, while '8' only executes one command and does not interrupt the other.

#
from Tkinter import Tk, Canvas, Frame
Tela_principal = Tk()
Tela_principal.geometry('1024x720+10+10')
def aplicacao():
    area_1.place(x=0, y=0), area_2.place(x=3, y=150)
    fundo_area2.place(x=10, y=10)
    persona()
def execucao():
    Tela_principal.bind("<KeyPress-8>", controles)
    Tela_principal.bind("<KeyPress-5>", controles)
    Tela_principal.bind("<KeyPress-6>", controles)
    Tela_principal.bind("<KeyPress-4>", controles)
    fundo_area2.delete('personagem')
    persona()
    area_2.after(FPS, execucao)
def persona():
    boneco_tronco_Lateral = fundo_area2.create_polygon(
        [(2 + x, 25 + y), (20 + x, 25 + y), (20 + x, 152 + y), (2 + x, 152 + y)],
        fill=pele, outline='black', tag='personagem')
def controles(event):
    global x, y, velocidade_x, velocidade_y
    if event.char == '6':
        x += velocidade_x
        fundo_area2.move('personagem', velocidade_x, 0)
    if event.char == '4':
        x -= velocidade_x
        fundo_area2.move('personagem', -velocidade_x, 0)
    if event.char == '8' and '6':
        y -= velocidade_y
        x += velocidade_x
        fundo_area2.move('personagem', -velocidade_x, 0)
area_1 = Frame(bg='snow', height=100, width=780, cursor='cross')
fundo_area1 = Canvas(area_1, bg='plum4', height=43, width=620)
area_2 = Frame(bg='snow', height=500, width=1016, cursor='dotbox')
fundo_area2 = Canvas(area_2, bg='gray', height=642, width=973)
x = 0
y = 100
pele = 'tomato'
velocidade_x = 0.5
velocidade_y = 0.5
FPS = 5
cont_salto = 0
aplicacao(), execucao()
Tela_principal.mainloop()
###########Thanks!!
  • Could you make a minimum replicable example? See instructions here: https://answall.com/help/minimal-reproducible-example

  • 1

    Sure, I’ll post a new, more simplified example.

2 answers

0

You can use a Thread to perform both functions unchangingly. Example:

from threading import Thread

thread1 = Thread(target=func1)
thread1.start()

thread2 = Thread(target=func2)
thread2.start()

0

You can create a set with all the keys pressed.

from tkinter import Tk, Canvas, Frame
Tela_principal = Tk()
Tela_principal.geometry('1024x720+10+10')

keys = set()

def keyPressHandler(event):
    keys.add(event.char)
    controles()

def keyReleaseHandler(event):
    keys.remove(event.char)
    controles()

def aplicacao():
    area_1.place(x=0, y=0), area_2.place(x=3, y=150)
    fundo_area2.place(x=10, y=10)
    persona()
def execucao():
    Tela_principal.bind_all('<KeyPress>', keyPressHandler)
    Tela_principal.bind_all('<KeyRelease>', keyReleaseHandler)
    fundo_area2.delete('personagem')
    persona()
    area_2.after(FPS, execucao)
def persona():
    boneco_tronco_Lateral = fundo_area2.create_polygon(
        [(2 + x, 25 + y), (20 + x, 25 + y), (20 + x, 152 + y), (2 + x, 152 + y)],
        fill=pele, outline='black', tag='personagem')
def controles():
    global x, y, velocidade_x, velocidade_y
    if '8' in keys and '6' in keys:
        y -= velocidade_y
        x += velocidade_x
        fundo_area2.move('personagem', -velocidade_x, 0)
    elif '6' in keys:
        x += velocidade_x
        fundo_area2.move('personagem', velocidade_x, 0)
    elif '4' in keys:
        x -= velocidade_x
        fundo_area2.move('personagem', -velocidade_x, 0)
    elif '8' in keys:
        y -= velocidade_y
        fundo_area2.move('personagem', -velocidade_x, 0)

area_1 = Frame(bg='snow', height=100, width=780, cursor='cross')
fundo_area1 = Canvas(area_1, bg='plum4', height=43, width=620)
area_2 = Frame(bg='snow', height=500, width=1016, cursor='dotbox')
fundo_area2 = Canvas(area_2, bg='gray', height=642, width=973)
x = 0
y = 100
pele = 'tomato'
velocidade_x = 0.5
velocidade_y = 0.5
FPS = 5
cont_salto = 0
aplicacao(), execucao()
Tela_principal.mainloop()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.