Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding buttons to Alt keypresses?

Tags:

tkinter

I've got a simple Tkinter application I've written, with a few buttons at the bottom of a form. My goal is to follow the standard convention of underlining a letter on the button, and binding an action for that letter with the Alt key (ie: Alt-s for "_S_ave").

I've tried making a root window binding to "Alt-s", "Alt-KeyPress-s", and "Mod1-s", and none seem to work reliably. They sometimes fire, but even though I have "return break" on my event function the "s" letter is propagating to the entry widget.

I believe this is an issue with Linux/X11 and Mod1 vs Alt handling because Control key bindings work consistently. I haven't been able to locate any best practices for working around the issue, thus my appeal here.

Can someone share how to get an Alt key binding working in Linux/X11?

** Updated with a sample

from Tkinter import *

class GUI:
    def __init__(self,root):
        self.root = root
        e = Entry(self.root)
        e.grid(column=0,row=0)
        b = Button(self.root, text="Save", underline = 0)
        b.grid(column=0,row=1)
        root.bind("<Alt-s>",self.save)
        e.focus()

    def save(self,event=None):
        print("Hey, you pressed Alt-s!")
        return "break"

root = Tk()
App = GUI(root)
print("At this point, pressing Alt-s places the s string in the entry widget, and doesn't trigger")
root.mainloop()

** Update 2

I've had a few reviewers let me know this works on their system, even Linux. Perhaps there's a problem with my tiling wm or other configuration for X11, however I've had no problems using Alt with any other X11 GUI apps.

I'm open to suggestions on how to troubleshoot this.

** Update 3

I've been reviewing the behavior with xmodmap, and it appears that when I assign Alt_R the Tk keybindings stop working. The events reported by xev match verbatim, and yet Tk's behavior changes. Still digging.

** Update 4

Mr. Lange on the Tkinter list found a link that helps explain similar behavior, at https://bbs.archlinux.org/viewtopic.php?id=58145 .

I've made that change to my xmodmap, and now Alt works as expected. I can't explain why binding Alt_R to mod4 would effect Alt_L, or why it would only effect Tk applications.

Thanks.

like image 535
Demosthenex Avatar asked Oct 08 '22 20:10

Demosthenex


1 Answers

I am not sure if this will work differently on linux, but i figured i'd give this answer a shot because it's been a couple days with no reply.

I'm not sure if this is the syntax you are using or not, but instead of return break try return ("break")

as for the alt problem, maybe try something like:

from Tkinter import *

class GUI:
    def __init__(self,root):    
        self.alt = False
        e = Entry(root)
        e.pack()
        e.focus()
        root.bind("<Alt_L>",self.AltOn)
        root.bind("<KeyRelease-Alt_L>",self.AltOff)
        root.bind("<s>",self._s)

    def AltOn(self,event): self.alt=True
    def AltOff(self,event): self.alt=False
    def _s(self,event):
        if self.alt:
            #whatever you want to do with alt+s
            print "ALT S"
            return ("break")

root = Tk()
App = GUI(root)
root.mainloop()

EDIT: The description for bind is:

    FUNC will be called if the event sequence occurs with an
    instance of Event as argument. If the return value of FUNC is
    "break" no further bound function is invoked.

So i'm really not sure why break isn't working for you.. sorry I can't help more.

like image 179
Symon Avatar answered Oct 12 '22 10:10

Symon