python - tkinter validation causes duplicate field behaviour -


i trying write validated entry classes in tkinter. on python 3.3, anacondas.

in following code, when start typing text 1 of 2 entries, python inserts text other field type. not want that. 1 field should validated date, , other validated integer.

what doing wrong?

if comment out e1 lines, works. if comment out e2 lines works. if have both somehow linked.

import pandas pd tkinter import *   class validatedentry(entry):      def __init__(self,*args,**kwargs):         entry.__init__(self,*args,**kwargs)          if 'default_value' in kwargs.keys():             self.default_value = kwargs['default_value']         else:             self.default_value = 0          self.previous_value = self.default_value                     self.config(textvariable=self.default_value)          self.register(self.__validate_entry())                 self.config(validate='focusout',validatecommand=self.__validate_entry)      def __validate_entry(self):          print('validating')         try:              self.validation_function(self.get())             self.previous_value = self.get()             return true         except exception:                         self.delete(0,end)             self.insert(0,self.previous_value)             return false      def validation_function(self,value):         print('should here?')         i=int(self.get())         #raise notimplemented('this abstract can''t implement it.')          class integerentry(validatedentry):                 def validation_function(self,value):         print('checking int {0}'.format(value))         i=int(self.get())  class dateentry(validatedentry):      def validation_function(self,value):         print('checking date {0}'.format(value))         d=pd.datetime.strptime(value,'%y-%m-%d')   master = tk()  e1 = integerentry(master) e1.pack() e1.focus_set()  e2 = dateentry(master) e2.pack()  b = button(master, text="get", width=10) b.pack()  mainloop() 

you cannot use normal python variable value of textvariable attribute. need use instance of stringvar, intvar, booleanvar or doublevar. using normal python variable, you're giving both widgets same textvariable, why set one, other gets set too.

however, since aren't using textvariable attribute anything, can remove option altogether , code work.

this line looks suspect:

self.register(self.__validate_entry())         

it's not doing think doing. one, it's calling validate_entry function @ startup doesn't useful since user hasn't entered data yet -- you're validating default value. second, reason call register use value returns, , you're ignoring returns. should remove line, it's useless.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -