Python Forum
Always lowercase entry in tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Always lowercase entry in tkinter
#1
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
Reply
#2
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.
Reply
#3
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.
Reply
#4
(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
Reply
#5
Just rewrite your validator:
def lowercase_letter_entry(P):
   return (P.isalpha() and P.islower()) or P == ""
Reply
#6
(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
Reply
#7
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.
Reply
#8
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.
Reply
#9
Thank you all, I've solved my problem! Have a nice day
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Making entry global in tkinter with multiprocessing luckyingermany 2 2,323 Jan-21-2022, 03:46 PM
Last Post: deanhystad
  Tkinter Exit Code based on Entry Widget Nu2Python 6 2,989 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,202 Oct-15-2021, 08:01 AM
Last Post: drSlump
  Tkinter | entry output. Sap2ch 1 1,995 Sep-25-2021, 12:38 AM
Last Post: Yoriz
  .get() from generated Entry widgets in tkinter snakes 4 4,222 May-03-2021, 11:26 PM
Last Post: snakes
  Entry Validation in tkinter shahulvk 4 16,324 Oct-28-2020, 10:12 PM
Last Post: joe_momma
  [Tkinter] Getting Input from Tkinter Entry juliabrushett 6 21,367 May-30-2020, 03:29 PM
Last Post: Larz60+
  Converting Entry field value to integer in tkinter scratchmyhead 2 4,950 May-11-2020, 03:41 PM
Last Post: scratchmyhead
  [Tkinter] Tkinter adding entry values scratchmyhead 1 2,200 May-04-2020, 05:21 AM
Last Post: Yoriz
  [Tkinter] Entry box not showing 2 decimal places Chuck_Norwich 3 5,707 Apr-24-2020, 05:28 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020