1

Is there a relatively easy way to automatically capitalize a Tkinter Entry's text input in realtime? So as a user is inputting values, they automatically get capitalized. Thanks!

TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65

2 Answers2

2

Yes, it can be easily accomplished with trace and str.capitalize:

from Tkinter import *

root = Tk()
var = StringVar()
entry = Entry(root, textvariable=var)
entry.pack(padx=20, pady=20)

def autocapitalize(*arg):
    var.set(var.get().capitalize())

var.trace("w", autocapitalize)
root.mainloop()
A. Rodas
  • 20,171
  • 8
  • 62
  • 72
  • I'm running into issues with my class, I don't think Im getting the function to work properly. http://pastebin.com/9zJtq74n Do you know how I could get your answer to work in my context! Super thanks! – TheoretiCAL May 30 '13 at 20:27
  • 1
    @TheoretiCAL The problem is that `func` is not defined. Also, in `wat==True` and `eng==True` you are using the reference of the widget. You should change it to simply `wat.get()` and `eng.get()`. – A. Rodas May 30 '13 at 20:37
  • Oh I figured out the capitalize problem, I changed it to .upper() instead for the function and it worked. Thanks for the reference tip! I have func defined above the gui, so that was fine. Thanks so much! – TheoretiCAL May 30 '13 at 20:39
1

You can bind to an event instead of using .trace() (in python 3.x, not tested in 2.x).

For details see my answer to this similar question: python - Converting entry() values to upper case

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
user1459519
  • 712
  • 9
  • 20