2

I'm looking to use the Tkinter message windows as an error handler. Basically just saying you can only input "x,y,z"

It's being used on a program that asks the user for the input, Any integers that are => 0 and =<100 are accepted. At the moment it's working but only displays it on a label.

Can anyone suggest anything i can use to input a tkinter error window? Also any idea how i can limit the input to just Integers? Below is the add function that once the user inputs a number and click's add, it fires this function.

If i haven't explained this well then please advise me and i'll try to expand on it.

def add():
    try:

        value = int(MarkEnt.get())
        assert 0 <= value <= 100
        mark.set(value)
        exammarks.append(value)
        ListStud.insert(len(exammarks),value)

    except AssertionError:
        error1.set("Please only input whole numbers from 0 to 100")
        errorLbl.grid()
3
  • Asking the user for input until they give a valid response(stackoverflow.com/questions/23294658/…) Commented May 13, 2014 at 13:53
  • @NorthCat, as much as I love that post, it's not too applicable here, since the input is coming from a Tkinter Entry widget rather than the command line. Commented May 13, 2014 at 13:57
  • By the way, it may be a bad idea to depend on assert in this way, because asserts can be disabled by the user if they use the -O command line flag. Commented May 13, 2014 at 13:59

1 Answer 1

5

You can use the tkMessageBox module to show error boxes.

from Tkinter import *
import tkMessageBox

def clicked():
    tkMessageBox.showerror("Error", "Please only input whole numbers")

root = Tk()
button = Button(root, text = "show message box", command = clicked)
button.pack()
root.mainloop()

Error message created with tkinter

Also any idea how i can limit the input to just Integers?

You can use validatecommand to reject input that doesn't fit your specifications.

from Tkinter import *

def validate_entry(text):
    if text == "": return True
    try:
        value = int(text)
    except ValueError: #oops, couldn't convert to int
        return False
    return 0 <= value <= 100

root = Tk()

vcmd = (root.register(validate_entry), "%P")
entry = Entry(root, validate = "key", validatecommand=vcmd)
entry.pack()

root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.