1
votes

I looked through about a dozen posts about Tkinter, binding keypress events, and canvas widgets, but none of the solutions I read changed anything in my program. The GUI currently works with Button-1 as the event, but I need it to be operable from a keyboard. I tried using various keys and letters of the alphabet. I've also tried self.c.focus_set(), which didn't work when I tried binding the event to the canvas or to the frame. Here's my code:

from Tkinter import *

class App:
    def __init__(self, master):
        self.parent = master
        frame = Frame(master)
        screen_width, screen_height = master.winfo_screenwidth(), master.winfo_screenheight()
        frame.configure(background='black')
        frame.place(x=0,y=0,width=screen_width,height=screen_height)
        master.overrideredirect(1)


        h = .8*screen_height
        self.c = Canvas(frame, width=h, height=h, bd=0, highlightthickness=0, bg='black')
        self.c.place(relx=0.5, rely=0.5, anchor=CENTER)
        radius = 10
        self.c.update_idletasks()

        hCanvas = (self.c.winfo_height())
        offset = int(float(.4*hCanvas))
        self.c.create_oval(hCanvas/2-radius, hCanvas/2-radius, hCanvas/2+radius, hCanvas/2+radius, fill='white', outline='white')

        self.c.focus_set()
        self.c.update_idletasks()
        self.c.bind('<Key>', lambda event, arg=[radius]: self.create_stim(event, arg))

    def create_stim(self, event, args):
        print('create stim')


if __name__ == "__main__":
    root = Tk()
    root.attributes('-fullscreen', True)
    app=App(root)

    root.update()
    root.mainloop()

Thank you in advance for your help!

  • Edited to reflect changes suggested by martineau.

Currently, when I run the program this way, I am no longer in fullscreen, I cannot click on the window to select it, and I get the error noise every time I hit a key on the keyboard. If I put root.attributes('-fullscreen', True) after app=App(root), then I get the GUI in fullscreen and I can click on it, and again get the error noise every time a key is pressed. Is it possible there is a problem with the version of python (2.7.10) running on my computer (MacBook Pro, running on OS X Yosemite, 10.10.4)? Should I download python 3?

1
Have you tried binding it to the root object with master.bind('<Space>', lambda...? - TigerhawkT3
What does it do when you claim it doesn't work? Does it throw an error? If so, what error? - Bryan Oakley
Can't run the code in your question: _tkinter.TclError: can't set fullscreen attribute for ".": override-redirect flag is set Also there's no def for create_image(). - martineau
@BryanOakley I don't get any errors when I run the GUI, but nothing happens when I press a key. When I ran it with '<Button-1>', I was able to click on the canvas and get to the next screen. - Maren
Suggest you read and follow the How to create a Minimal, Complete, and Verifiable example help article. - martineau

1 Answers

0
votes

This appears to be a limitation of Tkinter on OSX. When you call master.overrideredirect(1), it seems to turn off the ability for tkinter to process keyboard events. If you omit the call to overrideredirect your code will start to work.