0

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()
Usagi
  • 25
  • 1
  • 13

2 Answers2

1

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:

  • '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),
  • 'r' ('read'): the callback is called when the variable is read,
  • 'u' ('unset'): the callback is called when the variable is deleted

For more details about the arguments of the callback, see What are the arguments to Tkinter variable trace method callbacks?

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()
j_4321
  • 15,431
  • 3
  • 34
  • 61
1

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 https://bytes.com/topic/python/answers/897918-how-do-i-make-tkinter-text-entry-all-uppercase.


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

I was able to use this method in combination with custom validation (See B. Oakley answer to Interactively validating Entry widget content in tkinter) by placing the binding OUTSIDE the validation function, immediately after creating the Entry widget. Important: Do not put the binding inside the validation function, doing so will break the validation function (see accepted answer to Python tkInter Entry fun for explanation and a possible workaround).

user1459519
  • 712
  • 9
  • 20