1

I'm making a coding language in python and I want people to be able to save their code in a file with their own file name.

I've tried coding it but it keeps coming up with the error:
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Here is my current code:

import os

root = Tk()

def save_name():
    global file_title
    save_namewindow = Toplevel(root)
    save_namewindow.title("Save")
    Label(save_namewindow, text="Name the File").pack()
    file_title = Entry(save_namewindow).pack()
    Button(save_namewindow, text="Save", command=save).pack()

def save():
   file_name = (file_title + ".txt")
   if os.path.isfile(file_name):
       if messagebox.askyesno("File Exists", "Would you like to overwrite the existing file '" + file_name + "'?"):
         os.remove(file_name)
         file = open(file_name, "x")
         file.write(shell.get("1.0", END))
         file.close()
   else:
     file = open(file_name, "x")
     file.write(shell.get("1.0", END))
     file.close()

I want the answer to be: file_name = file_title + '.txt' in the correct way. Thanks in advance.

a_guest
  • 34,165
  • 12
  • 64
  • 118
  • That is a correct enough way to concatenate a string onto another string. But you have only declared `file_title` to be the name of a global variable inside the `save_name()` function. Inside `save()` the name `file_title` is still local. Put `global file_title` inside `save()` as well. This might work. This is not the correct way to pass data around a Tk() program, almost certainly, but your code does not look complete and that would be a more complex answer, e.g. https://stackoverflow.com/questions/32212408/how-to-get-variable-data-from-a-class – TessellatingHeckler Apr 04 '19 at 23:51
  • That didn't work – Jayden Collis Apr 05 '19 at 00:00
  • Please consider adding a code sample, or revising the one you posted in this question. As it currently stands, its formatting and scope make it hard for us to help you; here is a [great resource](http://stackoverflow.com/help/mcve) to get you started on that. Good luck with your code! – Reblochon Masque Apr 05 '19 at 00:06
  • all I want is for someone to tell me how to concentrate a string with another variable – Jayden Collis Apr 05 '19 at 00:09
  • "*all I want is for someone to tell me how to concentrate a string with another variable*" - `string1 + string2`, it's correct enough, as I said. Your problem is that within `save()`, `file_title` *is not a string*. Your problem is not adding two strings, your problem is passing data between different functions inside a TKinter GUI. – TessellatingHeckler Apr 05 '19 at 01:04
  • Yeah, that seemed to work. Thanks a lot. – Jayden Collis Apr 05 '19 at 02:57

1 Answers1

0

I see two mistakes in code

First:

file_title = Entry(save_namewindow).pack()

It assigns None to file_title because pack() returns None.
So you don't have access to Entry.
You have to do it in two steps

file_title = Entry(save_namewindow)
file_title.pack()

And now you have access to Entry.

Second:

To get text from Entry you have to use .get(). It means file_title.get().

So you need

file_name = file_title.get() + ".txt"
furas
  • 134,197
  • 12
  • 106
  • 148