I want to make the values I type on Entry field to be automatically converted to uppercase. I have code in here that only accepts uppercase letters to be typed in the entry using validatecommand.
from tkinter import * root = Tk() def text(a,b,c): ind=int(b) if c == '1': #insert if not a[ind].isupper(): return False return True entry = Entry(root, validate="key") entry['validatecommand'] = (entry.register(text),'%P','%i','%d') entry.pack() root.mainloop()Answer1:
If you want to convert the entry content to uppercase instead of preventing the user to insert non uppercase letters, you can associate a
StringVar to your entry and use its
trace_add since python 3.6) method to convert the entry content to uppercase each time the user modifies the entry.
trace takes two arguments: the mode and the callback. The mode determines when the callback is called. The modes are:
'write'for python 3.6): the callback is called when the variable is written (it's the mode I use in the code below),</li> <li>
'read'): the callback is called when the variable is read,</li> <li>
'unset'): the callback is called when the variable is deleted</li> </ul>
For more details about the arguments of the callback, see <a href="https://stackoverflow.com/questions/29690463/what-are-the-arguments-to-tkinter-variable-trace-method-callbacks" rel="nofollow">What are the arguments to Tkinter variable trace method callbacks?</a>
import tkinter as tk def to_uppercase(*args): var.set(var.get().upper()) root = tk.Tk() var = tk.StringVar(root) e = tk.Entry(root, textvariable=var) e.pack() try: # python 3.6 var.trace_add('write', to_uppercase) except AttributeError: # python < 3.6 var.trace('w', to_uppercase) root.mainloop()Answer2:
You can bind to an event instead of using .trace (in python 3.x, not tested in 2.x).
The following is copied verbatum from the accepted answer (by "bevdet") to <a href="https://bytes.com/topic/python/answers/897918-how-do-i-make-tkinter-text-entry-all-uppercase" rel="nofollow">https://bytes.com/topic/python/answers/897918-how-do-i-make-tkinter-text-entry-all-uppercase</a>.<hr />
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.
from Tkinter import * win = Tk() def caps(event): v.set(v.get().upper()) Label(win, text='Enter user nick:').pack(side=LEFT) v = StringVar() w = Entry(win, width=20, textvariable=v) w.pack(side=LEFT) w.bind("<KeyRelease>", caps) mainloop()<hr />
I was able to use this method in combination with custom validation (See B. Oakley answer to <a href="https://stackoverflow.com/questions/4140437/interactively-validating-entry-widget-content-in-tkinter" rel="nofollow">Interactively validating Entry widget content in tkinter</a>) by placing the binding OUTSIDE the validation function, immediately after creating the Entry widget. <em>Important: Do not put the binding inside the validation function, doing so will break the validation function</em> (see accepted answer to <a href="https://stackoverflow.com/questions/1019782/python-tkinter-entry-fun" rel="nofollow">Python tkInter Entry fun</a> for explanation and a possible workaround).