wx.txtctrl中的Unicode实时输入问题

时间:2018-11-14 20:48:34

标签: unicode wxpython

我的问题是,当我在文本控件中输入法语口音时,使用此代码会出现错误消息

   WARNING **: Error converting text from IM to UTF-8: Invalid byte sequence in conversion input

我尝试将这些行包括在内但没有成功:

      font=wx.Font(17, wx.FONTFAMILY_DEFAULT, wx.NORMAL,wx.FONTWEIGHT_NORMAL,False, encoding=wx.FONTENCODING_UTF8)
      self.txt = wx.TextCtrl(self, style=wx.TE_MULTILINE )
      self.txt.SetFont(font)

我该怎么做才能在控制文本框中输入unicode法语口音?

import wx

import codecs
class DemoFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title="Demo Unicode")
        self.txt = wx.TextCtrl(self, style=wx.TE_MULTILINE )
        self.save = wx.Button(self, label="Save")
        self.load = wx.Button(self, label="Load")
        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(self.txt, 1, wx.EXPAND|wx.ALL, 5)
        box.Add(self.save, 0, wx.ALIGN_CENTER|wx.ALL, 10)
        box.Add(self.load, 0, wx.ALIGN_CENTER|wx.ALL, 10)
        self.SetSizer(box)
        self.save.Bind(wx.EVT_BUTTON, self.OnSave)
        self.load.Bind (wx.EVT_BUTTON, self.OnLoad)

    def OnSave(self, evt):
        f = codecs.open("demo.txt", "w", 'utf-8')
        f.write(self.txt.GetValue())
        f.close()
    def OnLoad(self, evt):
        f = codecs.open("demo.txt", "r", 'utf-8')
        self.txt.SetValue(f.read())
        f.close()

if __name__ == "__main__":
    app = wx.App(0)
    frame = DemoFrame()
    frame.Show()
    app.MainLoop()

0 个答案:

没有答案