By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,996 Members | 1,498 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,996 IT Pros & Developers. It's quick & easy.

How do I make tkinter text entry to all uppercase?

Thekid
100+
P: 145
I have a toplevel window in tkinter that has some entry fields. Is there a way that I can get it to change the letters typed in it to uppercase, either as they're typed or after they're typed, while in the entry field? I don't want whatever is typed in to print out to IDLE. So as an example:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. win = Tk()
  3.  
  4. Label(win, text='Enter user nick:').pack(side=LEFT)
  5. Entry(win, width=20).pack(side=LEFT)
  6.  
  7. mainloop()
  8.  
As, or after I type Thekid in the entry box, I'd like it to turn to all caps.
Oct 8 '10 #1

✓ answered by bvdet

You can bind an event to your widget that calls a function to convert the text to upper case. You will need to initialize a textvariable for the Entry widget. In your case, there is nothing else to take the focus, otherwise you could bind "<FocusOut>" to the widget. "<KeyRelease>" works nicely however.
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. win = Tk()
  3.  
  4. def caps(event):
  5.     v.set(v.get().upper())
  6.  
  7. Label(win, text='Enter user nick:').pack(side=LEFT)
  8. v = StringVar()
  9. w = Entry(win, width=20, textvariable=v)
  10. w.pack(side=LEFT)
  11. w.bind("<KeyRelease>", caps)
  12.  
  13. mainloop()

Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
You can bind an event to your widget that calls a function to convert the text to upper case. You will need to initialize a textvariable for the Entry widget. In your case, there is nothing else to take the focus, otherwise you could bind "<FocusOut>" to the widget. "<KeyRelease>" works nicely however.
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. win = Tk()
  3.  
  4. def caps(event):
  5.     v.set(v.get().upper())
  6.  
  7. Label(win, text='Enter user nick:').pack(side=LEFT)
  8. v = StringVar()
  9. w = Entry(win, width=20, textvariable=v)
  10. w.pack(side=LEFT)
  11. w.bind("<KeyRelease>", caps)
  12.  
  13. mainloop()
Oct 8 '10 #2

Thekid
100+
P: 145
I see! That works like a charm! Thanks
Oct 8 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.