wxpython

Reply

Join Date: Feb 2008
Posts: 5
Reputation: nikoasumi is an unknown quantity at this point 
Solved Threads: 0
nikoasumi nikoasumi is offline Offline
Newbie Poster

wxpython

 
0
  #1
Feb 21st, 2008
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
Last edited by nikoasumi; Feb 21st, 2008 at 8:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: wxpython

 
0
  #2
Feb 22nd, 2008
Hi nioasumi,

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

Jeff
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: nikoasumi is an unknown quantity at this point 
Solved Threads: 0
nikoasumi nikoasumi is offline Offline
Newbie Poster

Re: wxpython

 
0
  #3
Feb 23rd, 2008
Hi Jeff,

This is what I have. Its really simple and comes from Boa.
EditorApp.py
  1. #!/usr/bin/env python
  2. #Boa:App:BoaApp
  3.  
  4. import wx
  5.  
  6. import Editor
  7.  
  8. modules ={u'Editor': [1, 'Main frame of Application', u'Editor.py']}
  9.  
  10. class BoaApp(wx.App):
  11. def OnInit(self):
  12. self.main = Editor.create(None)
  13. self.main.Show()
  14. self.SetTopWindow(self.main)
  15. return True
  16.  
  17. def main():
  18. application = BoaApp(0)
  19. application.MainLoop()
  20.  
  21. if __name__ == '__main__':
  22. main()

Editor.py
  1. #Boa:Frame:Frame1
  2.  
  3. import wx
  4. import wx.stc
  5.  
  6. def create(parent):
  7. return Frame1(parent)
  8.  
  9. [wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1STYLEDTEXTCTRL1,
  10. ] = [wx.NewId() for _init_ctrls in range(3)]
  11.  
  12. class Frame1(wx.Frame):
  13. def _init_ctrls(self, prnt):
  14. # generated method, don't edit
  15. wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
  16. pos=wx.Point(416, 165), size=wx.Size(400, 539),
  17. style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
  18. self.SetClientSize(wx.Size(392, 505))
  19.  
  20. self.styledTextCtrl1 = wx.stc.StyledTextCtrl(id=wxID_FRAME1STYLEDTEXTCTRL1,
  21. name='styledTextCtrl1', parent=self, pos=wx.Point(-8, 0),
  22. size=wx.Size(392, 464), style=0)
  23. self.styledTextCtrl1.SetLexer(wx.stc.STC_LEX_SQL)
  24.  
  25. self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label='OK',
  26. name='button1', parent=self, pos=wx.Point(304, 472),
  27. size=wx.Size(75, 23), style=0)
  28. self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
  29. id=wxID_FRAME1BUTTON1)
  30.  
  31. def __init__(self, parent):
  32. self._init_ctrls(parent)
  33.  
  34. def OnButton1Button(self, event):
  35. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: wxpython

 
0
  #4
Feb 23rd, 2008
Just a thought here. The wxpython binary comes in two flavors: unicode and ANSI. Which one do you have installed?

Jeff
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: nikoasumi is an unknown quantity at this point 
Solved Threads: 0
nikoasumi nikoasumi is offline Offline
Newbie Poster

Re: wxpython

 
0
  #5
Feb 23rd, 2008
I installed unicode version. wxpython2.5 unicode if that helps.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: wxpython

 
0
  #6
Feb 23rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: wxpython

 
0
  #7
Feb 23rd, 2008
Well, I've improved it a bit. The change is in the Button1 callback:

  1. #Boa:Frame:Frame1
  2.  
  3. import wx
  4. import wx.stc
  5.  
  6. def create(parent):
  7. return Frame1(parent)
  8.  
  9. [wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1STYLEDTEXTCTRL1,
  10. ] = [wx.NewId() for _init_ctrls in range(3)]
  11.  
  12. class Frame1(wx.Frame):
  13. def _init_ctrls(self, prnt):
  14. # generated method, don't edit
  15. wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
  16. pos=wx.Point(416, 165), size=wx.Size(400, 539),
  17. style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
  18. self.SetClientSize(wx.Size(392, 505))
  19.  
  20. self.styledTextCtrl1 = wx.stc.StyledTextCtrl(id=wxID_FRAME1STYLEDTEXTCTRL1,
  21. name='styledTextCtrl1', parent=self, pos=wx.Point(-8, 0),
  22. size=wx.Size(392, 464), style=0)
  23. self.styledTextCtrl1.SetLexer(wx.stc.STC_LEX_SQL)
  24.  
  25. self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label='OK',
  26. name='button1', parent=self, pos=wx.Point(304, 472),
  27. size=wx.Size(75, 23), style=0)
  28. self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
  29. id=wxID_FRAME1BUTTON1)
  30.  
  31. def __init__(self, parent):
  32. self._init_ctrls(parent)
  33.  
  34. def OnButton1Button(self, event): # <<< here!
  35. f = open("test.gs", "w")
  36. text = self.styledTextCtrl1.GetTextUTF8()
  37. #self.styledTextCtrl1.GetTextRaw() makes no difference
  38. f.write(text)
  39. f.close()
  40. #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
Last edited by jrcagle; Feb 23rd, 2008 at 8:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: nikoasumi is an unknown quantity at this point 
Solved Threads: 0
nikoasumi nikoasumi is offline Offline
Newbie Poster

Re: wxpython

 
0
  #8
Feb 24th, 2008
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.
  1. def OnButton2Button(self, event):
  2. dlg = wx.FileDialog(self, 'Choose a file', '.', '', '*.*', wx.OPEN)
  3. try:
  4. if dlg.ShowModal() == wx.ID_OK:
  5. filename = dlg.GetPath()
  6. # Your code
  7. self.styledTextCtrl1.LoadFile(filename)
  8. self.FileName=filename
  9. self.SetTitle(('Notebook -%s') % filename)
  10. finally:
  11. dlg.Destroy()
Last edited by nikoasumi; Feb 24th, 2008 at 6:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: nikoasumi is an unknown quantity at this point 
Solved Threads: 0
nikoasumi nikoasumi is offline Offline
Newbie Poster

Re: wxpython

 
0
  #9
Feb 24th, 2008
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
  1.  
  2. import codecs
  1. def OnButton1Button(self, event): # <<< here!
  2. f = codecs.open("test.gs", encoding='utf-8', mode="w")
  3. #text = self.styledTextCtrl1.GetTextUTF8()
  4. #self.styledTextCtrl1.GetTextRaw() makes no difference
  5.  
  6. f.write(self.styledTextCtrl1.GetText())
  7. #f.seek(0)
  8. f.close()
  9. #self.styledTextCtrl1.SaveFile('test.gs')
  10.  
  11. def OnButton2Button(self, event):
  12. dlg = wx.FileDialog(self, 'Choose a file', '.', '', '*.*', wx.OPEN)
  13. try:
  14. if dlg.ShowModal() == wx.ID_OK:
  15. filename = dlg.GetPath()
  16. # Your code
  17. #self.styledTextCtrl1.LoadFile(filename)
  18.  
  19. f = codecs.open(filename, encoding='utf-8', mode="r")
  20. for line in f:
  21. self.styledTextCtrl1.AppendText(line)
  22. finally:
  23. 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC