|
#! /Usr/bin/python
Import wx
Def OnOpen (event ):
"""
Load a file into the textField.
"""
Dialog = wx. FileDialog (None, 'notepad ', style = wx. OPEN)
If dialog. ShowModal () = wx. ID_ OK:
Filename. SetValue (dialog. GetPath ())
File = open (dialog. GetPath ())
Contents. SetValue (file. read ())
File. close ()
Dialog. Destroy ()
Def OnSave (event ):
"""
Save text into the orignal file.
"""
If filename. GetValue () = '':
Dialog = wx. FileDialog (None, 'notepad ', style = wx. SAVE)
If dialog. ShowModal () = wx. ID_ OK:
Filename. SetValue (dialog. GetPath ())
File = open (dialog. GetPath (), 'w ')
File. write (contents. GetValue ())
File. close ()
Dialog. Destory ()
Else:
File = open (filename. GetValue (), 'w ')
File. write (contents. GetValue ())
File. close ()
App = wx. App ()
Win = wx. Frame (None, title = "Simple Editor", size = (600,400 ))
Bkg = wx. Panel (win)
# Define a 'load' button and its label,
# Bind to an button event with a function 'load'
LoadButton = wx. Button (bkg, label = 'open ')
LoadButton. Bind (wx. EVT_BUTTON, OnOpen)
# Define a 'save' button and its label,
# Bind to an button event with a function 'save'
SaveButton = wx. Button (bkg, label = 'save ')
SaveButton. Bind (wx. EVT_BUTTON, OnSave)
# Define a textBox for filename.
Filename = wx. TextCtrl (bkg)
# Define a textBox for file contents.
Contents = wx. TextCtrl (bkg, style = wx. TE_MULTILINE | wx. HSCROLL)
# Use sizer to set relative position of the components.
# Horizontal layout
Hbox = wx. BoxSizer ()
Hbox. Add (filename, proportion = 1, flag = wx. EXPAND)
Hbox. Add (loadButton, proportion = 0, flag = wx. LEFT, border = 5)
Hbox. Add (saveButton, proportion = 0, flag = wx. LEFT, border = 5)
# Vertical layout
Vbox = wx. BoxSizer (wx. VERTICAL)
Vbox. Add (hbox, proportion = 0, flag = wx. EXPAND | wx. ALL, border = 5)
Vbox. Add (contents, proportion = 1,
Flag = wx. EXPAND | wx. LEFT | wx. BOTTOM | wx. RIGHT, border = 5)
Bkg. SetSizer (vbox)
Win. Show ()
App. MainLoop ()
|