One line start command.

I have a simple text editor with a autocomplete of some words. I am having problems getting it to open with a one line command from anoter file, like VisualWX. I have worked on this problem for a week now. there is some lesson I need to learn better.

I have a sample file. it will not start though.

import wx
import wx.stc as stc
import re

AutoFillStartLen=2
AutoFillMinWordLen=4
choices=

class MySTC(stc.StyledTextCtrl):
def init(self, parent, size=wx.DefaultSize, style=0,menu=None):
text=stc.StyledTextCtrl.init(self, parent, size=size, style=style)
self.SetMarginWidth(1,0)
choices.sort()
self.MyChoices=’ '.join(choices)
self.Bind(stc.EVT_STC_CHARADDED , self.OnAutoFill)
self.Bind( wx.EVT_KEY_DOWN , self.OnKeyDown, self )
self.Bind(wx.EVT_CONTEXT_MENU,self.MainPopUp)

def PopUpSwitch(self,event):

menu=wx.Menu()
print menu.IsEnabled(event)

def MainPopUp(self,event):
menu = wx.Menu()
menu.Append(wx.ID_UNDO,“&Undo”)
menu.Append(wx.ID_REDO,‘Redo’)
menu.AppendSeparator()
menu.Append(wx.ID_CUT,“Cu&t” )
menu.Append(wx.ID_COPY, “Copy”)
menu.Append(wx.ID_PASTE,“&Paste”)
menu.Append(wx.ID_DELETE,“&Delete”)
menu.AppendSeparator()
menu.Append(wx.ID_SELECTALL,‘Select All’)
menu.AppendSeparator()
menu.Append(4,
“RemoveWord”)
self.Bind(wx.EVT_MENU, self.OnUndo,id=wx.ID_UNDO)
self.Bind(wx.EVT_MENU, self.OnRedo,id=wx.ID_REDO)
self.Bind(wx.EVT_MENU, self.OnCut,id=wx.ID_CUT)
self.Bind(wx.EVT_MENU, self.OnCopy,id=wx.ID_COPY)
self.Bind(wx.EVT_MENU, self.OnPaste, id=wx.ID_PASTE)
self.Bind(wx.EVT_MENU, self.OnDelete, id=wx.ID_DELETE)
self.Bind(wx.EVT_MENU, self.OnSelectAll, id=wx.ID_SELECTALL)
self.Bind(wx.EVT_MENU, self.OnRemoveWord,id=4)
self.PopupMenu(menu)

 def

OnUndo(self,event):self.Undo()
def OnRedo(self,event):self.Redo()
def OnCut(self,event):self.Cut()
def OnCopy(self,event):self.Copy()
def OnPaste(self,event):self.Paste()
def OnDelete(self,event):self.Clear()
def OnSelectAll(self,event):self.SelectAll()
def OnRemoveWord(self,event):
WordToRemove=self.GetSelectedText()
WordInList=False
for num, colVal in enumerate(choices):
choice=re.match(WordToRemove,choices[num])
if choice:

WordInList=True
try:
choices.remove(WordToRemove)
except:
event.Skip()
choices.sort()
self.MyChoices=’ '.join(choices)
break

#else:
#event.Skip()

def OnAutoFill(self,event):
    p = self.GetCurrentPos()
    st = self.WordStartPosition(p, 1)
    en = self.WordEndPosition(p, 1)
    self.CurentWord=self.GetTextRange(st,en)
    if p-st>AutoFillStartLen:
        stc.StyledTextCtrl.AutoCompShow(self,(p-st),self.MyChoices)
   
def AddWord(self,WordToAdd):
     if len(WordToAdd)

AutoFillMinWordLen:
WordInList=False
for num, colVal in enumerate(choices):
choice=re.match(WordToAdd,choices[num])
if choice:
WordInList=True
break
else:

choices.append(WordToAdd)
choices.sort()
self.MyChoices=’ '.join(choices)

def OnKeyDown(self,event):
      if event.GetKeyCode() == wx.WXK_SPACE:
          self.AddWord(self.CurentWord)
      event.Skip()

class TestFrame(wx.Frame):

def init(self):
global MainEditor
wx.Frame.init(self, None, -1, ‘Walts Program’)
MainEditor = MySTC(self)
app = wx.App(0)

if name==“main”:
#app = wx.App(0)
win = TestFrame()
win.Show(True)
app.MainLoop()

stcEditor.py (3.67 KB)

Can you please specify your problem? I’m not sure what you’re asking.

Do you mean opening files from the command line? Like “python WaltsProgram.py test.txt”?

Here are a few random suggestions:

  • Change the global MainEditor in your TestFrame class - create a self.stc field in the class instead. You want to avoid global if you can.
  • Remove the app = wx.App(0) call in your TestFrame class - you should be calling that in the main block.
  • Change the name “TestFrame” to something more descriptive - your application is probably more than a test
  • Change the names of the constants to capitalized letters with underscores between the words - this is just convention (
    i.e. change AutoFillStartLen to AUTO_FILL_START_LENGTH)

Walter Igo wrote:

I have a simple text editor with a autocomplete of some words. I am having problems getting it to open with a one line command from anoter file, like VisualWX. I have worked on this problem for a week now. there is some lesson I need to learn better.
I have a sample file. it will not start though.

It doesn't start because you are trying to make an instance of the frame before you make an instance of the wx.App object.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!