尝试创建一个仅允许输入十六进制值的输入框。
我已经尝试过以下操作,但是一旦输入非十六进制,我就会收到错误。
class HexEntry(Entry):
def __init__(self, master=None, **kwargs):
self.var = StringVar()
Entry.__init__(self, master, textvariable=self.var, **kwargs)
self.old_value = ''
self.var.trace('w', self.check)
def check(self, *args):
current_value = self.var.get()
if (int(current_value,16)):
self.old_value = current_value
elif current_value == "":
self.old_value = current_value
else:
self.var.set(self.old_value)
这似乎工作正常。
class HexEntry(Entry):
def __init__(self, master=None, **kwargs):
self.var = StringVar()
Entry.__init__(self, master, textvariable=self.var, **kwargs)
self.old_value = ''
self.var.trace('w', self.check)
def check(self, *args):
current_value = self.var.get()
try:
if (int(current_value,16)):
self.old_value = current_value
elif current_value == "":
self.old_value = current_value
except:
self.var.set(self.old_value)