Home > Enterprise >  How to restrict user input into entry widgets?
How to restrict user input into entry widgets?

Time:08-24

I am writing a code to generate tournament brackets for the user. I wrote this piece of code so the user can input the number of teams participating in the tournament. I want to limit the input to only an integer, and the number should be less than or equal to 16 and more than 1. How do I add onto my code to accomplish this?

#Designing window for selecting number of teams for round-robin tournament(max 16)

from tkinter import *

def enter_numberofitems2():
  enter_numberofitems2_screen = Tk()
  enter_numberofitems2_screen.geometry("1000x500")
  enter_numberofitems2_screen.title("Enter Number of Players/Teams")
  Var1 = IntVar() 
  Label(enter_numberofitems2_screen, text= "Choose Number of Players/Teams", bg="yellow", 
  width="300", height="5", font=("Calibri", 20)).pack()
  Label(enter_numberofitems2_screen, text="").pack()
  Label(enter_numberofitems2_screen, text="Enter here", fg="green", height = "1", width = 
  "15", font=("calibri", 15)).pack()
  enter_teams = Entry(enter_numberofitems2_screen,textvariable=Var1).pack()
  Label(enter_numberofitems2_screen, text="").pack()
  Button(enter_numberofitems2_screen, text="Submit", height="5", width = "30", 
  bg="green").pack()

  enter_numberofitems2_screen.mainloop()

CodePudding user response:

Can you use something like this?

#Designing window for selecting number of teams for round-robin tournament(max 16)

from tkinter import *

def enter_numberofitems2():
  global Var1, lbl1
  enter_numberofitems2_screen = Tk()
  enter_numberofitems2_screen.geometry("1000x500")
  enter_numberofitems2_screen.title("Enter Number of Players/Teams")
  Var1 = IntVar()
  Label(enter_numberofitems2_screen, text= "Choose Number of Players/Teams", bg="yellow", 
  width="300", height="5", font=("Calibri", 20)).pack()
  Label(enter_numberofitems2_screen, text="").pack()
  Label(enter_numberofitems2_screen, text="Enter here", fg="green", height = "1", width = 
  "15", font=("calibri", 15)).pack()
  enter_teams = Entry(enter_numberofitems2_screen,textvariable=Var1).pack()
  Label(enter_numberofitems2_screen, text="").pack()
  Button(enter_numberofitems2_screen, text="Submit", height="5", width = "30", 
  bg="green", command=check).pack()
  lbl1 = Label(enter_numberofitems2_screen, text="", font=("Arial", 14))
  lbl1.pack()

  enter_numberofitems2_screen.mainloop()

def check():
    try:
        if Var1.get() >= 1 and Var1.get() <= 16:
            lbl1.configure(text=f"You enter: {Var1.get()}")
        else:
            lbl1.configure(text="Wrong number! (Must be greater than 1 and less than 16)")
    except Exception:
        lbl1.configure(text="Please, enter a number!")

enter_numberofitems2()

CodePudding user response:

You could try to add validation to the Entry Widget, which would not let the user enter anything except an integer that is in the [0, 16] range.

This tkinter documentation explains more thoroughly how you can achieve this.

from tkinter import *


def enter_numberofitems2():
    enter_numberofitems2_screen = Tk()
    enter_numberofitems2_screen.geometry("1000x500")
    enter_numberofitems2_screen.title("Enter Number of Players/Teams")
    Var1 = IntVar()
    Label(enter_numberofitems2_screen, text= "Choose Number of Players/Teams", bg="yellow",
          width="300", height="5", font=("Calibri", 20)).pack()
    Label(enter_numberofitems2_screen, text="").pack()
    Label(enter_numberofitems2_screen, text="Enter here", fg="green", height = "1", width =
    "15", font=("calibri", 15)).pack()

    def valid_func(what):
        if what.isdigit():
            if 0 <= int(what) <= 16:
                return True
        return False
    check_command = enter_numberofitems2_screen.register(valid_func)

    enter_teams = Entry(enter_numberofitems2_screen, validate='all', validatecommand=(check_command, '%P'), textvariable=Var1).pack()


    Label(enter_numberofitems2_screen, text="").pack()
    Button(enter_numberofitems2_screen, text="Submit", height="5", width="30",
           bg="green").pack()

    enter_numberofitems2_screen.mainloop()

enter_numberofitems2()

CodePudding user response:

Try something like this(i have have you a basic way of doing it, you just need to implement it to your code):

teams = input("How many teams participating in the tournament (1 - 16): ")
while not int(teams) in range(1,17):
    teams = input("How many teams participating in the tournament (1 - 16): ")

Or something like this:

enter_teams = Entry(enter_numberofitems2_screen,textvariable=Var1).pack()
while not int(enter_teams) in range(1,17):
    enter_teams = Entry(enter_numberofitems2_screen,textvariable=Var1).pack()
  • Related