samedi 9 août 2014

python - affichage unicode dans wxTextCtrl - Stack Overflow


I am using python 2.7 for creating a text editor. I am able to create the corresponding Unicode Malayalam(Indian Language) characters but I am not aware how to display it


on text control. I need to work the text Control like a Malayalam Text Editor. The editor should support the space, newline ,


insert characters also..it should work like a note pad. I am pasting the code which I have. 1. engine.py is a mapping file which does all the mapping and at the back-end it will generate the Malayalam letters. All I


required is to get it displayed on screen correctly. the engine.py code and texteditor.py code are as below


The texteditor.py is the file which has to be executed. conv(self, event) is the function which performs the conversion to


Malayalam. The issues I am facing here are 1. when I press space after entering a word in the textCtrl The first english word will also displayed on screen along with the


corresponding malayalam word conversion in the screen. This should be corrected. 2. when I start to enter the second line onwards eventhough it displays letters of malayalam in the editor window when i


press space , the last letters of the previous word will also truncate. This should also be avoided. Please help me to correct


these two problems. I have pasted the full code here. Let me know where it goes wrong in the code . My final aim is to build


a texteditor for malayalam like the notepad. Kindly help me...


import wx
import string
from keymaps import keymap ## load the keymaps
def mreplace(s, prv, nxt):
if prv in s:
s = s.replace(prv, nxt)
return s
else:
return s
def roman2mal(intext):

ans = ''

## formating the input string to a suitable form for internal use!


#intext = mreplace(intext, 'ng', 'M');
intext = mreplace(intext, 'ng', 'G');
#intext = mreplace(intext, 'NG', 'J');
intext = mreplace(intext, 'nj', 'J');

intext = mreplace(intext, '#', '`#');

intext = mreplace(intext, 'b ', 'ba ');
intext = mreplace(intext, 'c ', 'ca ');
intext = mreplace(intext, 'd ', 'da ');
intext = mreplace(intext, 'f ', 'fa ');
intext = mreplace(intext, 'g ', 'ga ');
intext = mreplace(intext, 'h ', 'ha ');
intext = mreplace(intext, 'j ', 'ja ');
intext = mreplace(intext, 'k ', 'ka ');
intext = mreplace(intext, 'K ', 'kha ');
intext = mreplace(intext, 'l ', 'la ');
intext = mreplace(intext, 'm ', 'ma ');
intext = mreplace(intext, 'n ', 'na ');
intext = mreplace(intext, 'p ', 'pa ');
intext = mreplace(intext, 'r ', 'ra ');
intext = mreplace(intext, 's ', 'sa ');
intext = mreplace(intext, 't ', 'ta ');
intext = mreplace(intext, 'v ', 'va ');
intext = mreplace(intext, 'y ', 'ya ');
intext = mreplace(intext, 'L ', 'La ');
intext = mreplace(intext, 'w', 'wa');
intext = mreplace(intext, 'q', 'q');
intext = mreplace(intext, 'V', 'V');

intext = mreplace(intext, 'N~', u'\u0D7A');
intext = mreplace(intext, 'n~', u'\u0D7B');
intext = mreplace(intext, 'r~', u'\u0D7C');
intext = mreplace(intext, 'l~', u'\u0D7D');
intext = mreplace(intext, 'L~', u'\u0D7E');
intext = mreplace(intext, 'M', u'\u0D02');
intext = mreplace(intext, 'H', u'\u0D03');
intext = mreplace(intext, ' ', u'\u0020');

intext = mreplace(intext, '^', u'\u0D4D');
intext = mreplace(intext, 'z ', 'zha');
intext = mreplace(intext, 'R ', 'Ra ');


intext = mreplace(intext, 'D ', 'Da ');
intext = mreplace(intext, 'G ', 'Ga ');
intext = mreplace(intext, 'J ', 'Ja ');
intext = mreplace(intext, 'N ', 'Na ');
intext = mreplace(intext, 'S ', 'Sa ');
intext = mreplace(intext, 'T ', 'Ta ');
intext = mreplace(intext, 'y ', 'ya ');
intext = mreplace(intext, 'X', 'X');
#intext = mreplace(intext, 'V', 'au');


#intext = mreplace(intext, '. ', '.a ');
intext = mreplace(intext, 'wx.WXK_BACK', u'<BACK>');
intext = mreplace(intext, 'wx.WXK_ESCAPE', u'<ESCAPE>');
intext = mreplace(intext, 'wx.WXK_RETURN', u'<RETURN>');




if '|' in intext:
intext = mreplace(intext, 'b|', 'ba|');
intext = mreplace(intext, 'c|', 'ca|');
intext = mreplace(intext, 'd|', 'da|');
intext = mreplace(intext, 'f|', 'fa|');
intext = mreplace(intext, 'g|', 'ga|');
intext = mreplace(intext, 'h|', 'ha|');
intext = mreplace(intext, 'j|', 'ja|');
intext = mreplace(intext, 'k|', 'ka|');
intext = mreplace(intext, 'K|', 'kha|');
intext = mreplace(intext, 'l|', 'la|');
intext = mreplace(intext, 'm|', 'ma|');
intext = mreplace(intext, 'n|', 'na|');
intext = mreplace(intext, 'p|', 'pa|');
intext = mreplace(intext, 'r|', 'ra|');
intext = mreplace(intext, 's|', 'sa|');
intext = mreplace(intext, 't|', 'ta|');
intext = mreplace(intext, 'v|', 'va|');
intext = mreplace(intext, 'Y|', 'Ya|');
intext = mreplace(intext, 'L|', 'La|');
intext = mreplace(intext, 'w|', 'wa|');
intext = mreplace(intext, 'D|', 'Da|');
intext = mreplace(intext, 'G|', 'Ga|');
intext = mreplace(intext, 'J|', 'Ja|');
intext = mreplace(intext, 'N|', 'Na|');
intext = mreplace(intext, 'S|', 'Sa|');
intext = mreplace(intext, 'T|', 'Ta|');
intext = mreplace(intext, 'y|', 'ya|');
intext = mreplace(intext, 'z|', 'zha|');

intext = mreplace(intext, 'n~|', u'\u0D7B|');
intext = mreplace(intext, 'N~|', u'\u0D7A|');
intext = mreplace(intext, 'r~|', u'\u0D7C|');
intext = mreplace(intext, 'l~|', u'\u0D7D|');
intext = mreplace(intext, 'L~|', u'\u0D7E|');
intext = mreplace(intext, '^|', u'\u0D4D|');
intext = mreplace(intext, 'M|', u'\u0D02|');
intext = mreplace(intext, 'H|', u'\u0D03|');
intext = mreplace(intext, 'q|', 'q|');
intext = mreplace(intext, 'V|', 'V|');

intext = mreplace(intext, '.|', '.a|');
intext = mreplace(intext, 'wx.WXK_BACK|', u'<BACK>');
intext = mreplace(intext, 'wx.WXK_ESCAPE|', u'<ESCAPE>');
intext = mreplace(intext, 'wx.WXK_RETURN|', u'<RETURN>');

intext = mreplace(intext, 'aa', 'A');
intext = mreplace(intext, 'ii', 'I');
intext = mreplace(intext, 'ee', 'E');
intext = mreplace(intext, 'uu', 'U');
intext = mreplace(intext, 'ai', 'Y');
intext = mreplace(intext, 'au', 'Q');
intext = mreplace(intext, 'r`', 'x');
intext = mreplace(intext, 'oo', 'O');

intext = mreplace(intext, 'ng', 'G');
intext = mreplace(intext, 'nj', 'J');
intext = mreplace(intext, 'zh', 'z');

intext = mreplace(intext, 'Sh', 'S');
intext = mreplace(intext, 'f', 'ph');
intext = mreplace(intext, '_', '');


## Main conversion to malayalam begins!
i = 0
#global len
intext_len = len(intext)

while i < intext_len:
cur = intext[i]
if i+1 < intext_len:
next = intext[i+1]
else:
next = ''

comb = cur + next

## mainly the 'vowels' list (first 17 are swaram which also acts as cihnam)
list1 = ['a','A','i','I','u','U','x','e','E','Y','o','O','Q','M','H','^']
## mainly the 'consonants' list
list2 =

['k','g','G','c','C','j','J','t','d','n','T','D','N','p','b','m','s','h','r','I','v','R','y','S','l','l~','^','L','w','z','q','V','P','F','K','W','B','X

']


    if cur in list1 or (cur >= '0' and cur <= '9'):
ans += keymap[cur]
i += 1

elif cur in list2:
## more consonants list
list3 = ['kh','gh','ch','jh','th','dh','Th','Dh','ph','bh','sh','ng','nj','zh','C']

if comb in list3:
cur = comb
i +=1
if i+1 < intext_len:
next = intext[i+1]
else:
next = ''

if next in list1[:17]: ## this lists the swarm / cihnam
ans += (keymap[cur] + keymap[next+'cihnam'])
i += 2
#elif#
#elif next != '':
else:
ans += (keymap[cur] + keymap['chandra'])
i += 1
###### trying to remove trailing chandra for words ending with 'a kaar'
#else :
# ans += keymap[cur]
# i += 1

else:
ans += cur
i += 1

print ans
return ans

Here the last print statement will display the Malayalam language equivalent of what I typed on textCtrl.


The textCtrl code is as below. texteditor.py


import wx
import os
import engine
import codecs

window_title = 'Malayalam Language Text Editor'

stockUndo = ['']
stockRedo = []
depth = 10 ## Default Undo-Redo Depth
exit_flag=0

class Beditor(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(800,600))

self.SetIcon(wx.Icon('..\icons\icon.png', wx.BITMAP_TYPE_PNG))

## variables
self.modify = False
self.last_name_saved = ''
self.replace = False
self.word = ''

## setting up the text ctrl

self.text = wx.TextCtrl(self, 1000, '', size=(-1, -1), style=wx.TE_MULTILINE)
self.text.SetFocus()
self.text.SetFont(wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
self.text.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.text.Bind(wx.EVT_CHAR, self.conv)
self.Centre()
self.Show(True)
def OnKeyDown(self, event):
keycode = event.GetKeyCode()


if keycode == wx.WXK_INSERT:
if not self.replace:
self.statusbar.SetStatusText('INS', 2)
self.replace = True
else:
self.statusbar.SetStatusText('', 2)
self.replace = False
if keycode == wx.WXK_RETURN:


self.text.WriteText(' ')

event.Skip()

###### Converting to Malayalam using engine ##########

def conv(self, event):
keycode = event.GetKeyCode()
event.Skip()

if keycode == wx.WXK_SPACE:

key = chr(keycode)
self.word += key
text = self.text.GetRange(0, self.text.GetInsertionPoint())#getting the insertionPoint here
wordlist = text.split(' ')
cur_word = ' ' + wordlist[-1] ## cur_word = current word
sow = text.rfind(' ')
self.text.Replace(sow, self.text.GetInsertionPoint(), engine.roman2mal(cur_word))#.decode

('utf-8')) )

event.Skip()



app = wx.App()
Beditor(None, -1, window_title + '[Untitled]')
app.MainLoop()


I am using python 2.7 for creating a text editor. I am able to create the corresponding Unicode Malayalam(Indian Language) characters but I am not aware how to display it


on text control. I need to work the text Control like a Malayalam Text Editor. The editor should support the space, newline ,


insert characters also..it should work like a note pad. I am pasting the code which I have. 1. engine.py is a mapping file which does all the mapping and at the back-end it will generate the Malayalam letters. All I


required is to get it displayed on screen correctly. the engine.py code and texteditor.py code are as below


The texteditor.py is the file which has to be executed. conv(self, event) is the function which performs the conversion to


Malayalam. The issues I am facing here are 1. when I press space after entering a word in the textCtrl The first english word will also displayed on screen along with the


corresponding malayalam word conversion in the screen. This should be corrected. 2. when I start to enter the second line onwards eventhough it displays letters of malayalam in the editor window when i


press space , the last letters of the previous word will also truncate. This should also be avoided. Please help me to correct


these two problems. I have pasted the full code here. Let me know where it goes wrong in the code . My final aim is to build


a texteditor for malayalam like the notepad. Kindly help me...


import wx
import string
from keymaps import keymap ## load the keymaps
def mreplace(s, prv, nxt):
if prv in s:
s = s.replace(prv, nxt)
return s
else:
return s
def roman2mal(intext):

ans = ''

## formating the input string to a suitable form for internal use!


#intext = mreplace(intext, 'ng', 'M');
intext = mreplace(intext, 'ng', 'G');
#intext = mreplace(intext, 'NG', 'J');
intext = mreplace(intext, 'nj', 'J');

intext = mreplace(intext, '#', '`#');

intext = mreplace(intext, 'b ', 'ba ');
intext = mreplace(intext, 'c ', 'ca ');
intext = mreplace(intext, 'd ', 'da ');
intext = mreplace(intext, 'f ', 'fa ');
intext = mreplace(intext, 'g ', 'ga ');
intext = mreplace(intext, 'h ', 'ha ');
intext = mreplace(intext, 'j ', 'ja ');
intext = mreplace(intext, 'k ', 'ka ');
intext = mreplace(intext, 'K ', 'kha ');
intext = mreplace(intext, 'l ', 'la ');
intext = mreplace(intext, 'm ', 'ma ');
intext = mreplace(intext, 'n ', 'na ');
intext = mreplace(intext, 'p ', 'pa ');
intext = mreplace(intext, 'r ', 'ra ');
intext = mreplace(intext, 's ', 'sa ');
intext = mreplace(intext, 't ', 'ta ');
intext = mreplace(intext, 'v ', 'va ');
intext = mreplace(intext, 'y ', 'ya ');
intext = mreplace(intext, 'L ', 'La ');
intext = mreplace(intext, 'w', 'wa');
intext = mreplace(intext, 'q', 'q');
intext = mreplace(intext, 'V', 'V');

intext = mreplace(intext, 'N~', u'\u0D7A');
intext = mreplace(intext, 'n~', u'\u0D7B');
intext = mreplace(intext, 'r~', u'\u0D7C');
intext = mreplace(intext, 'l~', u'\u0D7D');
intext = mreplace(intext, 'L~', u'\u0D7E');
intext = mreplace(intext, 'M', u'\u0D02');
intext = mreplace(intext, 'H', u'\u0D03');
intext = mreplace(intext, ' ', u'\u0020');

intext = mreplace(intext, '^', u'\u0D4D');
intext = mreplace(intext, 'z ', 'zha');
intext = mreplace(intext, 'R ', 'Ra ');


intext = mreplace(intext, 'D ', 'Da ');
intext = mreplace(intext, 'G ', 'Ga ');
intext = mreplace(intext, 'J ', 'Ja ');
intext = mreplace(intext, 'N ', 'Na ');
intext = mreplace(intext, 'S ', 'Sa ');
intext = mreplace(intext, 'T ', 'Ta ');
intext = mreplace(intext, 'y ', 'ya ');
intext = mreplace(intext, 'X', 'X');
#intext = mreplace(intext, 'V', 'au');


#intext = mreplace(intext, '. ', '.a ');
intext = mreplace(intext, 'wx.WXK_BACK', u'<BACK>');
intext = mreplace(intext, 'wx.WXK_ESCAPE', u'<ESCAPE>');
intext = mreplace(intext, 'wx.WXK_RETURN', u'<RETURN>');




if '|' in intext:
intext = mreplace(intext, 'b|', 'ba|');
intext = mreplace(intext, 'c|', 'ca|');
intext = mreplace(intext, 'd|', 'da|');
intext = mreplace(intext, 'f|', 'fa|');
intext = mreplace(intext, 'g|', 'ga|');
intext = mreplace(intext, 'h|', 'ha|');
intext = mreplace(intext, 'j|', 'ja|');
intext = mreplace(intext, 'k|', 'ka|');
intext = mreplace(intext, 'K|', 'kha|');
intext = mreplace(intext, 'l|', 'la|');
intext = mreplace(intext, 'm|', 'ma|');
intext = mreplace(intext, 'n|', 'na|');
intext = mreplace(intext, 'p|', 'pa|');
intext = mreplace(intext, 'r|', 'ra|');
intext = mreplace(intext, 's|', 'sa|');
intext = mreplace(intext, 't|', 'ta|');
intext = mreplace(intext, 'v|', 'va|');
intext = mreplace(intext, 'Y|', 'Ya|');
intext = mreplace(intext, 'L|', 'La|');
intext = mreplace(intext, 'w|', 'wa|');
intext = mreplace(intext, 'D|', 'Da|');
intext = mreplace(intext, 'G|', 'Ga|');
intext = mreplace(intext, 'J|', 'Ja|');
intext = mreplace(intext, 'N|', 'Na|');
intext = mreplace(intext, 'S|', 'Sa|');
intext = mreplace(intext, 'T|', 'Ta|');
intext = mreplace(intext, 'y|', 'ya|');
intext = mreplace(intext, 'z|', 'zha|');

intext = mreplace(intext, 'n~|', u'\u0D7B|');
intext = mreplace(intext, 'N~|', u'\u0D7A|');
intext = mreplace(intext, 'r~|', u'\u0D7C|');
intext = mreplace(intext, 'l~|', u'\u0D7D|');
intext = mreplace(intext, 'L~|', u'\u0D7E|');
intext = mreplace(intext, '^|', u'\u0D4D|');
intext = mreplace(intext, 'M|', u'\u0D02|');
intext = mreplace(intext, 'H|', u'\u0D03|');
intext = mreplace(intext, 'q|', 'q|');
intext = mreplace(intext, 'V|', 'V|');

intext = mreplace(intext, '.|', '.a|');
intext = mreplace(intext, 'wx.WXK_BACK|', u'<BACK>');
intext = mreplace(intext, 'wx.WXK_ESCAPE|', u'<ESCAPE>');
intext = mreplace(intext, 'wx.WXK_RETURN|', u'<RETURN>');

intext = mreplace(intext, 'aa', 'A');
intext = mreplace(intext, 'ii', 'I');
intext = mreplace(intext, 'ee', 'E');
intext = mreplace(intext, 'uu', 'U');
intext = mreplace(intext, 'ai', 'Y');
intext = mreplace(intext, 'au', 'Q');
intext = mreplace(intext, 'r`', 'x');
intext = mreplace(intext, 'oo', 'O');

intext = mreplace(intext, 'ng', 'G');
intext = mreplace(intext, 'nj', 'J');
intext = mreplace(intext, 'zh', 'z');

intext = mreplace(intext, 'Sh', 'S');
intext = mreplace(intext, 'f', 'ph');
intext = mreplace(intext, '_', '');


## Main conversion to malayalam begins!
i = 0
#global len
intext_len = len(intext)

while i < intext_len:
cur = intext[i]
if i+1 < intext_len:
next = intext[i+1]
else:
next = ''

comb = cur + next

## mainly the 'vowels' list (first 17 are swaram which also acts as cihnam)
list1 = ['a','A','i','I','u','U','x','e','E','Y','o','O','Q','M','H','^']
## mainly the 'consonants' list
list2 =

['k','g','G','c','C','j','J','t','d','n','T','D','N','p','b','m','s','h','r','I','v','R','y','S','l','l~','^','L','w','z','q','V','P','F','K','W','B','X

']


    if cur in list1 or (cur >= '0' and cur <= '9'):
ans += keymap[cur]
i += 1

elif cur in list2:
## more consonants list
list3 = ['kh','gh','ch','jh','th','dh','Th','Dh','ph','bh','sh','ng','nj','zh','C']

if comb in list3:
cur = comb
i +=1
if i+1 < intext_len:
next = intext[i+1]
else:
next = ''

if next in list1[:17]: ## this lists the swarm / cihnam
ans += (keymap[cur] + keymap[next+'cihnam'])
i += 2
#elif#
#elif next != '':
else:
ans += (keymap[cur] + keymap['chandra'])
i += 1
###### trying to remove trailing chandra for words ending with 'a kaar'
#else :
# ans += keymap[cur]
# i += 1

else:
ans += cur
i += 1

print ans
return ans

Here the last print statement will display the Malayalam language equivalent of what I typed on textCtrl.


The textCtrl code is as below. texteditor.py


import wx
import os
import engine
import codecs

window_title = 'Malayalam Language Text Editor'

stockUndo = ['']
stockRedo = []
depth = 10 ## Default Undo-Redo Depth
exit_flag=0

class Beditor(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(800,600))

self.SetIcon(wx.Icon('..\icons\icon.png', wx.BITMAP_TYPE_PNG))

## variables
self.modify = False
self.last_name_saved = ''
self.replace = False
self.word = ''

## setting up the text ctrl

self.text = wx.TextCtrl(self, 1000, '', size=(-1, -1), style=wx.TE_MULTILINE)
self.text.SetFocus()
self.text.SetFont(wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
self.text.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.text.Bind(wx.EVT_CHAR, self.conv)
self.Centre()
self.Show(True)
def OnKeyDown(self, event):
keycode = event.GetKeyCode()


if keycode == wx.WXK_INSERT:
if not self.replace:
self.statusbar.SetStatusText('INS', 2)
self.replace = True
else:
self.statusbar.SetStatusText('', 2)
self.replace = False
if keycode == wx.WXK_RETURN:


self.text.WriteText(' ')

event.Skip()

###### Converting to Malayalam using engine ##########

def conv(self, event):
keycode = event.GetKeyCode()
event.Skip()

if keycode == wx.WXK_SPACE:

key = chr(keycode)
self.word += key
text = self.text.GetRange(0, self.text.GetInsertionPoint())#getting the insertionPoint here
wordlist = text.split(' ')
cur_word = ' ' + wordlist[-1] ## cur_word = current word
sow = text.rfind(' ')
self.text.Replace(sow, self.text.GetInsertionPoint(), engine.roman2mal(cur_word))#.decode

('utf-8')) )

event.Skip()



app = wx.App()
Beditor(None, -1, window_title + '[Untitled]')
app.MainLoop()

0 commentaires:

Enregistrer un commentaire