Hi everyone, I am doing a program with python on Windows Vista, and I've put a entry in my program, now what I need to do is only allow the user to write number, if the user tries to write anything apart from numbers and commas, appears a message box saying that the program doesn't allow that. Can anyone help me out with that?

Thanks Dan.

I'm not even going to lecture you about why that is just horrible UX. If you want to put your users through that kind of torture, it's your call. The principle of what you're trying to achieve is the same no matter what toolkit you use. I don't use Tkinter though, so here's pseudocode (just put it in a routine that gets called every time the text in the entry changes):

for each character in the entry field:
    If the character entered is not a number:
        delete the character
        set deleted flag
    
if deleted flag is set:
    deploy annoying message box

Of course, it's better to just use a control that was designed to input numbers. (A spinner or slider, depending on what the number being entered represents.)

Hi dan,
We have validator concepts in python. I am sending a sampla code on that one. I think it is helpful to you.

pl = Tkinter.Label(group.interior(),text="Please enter the number of items to be added",bg="wheat",relief='ridge')
    pl.grid(row=3,column=0,sticky='nsew')

    self.hwtb = Pmw.EntryField(group.interior(),validate = {'validator' : 'integer',
                    'min' : 0, 'max' : 9})
    self.hwtb.grid(row=3,column=1,sticky='nsew')

Edited 3 Years Ago by Reverend Jim: Fixed formatting

The Tkinter GUI toolkit has data input dialogs that will validate your input, as shown here:

# Python GUI toolkit Tkinter has three different data input dialogs:
# tkSimpleDialog.askstring(title, prompt [,options])
# tkSimpleDialog.askinteger(title, prompt [,options])
# tkSimpleDialog.askfloat(title, prompt [,options])
# does error trapping with int and float, also optional max/min
# there is also an initialvalue=parameter

try:
    # for Python2
    import Tkinter as tk
    import tkSimpleDialog as tksd
except:
    # for Python3
    import tkinter as tk
    import tkinter.simpledialog as tksd


root = tk.Tk()

# parent=root needed to put dialog window on top of parent root window
mystr = tksd.askstring("Dialog (String)", "Enter your name:", parent=root)
print(mystr)

age = tksd.askinteger("Dialog (Integer)", "Enter your age:", parent=root,
    minvalue=0, maxvalue=120)
print(age)

pay = tksd.askfloat("Dialog (Float)", "Enter your annual pay:",
    parent=root, minvalue=1000)
print(pay)

root.mainloop()

# optional help
#help(tksd)

I really would like to use rajasekhar1242's way, with Pmw, but the thing is: I dont know how to install Pmw on windows vista.
Is there a version of Pmw for windows vista? where? and how to install it?

Thank you all, Dan08.

You open a DOS shell (from the windows main menu, you should be able to open a command line (I don't use vista, so I don't know which terminology is used)). Then in this shell, you move to the Pmw folder that you downloaded, whith a command like

cd C:\.....\Pmw

(put the path to the folder containing the file "setup.py"), press return, then type the command

python setup.py install

and return. To test your installation, open a python shell (for example idle) and execute import Pmw . If python doesn't complain, everything is ok.

Ive been searching on google but I cant find an .exe or a .zip file, for example Pmw.exe or Pmw.zip.
But I could find loads of tar.gz files, does this means that I cant have it, of course winzip cant open a file that is made for unix systems.
What can i do?

Ive been searching on google but I cant find an .exe or a .zip file, for example Pmw.exe or Pmw.zip.
But I could find loads of tar.gz files, does this means that I cant have it, of course winzip cant open a file that is made for unix systems.
What can i do?

Do yourself a favor and uninstall winzip. Download 7zip, which can handle all the important compressed formats

Ive got that one too... what should i do next?

Thank you all very much, Dan08.

Ive got that one too... what should i do next?

Open the archive, extract the files to your site-packages directory and use them as needed.

PMW has not been updated since 2007. Do you really want to go with this stale old thing?

Well, I dont see another solution. Does anyone have another solution to solve my problem?

Ok, thank you all again for your help, Ive done it, i had to put setup.py in the same folder of python.exe, and then run it with command prompt.

Thank you all, Dan08.

This question has already been answered. Start a new discussion instead.