Hey All,

sorry if this is a stupid question but im still new to all this.
i want to enter something into a text field..

but i want for example the time in 24hr format "2200". How can i ensure that what the person is entering is in fact an int?

Thanks for any help guys

If you use the Tkinter GUI toolkit, you can use an input dialog box called askinteger, it makes sure only integers are entered. A typical example ...

# use Tkinter popup dialogs for input

import Tkinter as tk
import tkSimpleDialog as tsd  # has askfloat, askinteger, askstring

def get_data():
    miles = tsd.askfloat('Miles', 'Enter miles driven')
    petrol = tsd.askfloat('Petrol', 'Enter gallon of petrol consumed')
    str1 = "Your milage is %0.2f miles/gallon" % (miles/petrol)
    var1.set(str1)

root = tk.Tk()
var1 = tk.StringVar()

button = tk.Button(root, text="Get data", command=get_data)
button.pack(pady=5)
label = tk.Label(root, textvariable=var1)
label.pack()

root.mainloop()

Also, you can use this small function that makes sure an integer is entered within a given range ...

def check_input(low, high):
    """
    check the input to be an integer number within a given range
    """
    prompt = "Enter a 24 hour time (hhmm) between %d and %d: " % (low, high)
    while True:
        try:
            val = int(raw_input(prompt))
            if low <= val <= high:
                return val
        except ValueError:
            print "Just an integer number!"

# expect an integer number between 0000 and 2359
time24h = check_input(0, 2359)
print time24h
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.