Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button() does not activate on spacebar #32

Closed
jpodolski opened this issue Oct 4, 2021 · 2 comments
Closed

Button() does not activate on spacebar #32

jpodolski opened this issue Oct 4, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@jpodolski
Copy link

jpodolski commented Oct 4, 2021

I have not been able to active a Button() element using the spacebar when it has focus (as in takefocus). I have tried this on OSX Catalina 10.15.7 and Windows 10, and I have confirmation that at least one other user is experiencing this issue as part of a question I asked on StackOverflow

Here is a code example that should illustrate the problem. When spacebar is pressed while a Button() has focus, the associated command does not trigger. The associated command triggers fine for both buttons when clicked using the mouse.

from tkinter import Tk
from tkmacosx import Button

root = Tk()

def one():
	print("one")
def two():
	print("two")

B1 = Button(root, text='Button One', command=one)
B1.pack(padx=20, pady=(20, 10))
B2 = Button(root, text='Button Two', command=two)
B2.pack(padx=20, pady=10)

root.mainloop()

(While takefocus is enabled by default, spacebar still doesn't work with takefocus=True or takefocus=1 when initializing a Button())

Python 3.9.7
OSX Catalina 10.15.7
Tcl/Tk: 8.6.11
tkmacosx 1.0.3

Love the work you're doing here by the way, hope to be using this project moving forward.

@jpodolski jpodolski added the bug Something isn't working label Oct 4, 2021
@Saadmairaj
Copy link
Owner

Hi
Thanks for mentioning the bug, I'll fix it with the next update.

Till then if this really stopping you from completing your project then you use the following hotfix till the next update is released officially.

from tkinter import Tk
from tkmacosx import Button


class Button(Button):
    def __init__(self, master=None, cnf=..., **kw):
        super().__init__(master=master, cnf=cnf, **kw)
        self.bind_class(self, "<space>", self._on_spacebar_press)
        self.bind_class(self, "<KeyRelease>", self._on_release)

    def _on_spacebar_press(self, evt):
        """Hotfix event callback function for spacebar key"""
        self._active("on_press")
        return self.invoke()


root = Tk()

B1 = Button(root, text='Button One', command=lambda: print("one"))
B2 = Button(root, text='Button Two', command=lambda: print("two"))

B1.pack(padx=20, pady=(20, 10))
B2.pack(padx=20, pady=10)

root.mainloop()

This hotfix is not tested but should not break anything. The final fix will be slightly different and more stable.

@jpodolski
Copy link
Author

Hotfix works perfectly! Thanks so much for the quick response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants