在"运行"之间保存用户输入在Python中

时间:2018-02-27 14:06:13

标签: python-3.x save user-input

我之所以这样问是因为我认为可能有更好的方法可以做到这一点,如果不是,至少它可以帮助那些试图做到这一点的人,即使它很笨重

所以,我想要的是询问用户一些输入,然后使用该输入来运行程序。但我不想每次都问它,所以程序应该保存最后一个输入并每次使用它,直到用户想要更新它

我使用tkinter创建了输入区域,并使用了一个txt文件来保存输入,但我觉得必须有一种更简单的方法来做到这一点^^

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()

0 个答案:

没有答案