Python 作为类方法的输入验证

Python 作为类方法的输入验证,python,class,validation,input,tkinter,Python,Class,Validation,Input,Tkinter,试图让输入验证在顶级窗口上为一些tkinter条目小部件工作,但我一直在用类定义一切(窗口/框架)。因此,现在当我尝试注册验证函数时,我不确定要向谁注册它,我尝试了类名,但这给了我一个“name(classname)is not defined”错误。我将如何注册以下验证函数以在类中工作?我创建了一个Medical类的实例以打开顶级窗口,但这是另一个单独类的不同方法 class Medical(tk.Toplevel): def __init__(self, master, *args,

试图让输入验证在顶级窗口上为一些tkinter条目小部件工作,但我一直在用类定义一切(窗口/框架)。因此,现在当我尝试注册验证函数时,我不确定要向谁注册它,我尝试了类名,但这给了我一个“name(classname)is not defined”错误。我将如何注册以下验证函数以在类中工作?我创建了一个Medical类的实例以打开顶级窗口,但这是另一个单独类的不同方法

class Medical(tk.Toplevel):
    def __init__(self, master, *args, **kwargs):
        tk.Toplevel.__init__(self, master, *args, **kwargs)
        self.master = master
        self.resizable(width=False, height=False)
        self.title('Medical Certificate Information')
        self.attributes('-topmost', True)
        self.geometry('230x230+580+280')
        self.grab_set()
        self.transient(master)
        self.duedate = tk.StringVar()
        self.age = tk.StringVar()

        frame1 = tk.Frame(self, bd=4, relief='ridge', width=230, height=230)
        frame1.place(anchor='nw')

        birthdate = tk.Label(self, text='Enter Birth Date(XX/XX/XXXX):', font='arial 10 bold')
        birthdate.place(anchor='nw', x=5, y=5)
        self.birthdate_entry = tk.Entry(self, font='arial 10 bold', width=10, relief='sunken', bd=2)
        self.birthdate_entry.place(anchor='nw', x=5, y=30)
        self.birthdate_entry.focus_set()

        agelabel = tk.Label(self, text = 'Age:', font='arial 10 bold')
        agelabel.place(anchor = 'nw', x= 130, y=30)
        ageinputlabel = tk.Label(self, font = 'arial 10 bold', textvariable = self.age)
        ageinputlabel.place(anchor = 'nw', x = 160, y = 30)

        medical = tk.Label(self, text='Medical Date(XX/XX/XXXX):', font='arial 10 bold')
        medical.place(anchor='nw', x=5, y=60)
        self.medical_entry = tk.Entry(self, font='arial 10 bold', width=10, relief='sunken', bd=2)
        self.medical_entry.place(anchor='nw',x=5, y=85)

        duedate = tk.Label(self, text='Medical Due Month:', font='arial 10 bold')
        duedate.place(anchor='nw', x=5, y=115)
        self.duedatelabel = tk.Label(self, font='arial 10 bold', width=10, relief='sunken', bd=2, textvariable=self.duedate)
        self.duedatelabel.place(anchor='nw', x=5, y=140)

        next = tk.Button(self, text='Next...', font='arial 10 bold', bd=2, bg='grey70', command=self.write)
        next.place(anchor='nw', x=170, y=195)

    def validate(self, inp, widget_name):
        if len(inp)<2:
            return True
        elif len(inp)==2:
            widget_name.insert('/')
            return True
        elif len(inp)<4:
            return True
        elif len(inp)==4:
            widget_name.insert('/')
            return True

    max1 = Medical.register(partial(validate, widget_name=self.birthdate_entry))
    max2 = Medical.register(partial(validate, widget_name=self.medical_entry))

    self.birthdate_entry.config(validate='key', validatecommand=(max1, '%P'))
    self.medical_entry.config(validate='key', validatecommand=(max2, '%P'))
class Medical(传统顶级):
定义初始值(self、master、*args、**kwargs):
tk.Toplevel.\uuuuu init\uuuuuuu(self、master、*args、**kwargs)
self.master=master
可自行调整大小(宽度=假,高度=假)
自身名称(“医疗证明信息”)
self.attributes('-top',True)
自几何体('230x230+580+280')
self.grab_set()
自瞬态(主)
self.duedate=tk.StringVar()
self.age=tk.StringVar()
框架1=tk.框架(自,bd=4,浮雕=ridge',宽度=230,高度=230)
框架1.放置(锚定class='nw')
birthdate=tk.Label(self,text='输入出生日期(XX/XX/XXXX):',font='arial 10 bold')
出生日期。地点(anchor='nw',x=5,y=5)
self.birthdate_entry=tk.entry(self,font='arial 10 bold',width=10,relief='sunken',bd=2)
自我出生日期\输入地点(anchor='nw',x=5,y=30)
self.birthdate\u entry.focus\u set()
agelabel=tk.Label(self,text='Age:',font='arial 10 bold')
agelabel.place(锚定='nw',x=130,y=30)
ageinputlabel=tk.Label(self,font='arial 10 bold',textvariable=self.age)
ageinputlabel.place(锚点='nw',x=160,y=30)
medical=tk.标签(self,text='medical Date(XX/XX/XXXX):',font='arial 10 bold')
医疗地点(锚=nw',x=5,y=60)
self.medical_entry=tk.entry(self,font='arial 10 bold',width=10,relief='sunken',bd=2)
自我医疗输入位置(锚定=nw',x=5,y=85)
duedate=tk.Label(self,text='Medical Due Month:',font='arial 10 bold')
duedate.地点(锚点=nw',x=5,y=115)
self.duedatelabel=tk.Label(self,font='arial 10 bold',width=10,relief='sunken',bd=2,textvariable=self.duedate)
self.duedatelabel.place(anchor='nw',x=5,y=140)
next=tk.Button(self,text='next…',font='arial 10 bold',bd=2,bg='grey70',command=self.write)
下一个位置(锚点=nw',x=170,y=195)
def验证(自身、inp、小部件名称):

如果len(inp)这里有太多与您的问题无关的代码。要增加获得答案的机会,请创建一个。也许只是一个经过验证的字段。就我个人而言,我会为每种字段类型创建一个新类,例如一个日期输入类,其中包含用于验证输入的代码。
register
是每个小部件上的一个方法。令人尴尬的是,我花了太长时间才实现我的验证寄存器和以下内容。配置处于错误的缩进级别。。。