Home > Enterprise >  Is it possible to disable the spacebar in Tkinter?
Is it possible to disable the spacebar in Tkinter?

Time:01-01

I'm working on a little game with Tkinter, but I'm facing a issue and I dont know how to fix it The issue is that sometime when I press a button in the game (like the right arrow to move right for example), the function corresponding to the button is working well, but when I press spacebar just after, it calls the function again which is something I dont want.

So I'm wondering if there is any way to completly disable the space bar for tkinter ?

I've already tried 2 things:

  1. Bind the spacebar to another function which is doing nothing, but when I press the spacebar it is still repeating the last button pressed

  2. Unbind the space bar at the start of the code like this:

import tkinter as tk

game = tk.Tk()

game.unbind('<space>')

If you want, here is the full code of the game: https://github.com/Nirs123/World_Of_Boats

Appreciate every feedback :)

CodePudding user response:

Here is an answer of what I said in my comment.

You can use game.unbind_class() to unbind a specific event for a specific type of widget. To unbind the spacebar from a button, you can use game.unbind_class("Button", "<Key-space>"). You can call it right after you create game, and all the buttons in the whole window will no longer be able to be pressed with the spacebar.

  • Related