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?
master.bind('<Space>', lambda...? - TigerhawkT3_tkinter.TclError: can't set fullscreen attribute for ".": override-redirect flag is setAlso there's no def forcreate_image(). - martineau