Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 575987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:59:56+00:00 2026-05-13T13:59:56+00:00

I have a problem in Python. I’m using Tkinter and have four bind events,

  • 0

I have a problem in Python.

I’m using Tkinter and have four bind events, that listen to key presses on my form.
My problem is, that these don’t run asynchronously. So, for example I can press one button, and the events are recognized. But when I press and hold two keys at the same time, just one event gets fired.

Is there an alternative way to do this?

    self.f.bind("w", self.player1Up)
    self.f.bind("s", self.player1Down)
    self.f.bind("o", self.player2Up)
    self.f.bind("l", self.player2Down)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:59:56+00:00Added an answer on May 13, 2026 at 1:59 pm

    Unfortunately, you are somewhat at the mercy of the underlying auto-repeat mechanism of your system. For example, on the mac I’m using at the moment if I press and hold “w” I’ll get a stream of press and release events. While pressed, if I press “o” I get a stream of presses and releases for “o” but no more events for “w”.

    You will need to set up a mini state machine, and bind to both key press and key release events. This will let you track which keys are pressed and which are not. Then, each time you draw a frame you can query the machine to see which keys are pressed and act accordingly.

    Here’s a quick hack I threw together. I’ve only tested it on my mac, and only with python 2.5. I’ve made no real attempt at being “pythonic” or efficient. The code merely serves to illustrate the technique. With this code you can simultaneously press either “w” or “s” and “o” or “l” to move two paddles up and down.

    '''Example that demonstrates keeping track of multiple key events'''
    from Tkinter import *
    
    class Playfield:
        def __init__(self):
            # this dict keeps track of keys that have been pressed but not
            # released
            self.pressed = {}
    
            self._create_ui()
    
        def start(self):
            self._animate()
            self.root.mainloop()
    
        def _create_ui(self):
            self.root = Tk()
            self.p1label = Label(text="press w, s to move player 1 up, down", 
                                 anchor="w")
            self.p2label = Label(text="press o, l to move player 2 up, down", 
                                 anchor="w")
            self.canvas = Canvas(width=440, height=440)
            self.canvas.config(scrollregion=(-20, -20, 420, 420))
    
            self.p1label.pack(side="top", fill="x")
            self.p2label.pack(side="top", fill="x")
            self.canvas.pack(side="top", fill="both", expand="true")
    
            self.p1 = Paddle(self.canvas, tag="p1", color="red", x=0, y=0)
            self.p2 = Paddle(self.canvas, tag="p2", color="blue", x=400, y=0)
    
            self._set_bindings()
    
        def _animate(self):
            if self.pressed["w"]: self.p1.move_up()
            if self.pressed["s"]: self.p1.move_down()
            if self.pressed["o"]: self.p2.move_up()
            if self.pressed["l"]: self.p2.move_down()
            self.p1.redraw()
            self.p2.redraw()
            self.root.after(10, self._animate)
    
        def _set_bindings(self):
            for char in ["w","s","o", "l"]:
                self.root.bind("<KeyPress-%s>" % char, self._pressed)
                self.root.bind("<KeyRelease-%s>" % char, self._released)
                self.pressed[char] = False
    
        def _pressed(self, event):
            self.pressed[event.char] = True
    
        def _released(self, event):
            self.pressed[event.char] = False
    
    class Paddle():
        def __init__(self, canvas, tag, color="red", x=0, y=0):
            self.canvas = canvas
            self.tag = tag
            self.x = x
            self.y = y
            self.color = color
            self.redraw()
    
        def move_up(self):
            self.y = max(self.y -2, 0)
    
        def move_down(self):
            self.y = min(self.y + 2, 400)
    
        def redraw(self):
            x0 = self.x - 10
            x1 = self.x + 10
            y0 = self.y - 20
            y1 = self.y + 20
            self.canvas.delete(self.tag)
            self.canvas.create_rectangle(x0,y0,x1,y1,tags=self.tag, fill=self.color)
    
    if __name__ == "__main__":
        p = Playfield()
        p.start()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 425k
  • Answers 425k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can't do this (reliably). The popup that appears is… May 15, 2026 at 12:10 pm
  • Editorial Team
    Editorial Team added an answer If you do a view history on the MAIN trunk,… May 15, 2026 at 12:10 pm
  • Editorial Team
    Editorial Team added an answer Just use the infallible and extremely simple DOM checked property.… May 15, 2026 at 12:10 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.