Python TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Question: I have looked into this before asking my question, but haven't been able to
find anything that fits in with my situation.

I'm writing a Python program - a text editor; using Python and Gtk+3.

Here is the error I'm getting:

    
    
    Traceback (most recent call last):
      File "file.py", line 58, in on_s_pressed
        if (self.set_title == (filename + " - DeSedit")):
    TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
    

And here is my code:

    
    
    #!/usr/bin/env python3
    
    from gi.repository import Gtk, Gdk
    
    class DeSedit(Gtk.Window):
        def __init__(self):
            Gtk.Window.__init__(self, title="DeSedit")
    
        self.set_default_size(650, 500)
    
        # keyboard shortcuts
        """ <Control>O """
        accel = Gtk.AccelGroup()
        accel.connect(Gdk.keyval_from_name('O'), Gdk.ModifierType.CONTROL_MASK, 0, self.on_o_pressed)
        self.add_accel_group(accel)
        """ <Control>S """
        accel1 = Gtk.AccelGroup()
        accel1.connect(Gdk.keyval_from_name('S'), Gdk.ModifierType.CONTROL_MASK, 0, self.on_s_pressed)
        self.add_accel_group(accel1)
    
        # grid to organize widgets
        self.box = Gtk.Box()
        self.add(self.box)
        # text view
        self.textview = Gtk.TextView()
        self.textview.set_wrap_mode(True)
        self.textbuffer = self.textview.get_buffer()
        # scroll bar
        scrollwindow = Gtk.ScrolledWindow()
        scrollwindow.add(self.textview)
        self.box.pack_start(scrollwindow, True, True, 0)
    
    # open file dialog
    def on_o_pressed(self, *args):
        openDialog = Gtk.FileChooserDialog("Select file to be opened", self,
            Gtk.FileChooserAction.OPEN,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
            Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
        response = openDialog.run()
        if response == Gtk.ResponseType.OK:
            filename = openDialog.get_filename()
            with open(filename, 'r') as fRead:
                data = fRead.read()
                self.textbuffer.set_text(data)
                self.set_title(filename + " - DeSedit")
                fRead.close()
            openDialog.destroy()
        elif response == Gtk.ResponseType.CANCEL:
            openDialog.destroy()
    
    # save file dialog
    def on_s_pressed(self, *args):
        saveDialog = Gtk.FileChooserDialog("Select folder to save file", self,
            Gtk.FileChooserAction.SAVE,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
            Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
        filename = saveDialog.get_filename()
        if self.set_title == (filename + " - DeSedit"):
            print("hmm")
        response = saveDialog.run()
        if response == Gtk.ResponseType.OK:
    
            with open(filename, 'w') as fWrite:
                data = self.textbuffer.get_text(self.textbuffer.get_start_iter(),
                                                self.textbuffer.get_end_iter(), True)
                fWrite.write(data)
                fWrite.close()
            saveDialog.destroy()
        elif response == Gtk.ResponseType.CANCEL:
            saveDialog.destroy()
    
    
    
    window = DeSedit()  # create DeSedit object
    window.connect("delete-event", Gtk.main_quit)
    window.show_all()
    Gtk.main()
    


Answer: It basically tells you `filename` is a NoneType object, which means
`saveDialog.get_filename()` returnded `None`.

According to [the doc](http://www.pygtk.org/pygtk2reference/class-
gtkfilechooser.html#method-gtkfilechooser--get-filename) this means no file
was selected

> gtk.FileChooser.get_filename
>
> def get_filename()
>
> Returns : The currently selected filename, or None if no file is >selected,
> or the selected file can't be represented with a local filename.

