質問

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()
役に立ちましたか?

解決

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()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top