我希望使用 Tkinter 消息窗口作为错误处理程序。基本上只是说你只能输入“x,y,z”
它被用在一个要求用户输入的程序上,任何 => 0 和 =<100 的整数都被接受。目前它正在工作,但仅将其显示在标签上。
任何人都可以建议我可以用来输入 tkinter 错误窗口的任何内容吗?另外你知道如何将输入限制为整数吗? 下面是添加函数,一旦用户输入数字并单击添加,就会触发此函数。
如果我没有很好地解释这一点,请告诉我,我会尽力扩展它。
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()
最佳答案
您可以使用tkMessageBox
显示错误框的模块。
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()
Also any idea how i can limit the input to just Integers?
您可以使用validatecommand
拒绝不符合您的规范的输入。
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()
关于Python:Tkinter 消息框 - 错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23632953/