0

I am trying to work my way through Tkinter and this is a part of my code:

 FirstName = Label(canvas, text="First Name")
 FirstName.configure(width=30, bg="white", fg="black", border=10)
 FirstName = canvas.create_window(330, 130, anchor = CENTER, window=FirstName)

 FName_Entry = Entry(canvas)
 canvas.create_window(850, 145, window=FName_Entry, height=35, width=300)

As you can see this is an entry widget for users to enter their first name. how can I validate this to only accept string (letters) and if they try to enter integers, symbols or basically anything that is not a letter, it should display a message on the side of the widget urging users to enter a valid name.

I tried to check online but most of them are using classes and I am not used to classes as of yet and am new to Tkinter. other examples explain how to limit entry to integers so I am a bit confused here. Thanks for helping!

khushi
  • 365
  • 1
  • 4
  • 17
  • 1
    Anything typed into that widget will be a string. It sounds like you want to validate the characters a string contains. But be careful assuming what characters are valid: some names include spaces, [hyphens, accents](https://en.wikipedia.org/wiki/Marc-Andr%C3%A9_Fleury)... and these days there [could be other characters too](https://www.cnn.com/2020/05/25/entertainment/grimes-musk-baby-name-tweak-scli-intl/index.html). – ChrisGPT was on strike Aug 09 '20 at 00:09
  • @Chris, makes sense will keep that in mind! However, what about number - how do I display a message beside it if someone tries to enter numbers? – khushi Aug 09 '20 at 00:22
  • In general, I'd assume that anything a person enters is valid. There are a lot of names in the world, and a lot of them won't fit into your idea of what a valid name is. Elon Musk named his child "X Æ A-12", there was a [child named Seven (or was it "7") in Seinfeld many years ago](https://en.wikipedia.org/wiki/The_Seven) and of course there's [Eleven ("11"?) in Stranger Things](https://www.imdb.com/title/tt4574334/characters/nm5611121). Validate things that actually have validation rules: phone numbers (normalize them), email addresses (though I suggest just requiring an `@` there), ... – ChrisGPT was on strike Aug 09 '20 at 00:26
  • I have a friend whose last name is two characters long and he frequently has software forms telling him his name is invalid. Guess what's invalid? The rules on the form, not the name. – ChrisGPT was on strike Aug 09 '20 at 00:28
  • See https://stackoverflow.com/q/4140437/7432 – Bryan Oakley Aug 09 '20 at 01:10

2 Answers2

2

Here is a small snippet to actually make you understand better

from tkinter import * 
from tkinter import messagebox

root = Tk()

def check():
    sel = e.get()
    
    if not sel.isalpha():
        messagebox.showerror('Only letters','Only letters are allowed!')

e = Entry(root)
e.pack(pady=10)

b = Button(root,text='Click Me',command=check)
b.pack(padx=10,pady=10)

root.mainloop()

Here we are checking if sel.isalpha() returns False or not, if it does, then show a messagebox saying only letters are allowed. Simple as that.

Do let me know if any errors. Happy coding

Here is more on isalpha() method

Cheers

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
  • `"abc123".isdigit()` returns False, so no error will be shown and it is not what OP wants. `isalpha()` may be used instead. – acw1668 Aug 09 '20 at 11:57
  • Would add that "it should display a message on the side of the widget urging users to enter a valid name" too, but your solutions fits. – Thingamabobs Aug 09 '20 at 14:02
  • Yes sure thing, just a snippet here, hoepfully the OP will do it – Delrius Euphoria Aug 09 '20 at 14:05
  • Hi I tried what you said but I am having a little issue, could you please help me with https://stackoverflow.com/q/63330189/14054851 – khushi Aug 09 '20 at 19:51
1

You can use list in which you can store the letter which is to be accepted. Then check the each letter of the input with the element in the list. If any character not found from the input in the list(acceptable character) then it is invalid input.

# acceptable character list
accepted_characters = ['a', 'b', 'c',.....'z', 'A', 'B', 'C',...'Z']

# input from the tkinter entry widget
inp = "hello"

for i in inp:
    if i not in accepted_characters:
        print('Invalid data.')

Another way is using RegEx module which is built-in module. But I am not too familiar with RegEx.

  • actually if you think the other way around, making a list of numbers is easy right? cause only 10 numbers exists (incl 0), and if and elif, based on that :D – Delrius Euphoria Aug 09 '20 at 10:58
  • Valid characters are (a-z, A-Z) if you want to accept strings. But if you validate your string only with numbers i.e you can accept any letters but not integers then you can create a list of integers. The given example which I've shown will only accept (a-z, A-Z). If you don't want to accept only integers than you can use ```.isdigit()``` or ```.isalpha()```. If you want to validate the name then I think the given example is best. – Sarabjeet Sandhu Aug 09 '20 at 13:00
  • in a way, yes!! but wt i mean is, its cumbersome to create a list with all the letters in both cases – Delrius Euphoria Aug 09 '20 at 13:05
  • Create a module and import it in the main program. The module will contain all the validation data for any purpose. Which will keep your code clean. – Sarabjeet Sandhu Aug 09 '20 at 13:08
  • still the OP has to go through making the list somewhere right? why such, when there are inbuilt functions to check for you,. Plus your answer works perfect, just suggesting – Delrius Euphoria Aug 09 '20 at 13:10
  • Yeah there are built-in functions. Thanks for the suggestion. :-) – Sarabjeet Sandhu Aug 09 '20 at 13:14
  • and also i might suggest adding a `break` cause i think or else if there are 3 "invalid characters" then its gonna `print('Invalid data.')` 3 times? – Delrius Euphoria Aug 09 '20 at 13:14
  • 1
    Yeah, I too was thinking to put a break statement. Thanks. :-) – Sarabjeet Sandhu Aug 09 '20 at 13:25
  • Hi I tried what you said but I am having a little issue, could you please help me with stackoverflow.com/q/63330189/14054851 – khushi Aug 09 '20 at 19:51
  • You can set you variable as global. Define name out of function. Whenever you want to change the variable Name use "global Name" before that. – Sarabjeet Sandhu Aug 10 '20 at 04:35