python - Key binding does not work in tkinter -


i made simple script:

from tkinter import *   class myframe(frame):     def __init__(self, parent = none):         frame.__init__(self, parent, bg = 'red')                         self.pack(fill=both, expand=yes)          self.bind('<key>', lambda e: print("pressed key"))    root = tk() root.geometry("300x200") f = myframe(root) root.mainloop() 

but binding pressing key not work. nothing happens whey press key. know why?

you need call bind method of parent, reference tkinter.tk instance represents main window:

parent.bind('<key>', lambda e: print("pressed key")) 

self.bind calling bind method of tkinter.frame instance created when did:

frame.__init__(self, parent, bg = 'red')  

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -