{"id":122,"date":"2006-03-22T00:56:08","date_gmt":"2006-03-21T17:56:08","guid":{"rendered":"http:\/\/lewcpe.com\/blog\/archives\/122"},"modified":"2006-03-22T01:14:02","modified_gmt":"2006-03-21T18:14:02","slug":"bug-2","status":"publish","type":"post","link":"https:\/\/lewcpe.com\/blog\/archives\/122\/bug-2\/","title":{"rendered":"Bug"},"content":{"rendered":"<p>As I&#8217;m working in my personal project about Thai language. I try to learn GUI programming with wxPython anf face some problem in following code.<\/p>\n<p>[ftf w=&#8221;400&#8243; h=&#8221;300&#8243; def=&#8221;python.xml&#8221;]#!\/usr\/bin\/env python<br \/>\n# -*- coding: utf-8 -*-<br \/>\n# generated by wxGlade 0.4 on Tue Mar 21 20:06:06 2006<\/p>\n<p>import wx<br \/>\nimport codecs<\/p>\n<p>class MainFrame(wx.Frame):<br \/>\n    def __init__(self, *args, **kwds):<br \/>\n        # begin wxGlade: MainFrame.__init__<br \/>\n        kwds[&#8220;style&#8221;] = wx.DEFAULT_FRAME_STYLE<br \/>\n        wx.Frame.__init__(self, *args, **kwds)<\/p>\n<p>        # Tool Bar<br \/>\n        self.wdMainFrameToolBar = wx.ToolBar(self, -1)<br \/>\n        self.SetToolBar(self.wdMainFrameToolBar)<br \/>\n        self.wdMainFrameToolBar.AddLabelTool(wx.NewId(), &#8220;Auto&#8221;, wx.Bitmap(&#8220;C:\\\\Documents and Settings\\\\Wason Lewlompaisal\\\\My Documents\\\\My Download\\\\more-smilies\\\\tango\\\\dialog-information.png&#8221;, wx.BITMAP_TYPE_ANY), wx.NullBitmap, wx.ITEM_CHECK, &#8220;Break Thai Word Automatic&#8221;, &#8220;&#8221;)<br \/>\n        self.wdMainFrameToolBar.AddLabelTool(wx.NewId(), &#8220;ShowZWSP&#8221;, wx.Bitmap(&#8220;C:\\\\Documents and Settings\\\\Wason Lewlompaisal\\\\My Documents\\\\My Download\\\\more-smilies\\\\tango\\\\applications-multimedia.png&#8221;, wx.BITMAP_TYPE_ANY), wx.NullBitmap, wx.ITEM_CHECK, &#8220;Show Zero Width Space in Text Box&#8221;, &#8220;&#8221;)<br \/>\n        # Tool Bar end<br \/>\n        self.SingleTextControl = wx.TextCtrl(self, -1, &#8220;&#8221;, style=wx.TE_MULTILINE)<\/p>\n<p>        self.__set_properties()<br \/>\n        self.__do_layout()<\/p>\n<p>        self.Bind(wx.EVT_TOOL, self.OnAutoWordBreak, id=-1)<br \/>\n        self.Bind(wx.EVT_TOOL, self.OnShowZWSP, id=-1)<br \/>\n        self.Bind(wx.EVT_TEXT, self.OnTextChange, self.SingleTextControl)<br \/>\n        # end wxGlade<\/p>\n<p>    def __set_properties(self):<br \/>\n        # begin wxGlade: MainFrame.__set_properties<br \/>\n        self.SetTitle(&#8220;Thai Input Demo&#8221;)<br \/>\n        self.SetSize((400, 300))<br \/>\n        self.wdMainFrameToolBar.Realize()<br \/>\n        # end wxGlade<\/p>\n<p>    def __do_layout(self):<br \/>\n        # begin wxGlade: MainFrame.__do_layout<br \/>\n        MainGridSizer = wx.GridSizer(1, 1, 0, 0)<br \/>\n        MainGridSizer.Add(self.SingleTextControl, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)<br \/>\n        self.SetAutoLayout(True)<br \/>\n        self.SetSizer(MainGridSizer)<br \/>\n        self.Layout()<br \/>\n        # end wxGlade<\/p>\n<p>    def OnAutoWordBreak(self, event): # wxGlade: MainFrame.<event_handler><br \/>\n        print &#8220;Event handler `OnAutoWordBreak&#8217; not implemented!&#8221;<br \/>\n        allText = self.SingleTextControl.GetValue()<br \/>\n        print &#8220;ALLTEXT>>&#8221;, repr(allText)<br \/>\n        event.Skip()<\/p>\n<p>    def OnShowZWSP(self, event): # wxGlade: MainFrame.<\/event_handler><event_handler><br \/>\n        print &#8220;Event handler `OnShowZWSP&#8217; not implemented!&#8221;<br \/>\n        event.Skip()<\/p>\n<p>    def OnTextChange(self, event): # wxGlade: MainFrame.<\/event_handler><event_handler><br \/>\n        sentence = self.ExtractCurrentSentence()<br \/>\n        print &#8220;Current Sentence::&#8221;, sentence.encode(&#8220;utf-8&#8221;)<\/p>\n<p>    def ExtractCurrentSentence(self):<br \/>\n        allText = self.SingleTextControl.GetValue()<br \/>\n        print &#8220;ALLTEXT>>&#8221;, repr(allText)<br \/>\n        curPos  = self.SingleTextControl.GetLastPosition()<br \/>\n        sentenceList = allText.split(u&#8221; &#8220;)<br \/>\n        for word in sentenceList:<br \/>\n            print word.encode(&#8220;utf-8&#8221;), &#8220;==>&#8221;, len(word)<br \/>\n        runPos = 0;<br \/>\n        curSentence = 0;<br \/>\n        while (True):<br \/>\n            if (len(sentenceList[curSentence]) + runPos > curPos &#8211; 1):<br \/>\n                break<br \/>\n            runPos += len(sentenceList[curSentence]) + 1<br \/>\n            curSentence += 1<br \/>\n        return sentenceList[curSentence]<\/p>\n<p># end of class MainFrame<\/p>\n<p>class ThaiBreakApp(wx.App):<br \/>\n    def OnInit(self):<br \/>\n        wx.InitAllImageHandlers()<br \/>\n        wdMainFrame = MainFrame(None, -1, &#8220;&#8221;)<br \/>\n        self.SetTopWindow(wdMainFrame)<br \/>\n        wdMainFrame.Show()<br \/>\n        return 1<\/p>\n<p># end of class ThaiBreakApp<\/p>\n<p>if __name__ == &#8220;__main__&#8221;:<br \/>\n    app = ThaiBreakApp(0)<br \/>\n    app.MainLoop()<br \/>\n[\/ftf]<\/p>\n<p>Program seems to be fine at start but when some of That character appear in the TextCtrl the EVT_TEXT which call function OnTextChange just stop working, all thing back to work properly again when all those character removed.<\/p>\n<p>After hours of debugging and searching for information about unicode bug in wxWindows, I noticed that there&#8217;s python exception in SPE&#8217;s shell. This bug is nothing but SPE&#8217;s coding without aware of non-latin chracter.<\/p>\n<p>I&#8217;m going to happily sleep now.<\/event_handler><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I&#8217;m working in my personal project about Thai language. I try to learn GUI programming with wxPython anf face some problem in following code. [ftf w=&#8221;400&#8243; h=&#8221;300&#8243; def=&#8221;python.xml&#8221;]#!\/usr\/bin\/env python # -*- coding: utf-8 -*- # generated by wxGlade 0.4 on Tue Mar 21 20:06:06 2006 import wx import codecs class MainFrame(wx.Frame): def __init__(self, *args, <a class=\"read-more\" href=\"https:\/\/lewcpe.com\/blog\/archives\/122\/bug-2\/\">&hellip;&nbsp;<span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[10,43,45],"_links":{"self":[{"href":"https:\/\/lewcpe.com\/blog\/wp-json\/wp\/v2\/posts\/122"}],"collection":[{"href":"https:\/\/lewcpe.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lewcpe.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lewcpe.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lewcpe.com\/blog\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":0,"href":"https:\/\/lewcpe.com\/blog\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"wp:attachment":[{"href":"https:\/\/lewcpe.com\/blog\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lewcpe.com\/blog\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lewcpe.com\/blog\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}