How do I make Tkinter KeyRelease Event consistently provide uppercase letters? How do I make Tkinter KeyRelease Event consistently provide uppercase letters? tkinter tkinter

How do I make Tkinter KeyRelease Event consistently provide uppercase letters?


What is happening is that you are releasing the shift key before the letter key. The shift is pressed at the time the character is inserted which is why the widget gets an uppercase character, but by the time your keyrelease binding is processed the shift has already been released so you see the lowercase character.

If you want to print what is being inserted, bind to the key press instead of the release.


Based on Bryan's insight, I modified the code and it appears to work:

from Tkinter import *import stringclass App:  def __init__(self):    # create application window    self.root = Tk()    # add frame to contain widgets    frame = Frame(self.root, width=768, height=576, padx=20, pady=20, bg="lightgrey")    frame.pack()    # add text widget to contain text typed by the user    self.text = Text(frame, name="typedText", bd="5", wrap=WORD, relief=FLAT)    self.text.bind("<KeyPress>", self.printKey)    self.text.pack(fill=X)  """  this correctly prints the letters when pressed (and does not print the Shift keys)  """    def printKey(self, event):    # Adapted from http://www.kosbie.net/cmu/fall-10/15-110/koz/misc-demos/src/keyEventsDemo.py    ignoreSyms = [ "Shift_L", "Shift_R", "Control_L", "Control_R", "Caps_Lock" ]        if event.keysym not in ignoreSyms:      print event.char  def start(self):    self.root.mainloop()def main():  a = App()  a.start()if __name__ == "__main__":  sys.exit(main())