Created
April 26, 2016 15:08
-
-
Save JuniorPolegato/1c5b069b6fe61391e665a4d7e2aeb522 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import re | |
import locale | |
import gi | |
gi.require_version('Gtk', '3.0') | |
from gi.repository import Gtk, GObject | |
locale.setlocale(locale.LC_ALL, '') | |
frac_digits = locale.localeconv()['frac_digits'] | |
mask = '%%0.%if' % frac_digits | |
divisor = 10 ** frac_digits | |
def string_to_real(entry): | |
entry.handler_block_by_func(string_to_real) # bloqueia o sinal da entry | |
digits = re.sub('[^0-9]', '', entry.get_text()) | |
if digits: | |
# entry.set_text(locale.format(mask, float(digits) / divisor, True)) | |
entry.set_text(locale.currency(float(digits) / divisor, True, True)) | |
GObject.idle_add(entry.set_position, -1) # cursor no final do campo | |
else: | |
# entry.set_text(locale.format(mask, 0., True)) | |
entry.set_text(locale.currency(0., True, True)) | |
entry.set_position(-1) # cursor no final do campo | |
entry.handler_unblock_by_func(string_to_real) | |
if __name__ == '__main__': | |
# classe apenas para testar a função | |
class TestWindow(Gtk.Window): | |
def __init__(self): | |
val_entry = Gtk.Entry(margin=10, xalign=1) | |
val_entry.connect('changed', string_to_real) | |
val_entry.set_text('0') | |
super(TestWindow, self).__init__() | |
self.add(val_entry) | |
self.connect('delete-event', Gtk.main_quit) | |
self.show_all() | |
TestWindow() | |
Gtk.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment