Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Text Editor Help. I cant save the files?

$
0
0
This is a simple text editor I am writing and the OnSave function wont seem to work. I am writing this half so I can learn python and half to make my own custom codding text editor. What have I done wrong? Also, any tips on text highlighting like in notepad++ or IDLE so I can do some coding in it? Thanks!

import wx
import os

ID_ABOUT=101
ID_OPEN=102
ID_SAVE=103
ID_BUTTON1=300
ID_EXIT=200

class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, wx.ID_ANY, title)
        
        #Editor and Status Bar
        self.control = wx.TextCtrl(self, 1, style = wx.TE_MULTILINE,pos = (60, 60), size = (700, 400))
        statusBar = self.CreateStatusBar()
        
        #Creating the Menu Bar
        #And other such things
        menubar = wx.MenuBar()
        
        helpMenu = wx.Menu()
        helpMenu.Append(ID_ABOUT, "&About", "About this program")
        
        fileMenu = wx.Menu() 
        fileMenu.Append(ID_OPEN, "&Open...", "Opens a File")
        fileMenu.Append(ID_SAVE, "&Save...", "Saves the File")
        fileMenu.AppendSeparator()
        fileMenu.Append(ID_EXIT, "E&xit", "Terminate the program")

        menubar.Append(fileMenu, "&File")
        menubar.Append(helpMenu, "&Help")
        self.SetMenuBar(menubar)
        
        #Code to run on each menu option
        wx.EVT_MENU(self, ID_ABOUT, self.OnAbout)
        wx.EVT_MENU(self, ID_EXIT, self.OnExit)
        wx.EVT_MENU(self, ID_OPEN, self.OnOpen)
        wx.EVT_MENU(self, ID_SAVE, self.OnSave)
        
        #Row of Buttons at the Bottom
        '''
			self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        self.buttons = []
        for i in range(6):
            bid = i + 1
            self.buttons.append(wx.Button(self, ID_BUTTON1+i, "Button &" + str(bid)))
            self.sizer2.Add(self.buttons[i], 1, wx.EXPAND)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.control, 1, wx.EXPAND)
        self.sizer.Add(self.sizer2, 0, wx.EXPAND)
        
        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)
        
        self.Show(1)
		'''
        
        #Widgets to make sure things are going the way we want
        self.aboutme = wx.MessageDialog(self, "Fedit\nJoshua French \nDecember 2012 \nBuild 1: Made in Python 2.7, wxPython 1.9.1")
        self.doiexit = wx.MessageDialog(self, "Exit\nAre You Sure?\n", "Thank You!", wx.YES_NO)
        
        
        # dirname is an APPLICATION variable that we're choosing to store
        # in with the frame - it's the parent directory for any file we
        # choose to edit in this frame
        self.dirname = ''
        
    def OnAbout(self, e):
        self.aboutme.ShowModal()
        
    def OnExit(self, e):
        igot = self.doiexit.ShowModal()
        if igot == wx.ID_YES:
            self.Close(True)
            
    def OnOpen(self, e):
        dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*",wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
            
            filehandle = open(os.path.join(self.dirname, self.filename), 'r')
            self.control.SetValue(filehandle.read())
            filehandle.close()
            
            self.SetTitle("Editing... "+self.filename)
        dlg.Destroy()
            
    def OnSave(self, e):
        dlg = wx.FileDialog(self, "Choose a File", self.dirname, "", "*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
        if dlg.ShowModal == wx.ID_OK:
            itcontains = self.control.GetValue()
            
            self.filename = dlg.GetFileName()
            self.dirname = dlg.GetDirectory()
            filehandle = open(os.path.join(self.dirname, self.filename), 'w')
            filehandle.write(itcontains)
            filehandle.close()
        dlg.Destroy()
    
        #Exiting the program on exit
    def exitProgram(self, event):
        self.Close(True)#Closes the Program
        
    def closeWindow(self, event):
        self.Destroy()#Destroys the window
        
        
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MainWindow(None, "FEdit")
    frame.Show()
    app.MainLoop()


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>