associação pythonbrasil[11] django zope/plone planet Início Logado como (Entrar)

MaskEntry

   1 #!/usr/bin/env python
   2 
   3 # example maskentry.py
   4 
   5 import pygtk
   6 pygtk.require('2.0')
   7 import gtk
   8 import re
   9 from gtk import *
  10 import gtk.gdk
  11 import gobject
  12 
  13 
  14 
  15 
  16 class EntryMask(gtk.Entry):
  17 
  18     def __init__(self):
  19 
  20         self.gtkentry=gtk.Entry.__init__(self)
  21         self.chars = re.compile(r'[\-\.\(\)/{_}|]')
  22         self.set_text('')
  23         hid = self.connect_after('insert-text', self.filter)
  24         self.set_data('handlerid', hid)
  25 
  26 
  27     def set(self, text, *args):
  28 
  29         hid = self.get_data('handlerid')
  30         self.handler_block(hid)
  31         self.set_text(text)
  32         self.set_position(-1)
  33         self.handler_unblock(hid)
  34 
  35 
  36     def filter(self, *args):
  37         _text=self.get_text()
  38         _text=self.unmask(_text)
  39         _new=self.mask(_text)
  40         gobject.timeout_add(1, self.set , _new)
  41 
  42 
  43     def validate(self, text_char, mask_char):
  44 
  45         num = re.compile(r'[0-9]')
  46         lettup = re.compile(r'[A-Z]')
  47         lettdw = re.compile(r'[a-z]')
  48 
  49         if mask_char == '9':
  50             valid = num.match(text_char)
  51         elif mask_char == 'a':
  52             valid = lettdw.match(text_char)
  53         elif mask_char == 'A':
  54             valid = lettup.match(text_char)
  55         elif mask_char == 'X':
  56             valid =  (num.match(text_char) or
  57                      lettdw.match(text_char) or
  58                      lettup.match(text_char))
  59 
  60         return valid
  61 
  62     def mask(self, _text):
  63 
  64         len_mask=int(len(self._mask))
  65         self._mask=list(self._mask)
  66         _len=int(len(_text))
  67         _text=list(_text)
  68         z=0
  69         new=''
  70         for n in range(len_mask):
  71             mask_char=self._mask[n]
  72             if z<_len:
  73                 if self.chars.match(mask_char):
  74                     new=new+mask_char
  75 
  76                 else:
  77                     text_char=_text[z]
  78                     if self.validate(text_char, mask_char):
  79                         new=new+text_char
  80                         z=z+1
  81                     else:           # Trecho para correção do bug
  82                         new=new+''  # quando digitava um valor diferente da
  83                         z=z+1       # mask ele completava o entry com
  84                                     # os caracteres da mask
  85         return new
  86 
  87     def unmask(self, _text):
  88         _text=self.chars.sub('',_text)
  89         return _text
  90 
  91     def set_mask(self, _mask):
  92         self._mask=_mask
  93         self.set_max_length(len(self._mask))
  94 
  95 
  96 
  97 
  98 class EntryExample:
  99 
 100     def entry_toggle_editable(self, checkbutton, entry):
 101         entry.set_editable(checkbutton.get_active())
 102 
 103     def entry_toggle_visibility(self, checkbutton, entry):
 104         entry.set_visibility(checkbutton.get_active())
 105 
 106     def __init__(self):
 107 
 108         window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 109         window.set_size_request(200, 100)
 110         window.set_title("Teste de EntryMask")
 111         window.connect("delete_event", lambda w,e: gtk.main_quit())
 112 
 113         vbox = gtk.VBox(False, 0)
 114         window.add(vbox)
 115         vbox.show()
 116 
 117         entry = EntryMask()
 118         entry.set_mask('99.999.999/9999-XX')
 119         vbox.pack_start(entry, True, True, 0)
 120         entry.show()
 121 
 122         hbox = gtk.HBox(False, 0)
 123         vbox.add(hbox)
 124         hbox.show()
 125 
 126         check = gtk.CheckButton("Editavel")
 127         hbox.pack_start(check, True, True, 0)
 128         check.connect("toggled", self.entry_toggle_editable, entry)
 129         check.set_active(True)
 130         check.show()
 131 
 132         check = gtk.CheckButton("Visivel")
 133         hbox.pack_start(check, True, True, 0)
 134         check.connect("toggled", self.entry_toggle_visibility, entry)
 135         check.set_active(True)
 136         check.show()
 137 
 138         button = gtk.Button(stock=gtk.STOCK_CLOSE)
 139         button.connect("clicked", lambda w: gtk.main_quit())
 140         vbox.pack_start(button, True, True, 0)
 141         button.set_flags(gtk.CAN_DEFAULT)
 142         button.grab_default()
 143         button.show()
 144         window.show()
 145 
 146 def main():
 147     gtk.main()
 148     return 0
 149 
 150 if __name__ == "__main__":
 151     EntryExample()
 152     main()