1

I'm asking this because I think there is probably a better way to do this, and if not at least it can help someone that is trying to do this, even if it is clunky

So, what I want is to ask the user some input, and then use that input to run the program. But i dont want to ask it everytime, so the program should save the last input and use it everytime until the user wants to update it

I created the input area with tkinter, and used a txt file to save the input, but I feel there has to be a way easier way to do this ^^

from tkinter import *
# opens the file and reads 1st line and assigns that value to "guardado1"..
ler = open("texto.txt", "r")
guardado1 = ler.readline()
guardado2 = ler.readline()
ler.close()

print (guardado1 + guardado2)
#this command is associated with a button, and it assigns 2 variables to the user input and writes/saves it on the text file
def actualizar():
    variavel1 = e1.get()
    variavel2 = e2.get()
    print("First Name: %s\nLast Name: %s" % (variavel1, variavel2))
    escrever = open("texto.txt", "w+")
    escrever.write(variavel1)
    escrever.write(variavel2)
    escrever.close()


#this creates the window that pops up, it writes the saved values in the respective input boxes, if there is none saved it will be a blank
master = Tk()
Label(master, text="valor1").grid(row=0)
Label(master, text="valor2").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.insert(10, guardado1)
e2.insert(10, guardado2)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
Button(master, text='Sair', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Actualizar', command=actualizar).grid(row=3, column=1, sticky=W, pady=4)

mainloop()
  • How do you define "easier"? This already looks pretty easy. – Bryan Oakley Feb 27 '18 at 19:04
  • it would be something that doesnt need to use an external file explicitly, and/or that would allow the end user to move the .exe without having the values reset to zero (if the .txt isnt moved as well) It would also be awesome if it could keep specific values for each windows user (i guess this can be done by saving the txt in the specific user folder, but it sounds tricky) I'm thinking this has to be such a basic need for many programs that there is some module to keep it simple – Pedro Sequeira Feb 28 '18 at 14:34

0 Answers0