Sergio
Sergio

Reputation: 53

Python storing user input

I don't have a working code at all for this, but if I wanted to take user input and store it, and then take a different input and store that to the same list (Like a site storing login information of it's members and associating it when they want to log back in) how would I do this in python? I have this short little code:

from Tkinter import *
import tkSimpleDialog
import tkMessageBox
root = Tk()
w = Label(root, text="")
w.pack()

User_info = tkSimpleDialog.askstring("User Information", "What is your name?")
def List(List_name):
    List_name = []
    List_name.append(User_info)
    return List_name
print List

yet this produces this result: function List at 0x7fdf1fa0f668

instead of (for instance) Johnny

Upvotes: 1

Views: 808

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

You are not calling the function so you are seeing a reference to the function, you also need to pass parameter:

print List(param) 

What you really want is to remove the parameter and just call the function:

User_info = tkSimpleDialog.askstring("User Information", "What is your name?")
def List():
    List_name = []
    List_name.append(User_info)
    return List_name
print List()

Or simply:

User_info = tkSimpleDialog.askstring("User Information", "What is your name?")
def List():
    return [List_name]
print List()

A simple example of taking and saving the input to a file, obviously real usernames and passwords would need to be stored a lot more securely:

master = Tk()
l = Label(master, text="Username")
l.pack()
 # create Entry for user
e = Entry(master)
e.pack()
l2 = Label(master, text="Password")
l2.pack()
# create Entry for pass and show * when user types their password
e2 = Entry(master,show="*")
e2.pack()
e.focus_set()

# callback function to save the username and password
def callback():
    with open("data.txt","a")as f:
        f.write("{},{}\n".format(e.get(),e2.get()))

# command set to callback when button is pressed
b = Button(master, text="Save", width=10, command=callback)
b.pack()

mainloop()

Obviously you should be verifying the user actually entered something for both and in the real word you would have to see if the username was taken etc..

Upvotes: 2

Joran Beasley
Joran Beasley

Reputation: 113930

from Tkinter import *
import tkSimpleDialog
import tkMessageBox
root = Tk()
w = Label(root, text="")
w.pack()

print list(iter( lambda x:tkSimpleDialog.askstring("User Information", "What is your name?"),""))

will print all the names you give it until you give it no strings

Upvotes: 2

Related Questions