Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import wx
- import os
- class TextEdit(wx.Frame): #TextEdit inherits from wx.Frame
- """adds multiline textbox to wx.Frame"""
- def __init__(self, parent, title):
- wx.Frame.__init__(self, parent, title = title, size = (400, 450))
- self.control = wx.TextCtrl(self, style = wx.TE_MULTILINE)
- self.CreateStatusBar()
- menu = wx.Menu() #menu creation
- menuNew = menu.Append(wx.ID_NEW, "&New", "Create a new file")
- menuSave = menu.Append(wx.ID_SAVE, "&Save", "Save to file")
- menuSaveAs = menu.Append(wx.ID_SAVEAS, "Save As", "Save to new file")
- menuOpen = menu.Append(wx.ID_OPEN, "&Open", "Open a text file")
- menuExit = menu.Append(wx.ID_EXIT, "&Quit", "End program")
- helpMenu = wx.Menu()
- menuAbout = helpMenu.Append(wx.ID_ABOUT, "&About", "Info about program")
- menuHelp = helpMenu.Append(wx.ID_HELP, "&Help", "Get help figuring out the program")
- menuBar = wx.MenuBar() #MenuBar creation
- menuBar.Append(menu, "&File") #add menu to menuBar and call it File
- menuBar.Append(helpMenu, "Help") #add menu to menuBar and call it Help
- self.SetMenuBar(menuBar) #add menuBar to Frame
- #event handling
- self.Bind(wx.EVT_MENU, self.OnNew, menuNew)
- self.Bind(wx.EVT_MENU, self.OnSave, menuSave)
- self.Bind(wx.EVT_MENU, self.OnSaveAs, menuSaveAs)
- self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
- self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
- self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
- self.Bind(wx.EVT_MENU, self.OnHelp, menuHelp)
- self.fileName = ""
- self.dirName = ""
- self.Show(True) #this line always at the end of the function
- def OnNew(self, e):
- """Creates a new text file"""
- #wx.ID_EXIT only closes one window with this method. TODO: make it more dependent
- TextEdit(None, "New Text File")
- def OnSave(self, e):
- #I/O stuff here
- if (self.fileName != "") and (self.dirName != ""):
- content = self.control.GetValue() #obtain content
- filehandle = open(os.path.join(self.dirName, self.fileName), "w")
- filehandle.write(content)
- filehandle.close()
- else:
- return self.OnSaveAs(e)
- def OnSaveAs(self, e):
- dbox = wx.FileDialog(self, "Choose a file", self.dirName, "", "*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
- if dbox.ShowModal() == wx.ID_OK:
- content = self.control.GetValue() #obtain content
- self.fileName = dbox.GetFilename()
- self.dirName = dbox.GetDirectory()
- filehandle = open(os.path.join(self.dirName, self.fileName), "w")
- filehandle.write(content)
- filehandle.close()
- dbox.Destroy()
- def OnOpen(self, e):
- #I/O stuff here
- """Open file"""
- self.dirName = " " #why necessary?
- dbox = wx.FileDialog(self, "Find your file", self.dirName, "","*.*", wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR)
- if dbox.ShowModal() == wx.ID_OK:
- self.fileName = dbox.GetFilename()
- self.dirName = dbox.GetDirectory()
- x = open(os.path.join(self.dirName, self.fileName), "r")
- self.control.SetValue(x.read())
- x.close()
- dbox.Destroy()
- def OnExit(self, e):
- """Exits program by closing frame"""
- self.Close(True) #close frame
- def OnAbout(self, e):
- """Displays info about program"""
- dbox = wx.MessageDialog(self, "A simple text editor built for practice", "about TextEdit", wx.OK)
- dbox.ShowModal()
- dbox.Destroy()
- def OnHelp(self, e):
- """Displays helpful information, hopefully"""
- dbox = wx.MessageDialog(self, "Please visit the website ___ for help with this program.", "Help", wx.OK)
- dbox.ShowModal()
- dbox.Destroy()
- app = wx.App(False)
- frame = TextEdit(None, "Editor")
- app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement