0

I have the code below to stop people entering nothing or entering names with numbers. It works, but I would like to add a message each time someone enter numbers, but not when they enter nothing. How would I do this.

from tkinter import *

import tkinter.simpledialog

player_one_name=""

def createGUI():
    global player_one_name

    diceWindow = Tk()


    while player_one_name=='' or player_one_name is None or not re.match("^[A-z]*$", player_one_name):
        player_one_name=tkinter.simpledialog.askstring("Player Name","Please enter your name: ")

createGUI()
user8435959
  • 165
  • 1
  • 11
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – wwii Aug 08 '17 at 18:59

4 Answers4

1

In while loop add:

if re.match('\d+', player_one_name):
    # your message
gribvirus74
  • 755
  • 6
  • 20
0

even better to encounter positive and negative numbers add the following to your code: the regular expression r'\d' only match single digit from 0 to 9 but r'-?\d+' should match positive and negative.

if re.match(r'-?\d+', player_one_name):
   print("your message")

or alternatively more straight forward use isdigit(). it is more readable this way

if any(i.isdigit() for i in player_one_name):
  print("your message")
OLIVER.KOO
  • 5,654
  • 3
  • 30
  • 62
0

def num_there(s): return any(i.isdigit() for i in s)

This kind of function can be used to see if there is a num inside string. You can then use some kind of if statement like:

if (num_there(playername)):
       print("no nums allowed")
rajivs
  • 1
0

I would use a simple for loop and some if statements to validate the name myself.

I have a button that links to a method used to validate the name.

This method gets the content of the entry field and then checks to see if it contains any numbers. If it does then set the variable valid to False and the following if statement will perform the desired results.

I only check for numbers but you can have your loop check for anything.

Take a look at the below code:

import tkinter as tk

class App(tk.Frame):

    def __init__(self, master):
        self.master = master
        lbl1 = tk.Label(self.master, text = "Type a name and press enter")
        lbl1.pack()
        self.entry1 = tk.Entry(self.master)
        self.entry1.pack()
        btn1 = tk.Button(self.master, text = "Enter", command = self.validate_name)
        btn1.pack()

    def validate_name(self):
        name = self.entry1.get()
        valid = True

        if name != "":
            for i in name:
                if i in "0123456789":
                    valid = False

            if valid == True:
                print("{} is a good name!".format(name))
            else:
                print("No numbers are allowed")

if __name__ == "__main__":

    root = tk.Tk()
    myapp = App(root)
    root.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79