First Off. Total newbie here and first thread so go easy on me. I used the tut from the Starting wxPython (GUI code) thread. I have converted it to a text editor and added some annoying features to mess with my boss. I am able to open a file, edit it in the window but when I go to save, it saves a empty file. I have searched the docs, this thread as well as others and I cannot figure it out. Also I know there must be a way to bind my annoying "are you sure" messages instead of writing it twice, so any suggestions would be great. The code is as follows:

import wx
import os

WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400

class MainFrame(wx.Frame):
    
    """We've finally added menubars."""
    def __init__(self):
        wx.Frame.__init__(self, None, title = 'Bob\'s New Editor',
                 pos = (200,75), size = (WINDOW_WIDTH, WINDOW_HEIGHT))
        self.Bind(wx.EVT_CLOSE, self.OnClose)

        # Menubar.  
        self.menuBar = wx.MenuBar()
        self.menuFile = wx.Menu() # Create a dropdown menu for 'File'
        self.menuInfo = wx.Menu() # Create a dropdown menu for 'Info'
        self.SetMenuBar(self.menuBar) # Tell the main frame what menubar to use.

        self.menuBar.Append(self.menuFile, '&File') # Add a menu.
        
        self.loadItem = self.menuFile.Append(-1, '&Load') # Add an item to the menu.
        self.Bind(wx.EVT_MENU, self.loadEvent, self.loadItem)
        # Bind an event to the menu item. Note how for menu items the Bind format is different?
        # I specify the widget to be bound as the last parameter, not just before 'Bind', as I usually do.
        # You can use this version for any binding you do, if you want. It's necessary for menus, though.
       
        self.exitItem = self.menuFile.Append(-1, 'E&xit')
        self.Bind(wx.EVT_MENU, self.exitEvent, self.exitItem)
        
        self.menuBar.Append(self.menuInfo, '&Info')
        # Add another menu. Note how their order on the menubar depends on the order they appear in your code?
        
        self.aboutItem = self.menuInfo.Append(-1, '&About')
        self.Bind(wx.EVT_MENU, self.aboutEvent, self.aboutItem)
        
        self.helpItem = self.menuInfo.Append(-1, '&Help')
        self.Bind(wx.EVT_MENU, self.helpEvent, self.helpItem)

        # Start of sizers and widgets contained within.
        self.background = wx.Panel(self)
       
        self.transferBtn = wx.Button(self.background, label = 'Load')
        self.transferBtn.Bind(wx.EVT_BUTTON, self.loadEvent)

        self.saveBtn = wx.Button(self.background, label='Save')
        self.saveBtn.Bind(wx.EVT_BUTTON, self.saveEvent)
        
        self.inputArea = wx.TextCtrl(self.background, style=wx.TE_PROCESS_ENTER | wx.HSCROLL)
        self.inputArea.Bind(wx.EVT_TEXT_ENTER, self.transferEvent)
        
        self.transferArea = wx.TextCtrl(self.background, style = wx.TE_MULTILINE)
        self.transferArea.SetBackgroundColour("White")
        self.horizontalBox = wx.BoxSizer()
        self.horizontalBox.Add(self.inputArea, proportion = 1, border = 0)
        self.horizontalBox.Add(self.transferBtn, proportion = 0, border = 0)
        self.horizontalBox.Add(self.saveBtn, proportion = 0, border = 0)

        self.verticalBox = wx.BoxSizer(wx.VERTICAL)
        self.verticalBox.Add(self.horizontalBox, proportion = 0, flag = wx.EXPAND, border = 0)
        self.verticalBox.Add(self.transferArea, proportion = 1, flag = wx.EXPAND, border = 0)

        self.background.SetSizer(self.verticalBox)
        self.Show()

    def loadEvent(self, event):
        """Create a load dialog box, load a file onto transferArea,
then destroy the load box"""
        loadBox = wx.FileDialog(self, message="Open",
                                defaultDir="C:\\", defaultFile="", style=wx.OPEN)
        if loadBox.ShowModal() == wx.ID_OK: # When the user clicks 'Open' , do this:
            filename = loadBox.GetPath()
            target = open(filename, 'r')
            self.transferArea.SetValue(target.read())
            target.close()
        loadBox.Destroy()
        # destroys the box so that we main return to program

    def saveEvent(self,event):
        saveBox = wx.FileDialog(self, message="Save",
                                defaultDir="C:\\", defaultFile="", style=wx.SAVE)
        if saveBox.ShowModal() == wx.ID_OK: # When the user clicks 'Save' , do this:
            filename = saveBox.GetPath()
            target = open(filename, 'w')
            self.transferArea.GetValue()
            target.close()
        saveBox.Destroy()

    def OnClose(self, event):
        dlg = wx.MessageDialog(self, 
            "Do you really want to close this application?",
            "Confirm Exit", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
        result = dlg.ShowModal()
        dlg.Destroy()
        if result == wx.ID_OK:
            self.Destroy()

        exitInfo = """Are you sure you want to exit?"""
        exitBox = wx.MessageDialog(self, message=exitInfo, caption='Exit',
                                    style=wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.OK)
        if exitBox.ShowModal() == wx.ID_OK: #message will stay until user clicks ok
            exitBox.Destroy()
            
        exitInfo = """Are you really sure you want to exit?"""
        exitBox = wx.MessageDialog(self, message=exitInfo, caption='Exit',
                                    style=wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.OK)
        if exitBox.ShowModal() == wx.ID_OK: #message will stay until user clicks ok
            exitBox.Destroy()

            exitInfo = """Are you really really sure you want to exit?"""
        exitBox = wx.MessageDialog(self, message=exitInfo, caption='Exit',
                                    style=wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.OK)
        if exitBox.ShowModal() == wx.ID_OK: #message will stay until user clicks ok
            exitBox.Destroy()


    def exitEvent(self, event):
        """Exit Program"""
        exitInfo = """Are you sure you want to exit?"""
        exitBox = wx.MessageDialog(self, message=exitInfo, caption='Exit',
                                    style=wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.OK)
        if exitBox.ShowModal() == wx.ID_OK: #message will stay until user clicks ok
            exitBox.Destroy()

            exitInfo = """Are you really sure you want to exit?"""
        exitBox = wx.MessageDialog(self, message=exitInfo, caption='Exit',
                                    style=wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.OK)
        if exitBox.ShowModal() == wx.ID_OK: #message will stay until user clicks ok
            exitBox.Destroy()

            exitInfo = """Are you really really sure you want to exit?"""
        exitBox = wx.MessageDialog(self, message=exitInfo, caption='Exit',
                                    style=wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.OK)
        if exitBox.ShowModal() == wx.ID_OK: #message will stay until user clicks ok
            exitBox.Destroy()


            
        self.Destroy()    

    def helpEvent(self, event):
        """Writes helpful infprmation to the transfer area"""
        self.transferArea.SetValue("""Could put help file here""")

    def aboutEvent(self, event):
        """Pops up a little message box to describe"""
        aboutInfo = """Hello there Kevin!
I sure do like this Python stuff!!"""
        aboutBox = wx.MessageDialog(self, message=aboutInfo, caption='About',
                                    style=wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.OK)
        if aboutBox.ShowModal() == wx.ID_OK: #message will stay until user clicks ok
            aboutBox.Destroy()

    def transferEvent(self, event):
        """Moves data in inputArea onto transferArea"""
        self.transferArea.SetValue(self.inputArea.GetValue())
    
app = wx.App(redirect=False)
window = MainFrame()
app.MainLoop()

In the function saveEvent:
the line self.transferArea.SetValue(target.read()) should be text = self.transferArea.GetValue() and then transfer the text to the file

Thanks kolosick but that does not seem to help. The line that you referred to is part of the read process which is working. It is the write process back to a file that is not working. It must be something in the

filename = saveBox.GetPath()
            target = open(filename, 'w')
            self.transferArea.GetValue()
            target.close()

I know it is something simple as I am not getting any traceback errors it simply does not write to text file.

As it seems that I keep writing a blank text could it be that I am pulling the text from the wrong area and therefore there is nothing actually to write?

You need to write the value/text you are getting to the file:
target.write(self.transferArea.GetValue())

DUH!! What a noob!! I was trying to open the new text file that I was attempting to save to. I was close though as I did try target.save(self.transferArea.GetValue()) and kept getting error messages about save not defined. Thank you so much for your help.

Any suggestions about my crazy and annoying "are you sure" messages when you attempt to close the frame? I know that there should be a way to just wire this once and bind to the close frame button as well as the exit in the menu function?

Any suggestions about my crazy and annoying "are you sure" messages when you attempt to close the frame? I know that there should be a way to just wire this once and bind to the close frame button as well as the exit in the menu function?

I suggest

def annoy(parent, how_much):
    for i in range(how_much):
        exitInfo = "Are you %ssure you want to exit ?" % ("really " * i)
        exitBox = wx.MessageDialog(parent, message=exitInfo, caption='Exit',
                                    style=wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.OK)
        if exitBox.ShowModal() == wx.ID_OK:
            exitBox.Destroy()

OK, that totally makes sense as far as creating the messages. Now I would then bind the EVT_BUTTON as well as the frame close button to the annoy, right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.