Tkinter - How to save Entry() string

Asked

Viewed 684 times

1

I have a problem about Tkinter, I am not able to save the value of an Entry() in any variable, so as soon as 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’d be grateful.

2 answers

2

Well, I changed two things:

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

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

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

And this:

I removed the "PLAYLIST NAME n", because it gave conflict on account of the " ", which is a special character, which you can not put in the name of the file. Yes, I know, the name will not be "Playlist name", but as the text does not disappear when clicking ( at least here ) and I do not know much Tkinter I did not find a way to do this, so I removed it so I do not have to erase, but I think you should solve this.

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

nome_pl.insert(END,'')

Any questions, just comment.

  • Thanks again haha.

  • is spinning here

0

Follow the code that worked here. I reversed the "pack" line order and the "Insert" line order, and in the "save" function, I used "name_pl.get()".

def criarPL ():
    global nome_pl
    def salvar():
        arq = open("{0}.txt".format(nome_pl.get()), "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.pack()
    nome_pl.insert(0, "NOME DA PLAYLIST")


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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.