Hi,
I am new to python and new to this forum. So how are you all?

What I am trying to do is create a script editor, with syntax highlight and unicdoe support. It doesnt need to link to ANY compiler, it simply have to display my script with highlights, able to save and load. In other words, a upgrade notepad so to speak. It 'seeems' its not too complicated rite? or am I in over my head in this?

I am using Boa Constructor, and tried the textctrl and styledtextctrl. But ran into some trouble.
1. Textctrl is everything I wanted, it can save/load unicode, but it doesnt have syntax highlighted. Maybe someone have another idea for this?
2. styledtxtctrl, it SEEMS it be able to have syntax highlight, but it cant save or load unicode, and I dont think i can type in unicode in it either. Did I do something wrong?

Hope some experts out here can give me some pointers. It only a simple script editor, shouldnt be too hard rite? lol

Thanks a lot

Hi nioasumi,

Fine, thanks. The best way to help is to see the offending code. Just post it here, surrounded by [ code ] tags.

Jeff

Hi Jeff,

This is what I have. Its really simple and comes from Boa.
EditorApp.py

#!/usr/bin/env python
#Boa:App:BoaApp

import wx

import Editor

modules ={u'Editor': [1, 'Main frame of Application', u'Editor.py']}

class BoaApp(wx.App):
    def OnInit(self):
        self.main = Editor.create(None)
        self.main.Show()
        self.SetTopWindow(self.main)
        return True

def main():
    application = BoaApp(0)
    application.MainLoop()

if __name__ == '__main__':
    main()

Editor.py

#Boa:Frame:Frame1

import wx
import wx.stc

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1STYLEDTEXTCTRL1, 
] = [wx.NewId() for _init_ctrls in range(3)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(416, 165), size=wx.Size(400, 539),
              style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
        self.SetClientSize(wx.Size(392, 505))

        self.styledTextCtrl1 = wx.stc.StyledTextCtrl(id=wxID_FRAME1STYLEDTEXTCTRL1,
              name='styledTextCtrl1', parent=self, pos=wx.Point(-8, 0),
              size=wx.Size(392, 464), style=0)
        self.styledTextCtrl1.SetLexer(wx.stc.STC_LEX_SQL)

        self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label='OK',
              name='button1', parent=self, pos=wx.Point(304, 472),
              size=wx.Size(75, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
              id=wxID_FRAME1BUTTON1)

    def __init__(self, parent):
        self._init_ctrls(parent)

    def OnButton1Button(self, event):
        self.styledTextCtrl1.SaveFile('test.gs')

The biggest problem is this. In the line
self.styledTextCtrl1.SaveFile('test.gs')

Whenever I run it, and input some text like "abc" into the styled text control and hit the button1 to save, it saves fine. However, if I input some unicode text like 你好, and hit the save button, the file is generated, but nothing is saved. Entire file is blank.

I also cant seem to get line numbers to show on the sidelines.

As you can see, I am very new to python. I tried seraching, but nothing came up. Either I dunno what I should be seraching for (whcih i dont) or I did something wrong. This is the first time I tackle such a complex language, so any help would be greatly appreciated.

Thank you for your time.

Just a thought here. The wxpython binary comes in two flavors: unicode and ANSI. Which one do you have installed?

Jeff

I installed unicode version. wxpython2.5 unicode if that helps.

OK, I've replicated your bug. I'll see what I can figure out.

Here's the basic hypothesis: either

(a) the window itself is not reporting out the unicode text found within it, or
(b) self.styledTextCtrl1.SaveFile('test.gs') is not behaving properly.

Jeff

Well, I've improved it a bit. The change is in the Button1 callback:

#Boa:Frame:Frame1

import wx
import wx.stc

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1STYLEDTEXTCTRL1, 
] = [wx.NewId() for _init_ctrls in range(3)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(416, 165), size=wx.Size(400, 539),
              style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
        self.SetClientSize(wx.Size(392, 505))

        self.styledTextCtrl1 = wx.stc.StyledTextCtrl(id=wxID_FRAME1STYLEDTEXTCTRL1,
              name='styledTextCtrl1', parent=self, pos=wx.Point(-8, 0),
              size=wx.Size(392, 464), style=0)
        self.styledTextCtrl1.SetLexer(wx.stc.STC_LEX_SQL)

        self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label='OK',
              name='button1', parent=self, pos=wx.Point(304, 472),
              size=wx.Size(75, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
              id=wxID_FRAME1BUTTON1)

    def __init__(self, parent):
        self._init_ctrls(parent)

    def OnButton1Button(self, event):  # <<< here!
        f = open("test.gs", "w")
        text = self.styledTextCtrl1.GetTextUTF8()
        #self.styledTextCtrl1.GetTextRaw() makes no difference
        f.write(text)
        f.close()
        #self.styledTextCtrl1.SaveFile('test.gs')

This will at least save the text, but the encoding appears to be incorrect. I'm not an encoding expert, so maybe someone can take it from here?

Essentially, I believe that SaveFile was (incorrectly!) calling GetText() instead of GetTextUTF8(). As a result, it wasn't getting anything.

Jeff

hmm would it be possible to call the save function from textctrl from styledtextctrl? Because textctrl works perfectly in saving unicode files.

Just a thought, dunno if its possible or not.

Also, how does one activate line number display on the styledtextcontrol? couldnt find it under boa.

Thanks

EDIT:
This is REALLY weird. I input some uncode text. 廣東話 for example. If I save with just one line (just 廣東話), its encoded all weird. HOWEVER, if I press enter, next line, and save. It works FINE! I have no clue what happen. Could someone explain what just happened here?

EDIT2:
I now tried to Load a unicode File into it. And it seems to mess up too. I added a button 2 to the form just to test it out. It seems to acting weird too.

def OnButton2Button(self, event):
        dlg = wx.FileDialog(self, 'Choose a file', '.', '', '*.*', wx.OPEN)
        try:
            if dlg.ShowModal() == wx.ID_OK:
                filename = dlg.GetPath()
                # Your code
                self.styledTextCtrl1.LoadFile(filename)
                self.FileName=filename
                self.SetTitle(('Notebook -%s') % filename)
        finally:
            dlg.Destroy()

Hmm Ok i THINK i solved it. I THINK. lol Thank you for your help Jeff.

I also have to import codecs to make it work

import codecs
def OnButton1Button(self, event):  # <<< here!
        f = codecs.open("test.gs", encoding='utf-8', mode="w")
        #text = self.styledTextCtrl1.GetTextUTF8()
        #self.styledTextCtrl1.GetTextRaw() makes no difference
        
        f.write(self.styledTextCtrl1.GetText())
        #f.seek(0)
        f.close()
        #self.styledTextCtrl1.SaveFile('test.gs')

    def OnButton2Button(self, event):
        dlg = wx.FileDialog(self, 'Choose a file', '.', '', '*.*', wx.OPEN)
        try:
            if dlg.ShowModal() == wx.ID_OK:
                filename = dlg.GetPath()
                # Your code
                #self.styledTextCtrl1.LoadFile(filename)
                
                f = codecs.open(filename, encoding='utf-8', mode="r")
                for line in f:
                    self.styledTextCtrl1.AppendText(line)
        finally:
            dlg.Destroy()

It seems to work ok, does it seem to have any problems?

Now this is out of the way......all I need would be, syntax highlight and line numbers. Again, I search endlessly, and no prevail. Dun even know where to start on this part. How do I do highlights and line numbers?

Thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.