0

Hello, I am newish to wxPython and I am attmpting to make a text editor
Everytime it attempts the open file code it gets this error:
IOError: [Errno 22] invalid mode ('r') or filename: u''
WHY!!!
here is the code...

import wx
import os
wildcard = "Text file (*.txt)|*.txt;|"\
         "All files (*.*)|*.*"

class frame(wx.Frame):
    def __init__(self,parent,id):    
        wx.Frame.__init__(self,parent,id,'Simple Text Control!', size=(478,300))
        panel=wx.Panel(self)
        edit=wx.TextCtrl(panel,1,size=(300,220), pos=(2,2), style = wx.TE_MULTILINE)
        btn=wx.Button(panel, -1, "Open File", pos=(342,15), size=(95,32))
        self.Bind(wx.EVT_BUTTON, self.Open, btn)
    def Open(self, event):
        dlg = wx.FileDialog(
        self, message="Grab your file...",
        defaultFile="",
        wildcard=wildcard,
        style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR
        )
        self.dirname=dlg.GetDirectory()
        self.filename=dlg.GetFilename()
        if dlg.ShowModal() == wx.ID_OK:
            filehandle=open(os.path.join(self.dirname, self.filename),'ab')
            self.edit.SetValue(filehandle.read())
            filehandle.close()
            dlg.Destroy()

if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=frame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by pyTony
0

You are reading filename and dirname you show the dialog, those lines must be inside if block. 'ab' is strange mode to open file you only read. Also there is not defined variable 'self.edit'.

Edited by pyTony

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.