Rudy Schockaert
Email: rudy.schockaert@gmail.com
1 import os, wx
2 ID_OPEN = 101
3 ID_SAVE = 102
4 ID_SAVEAS = 103
5 ID_EXIT = 104
6 ID_ABOUT = 111
7 class MainWindow(wx.Frame):
8 """ We simply derive a new class of Frame. """
9 def __init__(self,parent,id,title):
10 wx.Frame.__init__(self,parent,wx.ID_ANY, title, size = ( 400,200),
11 style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)
12
13 self.dirname = os.curdir
14 self.filename = ''
15 self.modified = False
16
17 self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
18 self.CreateStatusBar()
19
20 self.fileMenu = wx.Menu()
21 self.fileMenu.Append(ID_OPEN, "&Open", " Open a file to edit")
22 self.fileMenu.Append(ID_SAVE, "&Save", " Save your modifications")
23 self.fileMenu.Enable(ID_SAVE, False)
24 self.fileMenu.Append(ID_SAVEAS, "Save &As", " Save your modifications to another file")
25 self.fileMenu.Enable(ID_SAVEAS, False)
26 self.fileMenu.AppendSeparator()
27 self.fileMenu.Append(ID_EXIT, "&Exit", " Terminate the program")
28
29 helpMenu = wx.Menu()
30 helpMenu.Append(ID_ABOUT, "&About", " Information about this program")
31
32 menuBar = wx.MenuBar()
33 menuBar.Append(self.fileMenu, "&File")
34 menuBar.Append(helpMenu, "&Help")
35 self.SetMenuBar(menuBar)
36
37 self.Connect(ID_OPEN, -1, wx.wxEVT_COMMAND_MENU_SELECTED, self.OnOpen)
38 self.Connect(ID_SAVE, -1, wx.wxEVT_COMMAND_MENU_SELECTED, self.OnSave)
39 self.Connect(ID_SAVEAS, -1, wx.wxEVT_COMMAND_MENU_SELECTED, self.OnSaveAs)
40 self.Connect(ID_EXIT, -1, wx.wxEVT_COMMAND_MENU_SELECTED, self.OnExit)
41 self.Connect(ID_ABOUT, -1, wx.wxEVT_COMMAND_MENU_SELECTED, self.OnAbout)
42 self.control.Connect(-1, -1, wx.wxEVT_COMMAND_TEXT_UPDATED, self.OnTextUpdated)
43 self.Show(True)
44
45 def OnAbout(self,e):
46 d= wx.MessageDialog( self, " A sample editor \n"
47 " in wxPython","About Sample Editor", wx.OK)
48 # Create a message dialog box
49 d.ShowModal() # Shows it
50 d.Destroy() # finally destroy it when finished.
51
52 def OnOpen(self,e):
53 """ Open a file"""
54 dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
55 if dlg.ShowModal() == wx.ID_OK:
56 self.filename=dlg.GetFilename()
57 self.dirname=dlg.GetDirectory()
58 f=open(os.path.join(self.dirname,self.filename),'r')
59 self.control.SetValue(f.read())
60 f.close()
61 self.SetTitle(self.filename)
62 dlg.Destroy()
63
64 def OnSave(self,e):
65 f=open(os.path.join(self.dirname,self.filename),'w')
66 f.write(self.control.GetValue())
67 f.close()
68
69 def OnSaveAs(self,e):
70 """ Save file as ..."""
71 dlg = wx.FileDialog(self, "Save file as ...", self.dirname, self.filename, "*.*", wx.SAVE)
72 if dlg.ShowModal() == wx.ID_OK:
73 self.filename=dlg.GetFilename()
74 self.fileMenu.Enable(ID_SAVE, True)
75 self.dirname=dlg.GetDirectory()
76 self.OnSave(True)
77 self.SetTitle(self.filename)
78 dlg.Destroy()
79
80 def OnExit(self,e):
81 self.Close(True) # Close the frame.
82 def OnTextUpdated(self,e):
83 self.modified = True
84 if self.filename <> '':
85 self.fileMenu.Enable(ID_SAVE, True)
86 self.fileMenu.Enable(ID_SAVEAS, True)
87
88
89
90 app = wx.PySimpleApp()
91 frame = MainWindow(None, -1, "Sample editor")
92 app.MainLoop()