python - Converting entry() values to upper case


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()


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 (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:

<ul><li>'w' ('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>'r' ('read'): the callback is called when the variable is read,</li> <li>'u' ('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()


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).


  • how to change url in browser url box?
  • Javascript: parseInt() with trailing characters
  • Confusing prototype behavior in JavaScript
  • Creating a SOAP proxy?
  • TFS Builds: Running the builds as administrator
  • Eclipse: missing requirement E4 RCP patch when installing properties editor
  • unrecognized selector isPitched called
  • maven jboss-as:start A required class was missing … org/sonaty…/ArtifactResolutionException
  • import scipy.sparse failed
  • Doctrine/Symfony entity generator and generating entity from one table
  • Azure webjobs output logs indexing taking very long
  • Casting between Interfaces and Classes
  • jwtBearer bearer token with rc-1 update to ASP.Net 5
  • Enabling DTD support in Sql Server
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • Convert SQLite database to XML
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Python pickle not one-to-one: different pickles give same object
  • How to add a focus style to an editable ComboBox in WPF
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • WPF Visiblity Binding to Boolean Expression with multiple Variables
  • Conversion from string “a” to type 'Boolean' is not valid
  • ADO and msqli connections very slow
  • Marklogic : Query response time is very high
  • Create DicomImage from scratch using Dcmtk
  • Problem deserializing objects from cache on MyBatis 3/Java
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Align navbar back button on right side
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Trying to switch camera back to front but getting exception
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Arrays break string types in Julia
  • Load html files in TinyMce
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Programmatically clearing map cache