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