Open a file

Hello…

I have just created a simple editor. I want it to open a .py file.

Its opening the file but not displaying it. and also I want it to have a syntax highlighting if it imports a .py file.

Here is the code…

import os
from wxPython.wx import *
ID_OPEN=101
ID_EXIT=110
ID_SAVE=201
ID_SOLVE=210
import wx
class MainWindow(wxFrame):
def init(self,parent,id,title):
wxFrame.init(self,parent,wxID_ANY, title, size = ( 200,100),

                                    style=wxDEFAULT_FRAME_STYLE|
                                   wxNO_FULL_REPAINT_ON_RESIZE)
    self.control = wxTextCtrl(self, 1, style=wxTE_MULTILINE)
    self.CreateStatusBar() # A StatusBar in the bottom of the window
    filemenu= wxMenu()
    filemenu.Append(ID_OPEN,"Open","Choose a file to open")
    filemenu.AppendSeparator()
    filemenu.Append(ID_SOLVE,"Solve")
    filemenu.AppendSeparator()
    filemenu.Append(ID_EXIT,"E&xit"," Terminate the program")
    # Creating the menubar.
    menuBar = wxMenuBar()
    menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
    self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
    EVT_MENU(self, ID_EXIT, self.OnExit)   # attach the menu-event ID_EXIT to the
    EVT_MENU(self, ID_OPEN, self.OnOpen)                                                 # method self.OnExit
    self.Show(true)
    self.projectdirty = false
   
def OnAbout(self,e):
    d= wxMessageDialog( self, "MPY Optimizer ")
                       
                        # Create a message dialog box
    d.ShowModal() # Shows it
    d.Destroy() # finally destroy it when finished.
def OnExit(self,e):
    self.Close(true)  # Close the frame.

def OnSolve(self,evt):
    res = eval(self.txtCommand.GetValue())

self.txtResult.AddText(’%s\n’ % res)
return res
def OnOpen(self,e):
open_it = true
if self.projectdirty:
dlg=wxMessageDialog(self, ‘The project has been changed. Save?’, ‘wxProject’,
wxYES_NO | wxCANCEL)
result = dlg.ShowModal()
if result == wxID_YES:
self.project_save()
if result == wxID_CANCEL:
open_it = false
dlg.Destroy()
if open_it:
dlg = wxFileDialog(self, “Choose a project to open”, “.”, “”, “*.py”, wxOPEN)
if dlg.ShowModal() == wxID_OK:
self.project_open(dlg.GetPath())
dlg.Destroy()
def project_open(self, project_file):
try:
input = open (project_file, ‘r’)

        self.tree.DeleteAllItems()

        self.project_file = project_file
        name = replace (input.readline(), "\n", "")
        self.SetTitle (name)
        self.root = self.tree.AddRoot(name)
        self.activeitem = self.root
        for line in input.readlines():
            self.tree.AppendItem (self.root, replace(line, "\n", ""))
        input.close
        self.tree.Expand (self.root)

        self.editor.Clear()
        self.editor.Enable (false)

        self.projectdirty = false
    except IOError:
        pass

def project_save(self):
    try:
        output = open (self.project_file, 'w+')
        output.write (self.tree.GetItemText (self.root) + "\n")

        count = self.tree.GetChildrenCount (self.root)
        iter = 0
        child = ''
        for i in range(count):
           if i == 0:
              (child,iter) = self.tree.GetFirstChild(self.root,iter)
           else:
              (child,iter) = self.tree.GetNextChild(self.root,iter)
           output.write (self.tree.GetItemText(child) + "\n")
        output.close()
        self.projectdirty = false
    except IOError:
        dlg_m = wxMessageDialog (self, 'There was an error saving the project file.',
  'Error!', wxOK)
        dlg_m.ShowModal()
        dlg_m.Destroy()

app = wxPySimpleApp()
frame = MainWindow(None, -1, “Mpy Optimizer - Leo Lopez and Balaji Balaraman”)
app.MainLoop()

Balaji

Depending on your specific requirements, the Py Shell may be applicable for your needs. It does syntax highlighting and a quite a bit more for Python source files.

See the wxPython demo called 'PyShell' and there is a script 'shell.py' to run the Py Shell standalone***.

/Jean Brouwers
ProphICy Semiconductor, Inc.

**) it is under More Windows/Controls on wxPython 2.4.1.2

***) that should be in a subdirectory called 'py' under your 'wx' directory.

Balaji Balaraman wrote:

···

Hello..
I have just created a simple editor. I want it to open a .py file.
Its opening the file but not displaying it. and also I want it to have a syntax highlighting if it imports a .py file.
Here is the code..
import os
from wxPython.wx import *
ID_OPEN=101
ID_EXIT=110
ID_SAVE=201
ID_SOLVE=210
import wx
class MainWindow(wxFrame):
    def __init__(self,parent,id,title):
        wxFrame.__init__(self,parent,wxID_ANY, title, size = ( 200,100),
                                         style=wxDEFAULT_FRAME_STYLE|
                                       wxNO_FULL_REPAINT_ON_RESIZE)
        self.control = wxTextCtrl(self, 1, style=wxTE_MULTILINE)
        self.CreateStatusBar() # A StatusBar in the bottom of the window
        filemenu= wxMenu()
        filemenu.Append(ID_OPEN,"Open","Choose a file to open")
        filemenu.AppendSeparator()
        filemenu.Append(ID_SOLVE,"Solve")
        filemenu.AppendSeparator()
        filemenu.Append(ID_EXIT,"E&xit"," Terminate the program")
        # Creating the menubar.
        menuBar = wxMenuBar()
        menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
        self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content.
        EVT_MENU(self, ID_EXIT, self.OnExit) # attach the menu-event ID_EXIT to the
        EVT_MENU(self, ID_OPEN, self.OnOpen) # method self.OnExit
        self.Show(true)
        self.projectdirty = false
           def OnAbout(self,e):
        d= wxMessageDialog( self, "MPY Optimizer ")
                                                       # Create a message dialog box
        d.ShowModal() # Shows it
        d.Destroy() # finally destroy it when finished.
    def OnExit(self,e):
        self.Close(true) # Close the frame.
     def OnSolve(self,evt):
        res = eval(self.txtCommand.GetValue())
self.txtResult.AddText('%s\n' % res)
return res
    def OnOpen(self,e):
        open_it = true
        if self.projectdirty:
           dlg=wxMessageDialog(self, 'The project has been changed. Save?', 'wxProject',
      wxYES_NO | wxCANCEL)
           result = dlg.ShowModal()
           if result == wxID_YES:
              self.project_save()
           if result == wxID_CANCEL:
              open_it = false
           dlg.Destroy()
        if open_it:
           dlg = wxFileDialog(self, "Choose a project to open", ".", "", "*.py", wxOPEN)
           if dlg.ShowModal() == wxID_OK:
              self.project_open(dlg.GetPath())
           dlg.Destroy()
    def project_open(self, project_file):
        try:
            input = open (project_file, 'r')
             self.tree.DeleteAllItems()
             self.project_file = project_file
            name = replace (input.readline(), "\n", "")
            self.SetTitle (name)
            self.root = self.tree.AddRoot(name)
            self.activeitem = self.root
            for line in input.readlines():
                self.tree.AppendItem (self.root, replace(line, "\n", ""))
            input.close
            self.tree.Expand (self.root)
             self.editor.Clear()
            self.editor.Enable (false)
             self.projectdirty = false
        except IOError:
            pass
     def project_save(self):
        try:
            output = open (self.project_file, 'w+')
            output.write (self.tree.GetItemText (self.root) + "\n")
             count = self.tree.GetChildrenCount (self.root)
            iter = 0
            child = ''
            for i in range(count):
               if i == 0:
                  (child,iter) = self.tree.GetFirstChild(self.root,iter)
               else:
                  (child,iter) = self.tree.GetNextChild(self.root,iter)
               output.write (self.tree.GetItemText(child) + "\n")
            output.close()
            self.projectdirty = false
        except IOError:
            dlg_m = wxMessageDialog (self, 'There was an error saving the project file.',
      'Error!', wxOK)
            dlg_m.ShowModal()
            dlg_m.Destroy()
   app = wxPySimpleApp()
frame = MainWindow(None, -1, "Mpy Optimizer - Leo Lopez and Balaji Balaraman")
app.MainLoop()
Balaji

Actually I was using the tree for other purpose. Now I have taken it out still its not opening the file..

import os
from wxPython.wx import *
ID_OPEN=101
ID_EXIT=110
ID_SAVE=201
ID_SOLVE=210
import wx
class MainWindow(wxFrame):
    def __init__(self,parent,id,title):
        wxFrame.__init__(self,parent,wxID_ANY, title, size = ( 200,100),

                                        style=wxDEFAULT_FRAME_STYLE|
                                       wxNO_FULL_REPAINT_ON_RESIZE)
        self.control = wxTextCtrl(self, 1, style=wxTE_MULTILINE)
        self.CreateStatusBar() # A StatusBar in the bottom of the window
        filemenu= wxMenu()
        filemenu.Append(ID_OPEN,"Open","Choose a file to open")
        filemenu.AppendSeparator()
        filemenu.Append(ID_SOLVE,"Solve")
        filemenu.AppendSeparator()
        filemenu.Append(ID_EXIT,"E&xit"," Terminate the program")
        # Creating the menubar.
        menuBar = wxMenuBar()
        menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
        self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content.
        EVT_MENU(self, ID_EXIT, self.OnExit) # attach the menu-event ID_EXIT to the
        EVT_MENU(self, ID_OPEN, self.OnOpen) # method self.OnExit
        self.Show(true)
        self.projectdirty = false

    def OnAbout(self,e):
        d= wxMessageDialog( self, "MPY Optimizer ")

                            # Create a message dialog box
        d.ShowModal() # Shows it
        d.Destroy() # finally destroy it when finished.
    def OnExit(self,e):
        self.Close(true) # Close the frame.

    def OnSolve(self,evt):
        res = eval(self.txtCommand.GetValue())
self.txtResult.AddText('%s\n' % res)
return res
    def OnOpen(self,e):
        open_it = true
        if self.projectdirty:
           dlg=wxMessageDialog(self, 'The project has been changed. Save?', 'wxProject',
      wxYES_NO | wxCANCEL)
           result = dlg.ShowModal()
           if result == wxID_YES:
              self.project_save()
           if result == wxID_CANCEL:
              open_it = false
           dlg.Destroy()
        if open_it:
           dlg = wxFileDialog(self, "Choose a project to open", ".", "", "*.py", wxOPEN)
           if dlg.ShowModal() == wxID_OK:
              self.project_open(dlg.GetPath())
           dlg.Destroy()
    def project_open(self, project_file):
        try:
            input = open (project_file, 'r')
            self.project_file = project_file
            input.close
            self.projectdirty = false
        except IOError:
            pass

    def project_save(self):
        try:
            output = open (self.project_file, 'w+')
            output.write (self.tree.GetItemText (self.root) + "\n")
            count = self.tree.GetChildrenCount (self.root)
            iter = 0
            child = ''
            for i in range(count):
               if i == 0:
                  (child,iter) = self.tree.GetFirstChild(self.root,iter)
               else:
                  (child,iter) = self.tree.GetNextChild(self.root,iter)
               output.write (self.tree.GetItemText(child) + "\n")
            output.close()
            self.projectdirty = false
        except IOError:
            dlg_m = wxMessageDialog (self, 'There was an error saving the project file.',
      'Error!', wxOK)
            dlg_m.ShowModal()
            dlg_m.Destroy()

app = wxPySimpleApp()
frame = MainWindow(None, -1, "Mpy Optimizer - Leo Lopez and Balaji Balaraman")
app.MainLoop()

Balaji Balaraman

I don't see that you're ever actually resetting the value of the text box,
self.control.

Anyhow, changing your project_open function to the following loads the file
okay for me:

def project_open(self, project_file):
        try:
            input = open (project_file, 'r')
            self.project_file = project_file
            self.control.SetValue(input.read())
            input.close
            self.projectdirty = false
        except IOError:
            pass

The changes I made were setting the value of self.control to the contents of
project_file.

Greg

···

-----Original Message-----
From: Balaji Balaraman [mailto:balaji@email.arizona.edu]
Sent: Sunday, October 31, 2004 8:16 PM
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] Open a file

Actually I was using the tree for other purpose. Now I have taken it out
still its not opening the file..

import os
from wxPython.wx import *
ID_OPEN=101
ID_EXIT=110
ID_SAVE=201
ID_SOLVE=210
import wx
class MainWindow(wxFrame):
    def __init__(self,parent,id,title):
        wxFrame.__init__(self,parent,wxID_ANY, title, size = ( 200,100),

                                        style=wxDEFAULT_FRAME_STYLE|
                                       wxNO_FULL_REPAINT_ON_RESIZE)
        self.control = wxTextCtrl(self, 1, style=wxTE_MULTILINE)
        self.CreateStatusBar() # A StatusBar in the bottom of the window
        filemenu= wxMenu()
        filemenu.Append(ID_OPEN,"Open","Choose a file to open")
        filemenu.AppendSeparator()
        filemenu.Append(ID_SOLVE,"Solve")
        filemenu.AppendSeparator()
        filemenu.Append(ID_EXIT,"E&xit"," Terminate the program")
        # Creating the menubar.
        menuBar = wxMenuBar()
        menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the
MenuBar
        self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content.
        EVT_MENU(self, ID_EXIT, self.OnExit) # attach the menu-event
ID_EXIT to the
        EVT_MENU(self, ID_OPEN, self.OnOpen)
# method self.OnExit
        self.Show(true)
        self.projectdirty = false

    def OnAbout(self,e):
        d= wxMessageDialog( self, "MPY Optimizer ")

                            # Create a message dialog box
        d.ShowModal() # Shows it
        d.Destroy() # finally destroy it when finished.
    def OnExit(self,e):
        self.Close(true) # Close the frame.

    def OnSolve(self,evt):
        res = eval(self.txtCommand.GetValue())
self.txtResult.AddText('%s\n' % res)
return res
    def OnOpen(self,e):
        open_it = true
        if self.projectdirty:
           dlg=wxMessageDialog(self, 'The project has been changed. Save?',

'wxProject',
      wxYES_NO | wxCANCEL)
           result = dlg.ShowModal()
           if result == wxID_YES:
              self.project_save()
           if result == wxID_CANCEL:
              open_it = false
           dlg.Destroy()
        if open_it:
           dlg = wxFileDialog(self, "Choose a project to open", ".", "",
"*.py", wxOPEN)
           if dlg.ShowModal() == wxID_OK:
              self.project_open(dlg.GetPath())
           dlg.Destroy()
    def project_open(self, project_file):
        try:
            input = open (project_file, 'r')
            self.project_file = project_file
            input.close
            self.projectdirty = false
        except IOError:
            pass

    def project_save(self):
        try:
            output = open (self.project_file, 'w+')
            output.write (self.tree.GetItemText (self.root) + "\n")
            count = self.tree.GetChildrenCount (self.root)
            iter = 0
            child = ''
            for i in range(count):
               if i == 0:
                  (child,iter) = self.tree.GetFirstChild(self.root,iter)
               else:
                  (child,iter) = self.tree.GetNextChild(self.root,iter)
               output.write (self.tree.GetItemText(child) + "\n")
            output.close()
            self.projectdirty = false
        except IOError:
            dlg_m = wxMessageDialog (self, 'There was an error saving the
project file.',
      'Error!', wxOK)
            dlg_m.ShowModal()
            dlg_m.Destroy()

app = wxPySimpleApp()
frame = MainWindow(None, -1, "Mpy Optimizer - Leo Lopez and Balaji
Balaraman")
app.MainLoop()

Balaji Balaraman

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org