Entering an upper case in the input field Tcl / Tk

I have a Tcl / Tk window with an input field in which I would like to force an uppercase character. That is, if any letters are printed, I would like them to be displayed in upper case in the input field, and not just reject the input in lower case.

I looked through the documentation for entry and Tcl / Tk wiki login validation , but I should not search in the right place, because although there are many validation examples, I cannot find an example of key input filtering to change the case.

The closest I could get is something like this:

entry .message -validate key -validatecommand { .message insert %i [string toupper "%S"] return 0 } 

This causes the first character to be uppercase, but subsequent characters are not translated. In fact, the script confirmation is not called at all after the first character. If I omit the .message insert command for testing, a script confirmation is called for each character.

+4
source share
2 answers

If you set a new value for your entry in your validation command, validation will be disabled (presumably to prevent an infinite loop). However, you can turn it back on:

 entry .message -validate key -validatecommand { .message insert %i [string toupper "%S"] .message configure -validate key return 0 } 
+5
source

Alternatively, you can use events and bindings:

 entry .message bind .message <KeyRelease> { set v [string toupper [.message get]] .message delete 0 end .message insert 0 $v } pack .message 

This gives an idea of ​​the type of things you might want to pay attention to - the handling in this case is very simple and can be greatly improved.

+1
source

Source: https://habr.com/ru/post/1301301/


All Articles