Python Forum

Full Version: Always lowercase entry in tkinter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I'd like to have my entry always in lowercase and with only letters allowed. This is the code I've come up with:

def lowercase_letter_entry(P):
   if P.isalpha() or P == "":
     if P.islower():
        return True
     else:
      if not P.islower():
       P.get()lower()
       return True
   else:
    messagebox.showwarning("Warning","Only letters are allowed")
    return False
Right now the "check if entry are letters or not" is working but self.world.get() isn't in lowercase. this is how I'm using it
self.world = StringVar(self)
        self.world.set('')
        self.world=Entry(self,textvariable = self.world, width=280, bg='WHITE')  
        self.world.place(relx=0.19, rely=0.24, height=25, width=160) e
        callback_world = self.register(lowercase_letter_entry) 
        self.world.configure(validate="key", validatecommand=(callback_world, "%P"))
        print(self.world.get())
Can somebody help me? Thank you
make a method

def set_lower(self, event):
   zz.set(self.wordld.get().lower())

# replace line 6 with
self.world.bind("<KeyRelease>", set_lower)
there are other issues here:
the Entry widget cannot be same as StringVar
also you have some indentation issues.
and you will have to alter code above (set zz = to new StringVar nane), and textvariable part of entry.
The validator prevents invalid entries, it does not correct. You validator should return True if the P string is empty or an upper case alpha, otherwise it should return False. Changing the P string inside the validator is not going to have any effect. As mentioned before, P is a str, not the StringVar associated with the widget. P.get() throws an error because str.get() is not valid.
(Apr-17-2020, 11:01 AM)Larz60+ Wrote: [ -> ]make a method

def set_lower(self, event):
   zz.set(self.wordld.get().lower())

# replace line 6 with
self.world.bind("<KeyRelease>", set_lower)
there are other issues here:
the Entry widget cannot be same as StringVar
also you have some indentation issues.
and you will have to alter code above (set zz = to new StringVar nane), and textvariable part of entry.

I've tried to use it:
        def set_lower(self, event):
         one_world.set(self.name.get().lower())

        self.world = StringVar(self)
        self.world.set('')
        self.world=Entry(self,textvariable = self.world, width=280, bg='WHITE')
        self.world.place(relx=0.2, rely=0.05, height=25, width=100)
        callback_world = self.register(set_lower) 
        self.world.bind("<KeyRelease>", set_lower)
        print(one_world)
I've tried this and it is not working. Also I've a question: I'll need to use the .delete function on the lowered case entry. Could I use it with this code?
Thank you again
Just rewrite your validator:
def lowercase_letter_entry(P):
   return (P.isalpha() and P.islower()) or P == ""
(Apr-17-2020, 01:39 PM)deanhystad Wrote: [ -> ]Just rewrite your validator:
def lowercase_letter_entry(P):
   return (P.isalpha() and P.islower()) or P == ""

Thank you it worked great! I'm just a little bit afraid that it could increase the input error since someone could not realize that they are not typing. Is there a way to get the entry with eventual uppercase and then turn it all in lower case? Thank you
If you want you can play a an invalid key ding or display a message like in your first example.

Let the user type upper/lower. It is easy to convert when you finally use the value.
the entry widget has a validate entry command here's a link to stackoverflow thread validate entry the first example by brian oakley shows lowercase only and other options.
Thank you all, I've solved my problem! Have a nice day