6 Replies - 477 Views - Last Post: 27 December 2012 - 02:40 PM Rate Topic: -----

#1 Jbf1001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 24-October 12

Text Editor Help. I cant save the files?

Posted 27 December 2012 - 10:36 AM

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()


This post has been edited by Jbf1001: 27 December 2012 - 10:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Text Editor Help. I cant save the files?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,236
  • Joined: 21-June 11

Re: Text Editor Help. I cant save the files?

Posted 27 December 2012 - 11:06 AM

"wont seem to work" is not an adequate problem description.

That said one problem is that you're not actually calling the ShowModal function on line 93.
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,844
  • Joined: 12-December 12

Re: Text Editor Help. I cant save the files?

Posted 27 December 2012 - 11:17 AM

if dlg.ShowModal() == wx.ID_OK:

to call the ShowModal method.

This similar code may be useful for comparison, and implementing OnSaveAs.
Was This Post Helpful? 0
  • +
  • -

#4 woooee  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 78
  • Joined: 21-November 12

Re: Text Editor Help. I cant save the files?

Posted 27 December 2012 - 11:20 AM

General good programming technique says that you should have an else along with the following if statement to print a message when not true.
if dlg.ShowModal == wx.ID_OK: 

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,844
  • Joined: 12-December 12

Re: Text Editor Help. I cant save the files?

Posted 27 December 2012 - 11:25 AM

Quote

General good programming technique says that you should have an else along with the following if statement to print a message when not true.

I agree that an else might be useful in this instance (for testing) but would not agree that an else is a "should have".

This post has been edited by andrewsw: 27 December 2012 - 11:26 AM

Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,236
  • Joined: 21-June 11

Re: Text Editor Help. I cant save the files?

Posted 27 December 2012 - 11:26 AM

View Postwoooee, on 27 December 2012 - 07:20 PM, said:

General good programming technique says that you should have an else along with the following if statement to print a message when not true.
if dlg.ShowModal == wx.ID_OK: 


Print a message where? And why? Surely you're not recommending displaying a user-visible error (or warning) message just because the user hit the "cancel" button of the save dialog.
Was This Post Helpful? 1
  • +
  • -

#7 Jbf1001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 24-October 12

Re: Text Editor Help. I cant save the files?

Posted 27 December 2012 - 02:40 PM

Thanks guys. I did some tweaking and it's all in order now. I don't know how I missed my not calling dlg.ShowModal() correctly. And my appologies sep2k, I was In a hurry typing this and posted wothout really describing the problem I was having. Thanks all!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1