Advertisement
Dernhelm

try2.py

Jun 12th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. import wx
  2. import os
  3.  
  4. class TextEdit(wx.Frame): #TextEdit inherits from wx.Frame
  5.    """adds multiline textbox to wx.Frame"""
  6.    def __init__(self, parent, title):
  7.     wx.Frame.__init__(self, parent, title = title, size = (400, 450))
  8.     self.control = wx.TextCtrl(self, style = wx.TE_MULTILINE)
  9.     self.CreateStatusBar()
  10.  
  11.     menu = wx.Menu() #menu creation
  12.     menuNew = menu.Append(wx.ID_NEW, "&New", "Create a new file")
  13.     menuSave = menu.Append(wx.ID_SAVE, "&Save", "Save to file")
  14.     menuSaveAs = menu.Append(wx.ID_SAVEAS, "Save As", "Save to new file")
  15.     menuOpen = menu.Append(wx.ID_OPEN, "&Open", "Open a text file")
  16.     menuExit = menu.Append(wx.ID_EXIT, "&Quit", "End program")
  17.  
  18.     helpMenu = wx.Menu()
  19.     menuAbout = helpMenu.Append(wx.ID_ABOUT, "&About", "Info about program")
  20.     menuHelp = helpMenu.Append(wx.ID_HELP, "&Help", "Get help figuring out the program")
  21.  
  22.     menuBar = wx.MenuBar() #MenuBar creation
  23.     menuBar.Append(menu, "&File") #add menu to menuBar and call it File
  24.     menuBar.Append(helpMenu, "Help") #add menu to menuBar and call it Help
  25.     self.SetMenuBar(menuBar) #add menuBar to Frame
  26.  
  27.     #event handling
  28.     self.Bind(wx.EVT_MENU, self.OnNew, menuNew)
  29.     self.Bind(wx.EVT_MENU, self.OnSave, menuSave)
  30.     self.Bind(wx.EVT_MENU, self.OnSaveAs, menuSaveAs)
  31.     self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
  32.     self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
  33.     self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
  34.     self.Bind(wx.EVT_MENU, self.OnHelp, menuHelp)
  35.    
  36.     self.fileName = ""
  37.     self.dirName = ""
  38.  
  39.     self.Show(True) #this line always at the end of the function
  40.  
  41.    def OnNew(self, e):
  42.     """Creates a new text file"""
  43.     #wx.ID_EXIT only closes one window with this method. TODO: make it more dependent
  44.     TextEdit(None, "New Text File")
  45.    def OnSave(self, e):
  46.     #I/O stuff here
  47.     if (self.fileName != "") and (self.dirName != ""):
  48.        content = self.control.GetValue() #obtain content
  49.        filehandle = open(os.path.join(self.dirName, self.fileName), "w")
  50.        filehandle.write(content)
  51.        filehandle.close()
  52.     else:
  53.        return self.OnSaveAs(e)
  54.    def OnSaveAs(self, e):
  55.     dbox = wx.FileDialog(self, "Choose a file", self.dirName, "", "*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
  56.     if dbox.ShowModal() == wx.ID_OK:
  57.        content = self.control.GetValue() #obtain content
  58.        self.fileName = dbox.GetFilename()
  59.        self.dirName = dbox.GetDirectory()
  60.        filehandle = open(os.path.join(self.dirName, self.fileName), "w")
  61.        filehandle.write(content)
  62.        filehandle.close()
  63.        dbox.Destroy()
  64.    def OnOpen(self, e):
  65.     #I/O stuff here
  66.     """Open file"""
  67.     self.dirName = " " #why necessary?
  68.     dbox = wx.FileDialog(self, "Find your file", self.dirName, "","*.*", wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR)
  69.     if dbox.ShowModal() == wx.ID_OK:
  70.        self.fileName = dbox.GetFilename()
  71.        self.dirName = dbox.GetDirectory()
  72.        x = open(os.path.join(self.dirName, self.fileName), "r")
  73.        self.control.SetValue(x.read())
  74.        x.close()
  75.     dbox.Destroy()
  76.    def OnExit(self, e):
  77.     """Exits program by closing frame"""
  78.     self.Close(True) #close frame
  79.    def OnAbout(self, e):
  80.     """Displays info about program"""
  81.     dbox = wx.MessageDialog(self, "A simple text editor built for practice", "about TextEdit", wx.OK)
  82.     dbox.ShowModal()
  83.     dbox.Destroy()
  84.    def OnHelp(self, e):
  85.     """Displays helpful information, hopefully"""
  86.     dbox = wx.MessageDialog(self, "Please visit the website ___ for help with this program.", "Help", wx.OK)
  87.     dbox.ShowModal()
  88.     dbox.Destroy()
  89.  
  90. app = wx.App(False)
  91. frame = TextEdit(None, "Editor")
  92. app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement