| | |
wxpython
![]() |
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
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
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.
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
Hi Jeff,
This is what I have. Its really simple and comes from Boa.
EditorApp.py
Editor.py
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.
This is what I have. Its really simple and comes from Boa.
EditorApp.py
Python Syntax (Toggle Plain Text)
#!/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
Python Syntax (Toggle Plain Text)
#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.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
Well, I've improved it a bit. The change is in the Button1 callback:
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
Python Syntax (Toggle Plain Text)
#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
Last edited by jrcagle; Feb 23rd, 2008 at 8:57 pm.
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
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.
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.
Python Syntax (Toggle Plain Text)
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()
Last edited by nikoasumi; Feb 24th, 2008 at 6:03 pm.
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
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
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
I also have to import codecs to make it work
Python Syntax (Toggle Plain Text)
import codecs
Python Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- Instal of wxPython (Python)
- no WxPython here? (Python)
Other Threads in the Python Forum
- Previous Thread: Need help incrementing...
- Next Thread: A little help tweaking the my code
| Thread Tools | Search this Thread |
accessdenied apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysql mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple smtp software sprite statictext string strings syntax table tennis terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython





