TKiNTER - How to save string from Entry ()

0

I have a problem with tkinter, I am not able to save the value of an Entry () in any variable so that I can use the name of that file to create a TXT.

Follow the code:

def criarPL ():
    global nome_pl
    def salvar():
        arq = open("{0}.txt".format(nome_pl), "w")
        arq.close()

    janela2 = Tk()
    janela2.title("Criar Playlist")

    bt_salvar = Button(janela2, width=20, text="SALVAR", command=salvar)

    nome_pl = Entry(janela2)
    nome_pl.insert(END, "NOME DA PLAYLIST\n")
    nome_pl.pack()

    nome_pl.place  (x=170, y=2)
    bt_salvar.place(x=2, y=2)

    janela2.geometry("400x27+250+250")
    janela2.mainloop()

If anyone can help me, I'll be grateful.

    
asked by anonymous 11.05.2017 / 00:09

1 answer

1

Well, I changed two things:

arq = open("{0}.txt".format(nome_pl), "w")

I put .get () at the end of nome_pl , I gave a read and get () serves to literally get the content, something like that. So when you click on the "Save" the file is generated with the name you typed.

arq = open("{0}.txt".format(nome_pl.get()), "w")

And this:

I took the "PLAYLIST NAME \ n", because it was conflicting because of the "\", which is a special character, which you can not put in the file name. Yes, I know, the name will not be "Playlist Name", but as the text does not add to clicking (at least here) and I do not know much about tkinter I did not find a way to do this, so I retired to not have which will erase, but I think that you should resolve.

nome_pl.insert(END, "NOME DA PLAYLIST\n")

nome_pl.insert(END,'')

Any questions, just comment.

    
11.05.2017 / 05:38