好吧我自己的解决方案...
感谢@Justin S Barrett 和@Brian Oakley。
根据@Brian Oakley 的提示,我解决了我的问题:
class DigitEntry(Frame):
mapping = {
"o": "0",
"e": "1",
"z": "2",
"d": "3",
"v": "4",
"f": "5",
"s": "6",
"i": "7",
"a": "8",
"n": "9",
",": ".",
"0": "0",
"1": "1",
"2": "2",
"3": "3",
"4": "4",
"5": "5",
"6": "6",
"7": "7",
"8": "8",
"9": "9",
".": "."
}
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
self.entry = Entry(self, width=10)
self.entry.pack(fill="x")
self.entry.bind("<Key>", self.keymap)
self.entry.bind("<FocusOut>", self.check)
def check(self, event):
"""if checks are necessary
"""
pass
def keymap(self, event):
""" do mappings, pass allowed inputs and block the rest.
"""
if event.char in self.mapping:
self.entry.insert("insert", self.mapping[event.char])
return "break"
elif event.keysym in ["BackSpace", "Tab"]:
pass
else:
self.entry.insert("insert", "")
return "break"
class PointsEntry(DigitEntry):
def __init__(self, *args, **kwargs):
DigitEntry.__init__(self, *args, **kwargs)
def check(self, event):
value = self.entry.get()
if value.find(".") >= 0:
if not (value[-1] == "." or value[-2:] == ".5"):
err = value + " bad frac digit"
self.entry.delete(0, "end")
self.entry["bg"] = "red"
self.entry.insert("insert", err)
elif value[-1] == ".":
self.entry.insert("insert", "5")
if len(value) == 1:
self.entry.insert(0, "0")
return "break"