簡體   English   中英

Python存儲用戶輸入

[英]Python storing user input

我完全沒有可用的代碼,但是如果我想接受用戶輸入並存儲它,然后接受不同的輸入並將其存儲到同一列表中(就像一個站點,該站點存儲了其成員的登錄信息並關聯了該站點當他們想重新登錄時)如何在python中執行此操作? 我有這個簡短的小代碼:

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

但這會產生以下結果:函數列表位於0x7fdf1fa0f668

而不是(例如)約翰尼

您沒有在調用函數,因此看到了對該函數的引用,還需要傳遞參數:

print List(param) 

您真正想要的是刪除參數,然后調用函數:

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

或者簡單地:

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

一個簡單的示例,將輸入內容保存到文件中,顯然,真實的用戶名和密碼需要更安全地存儲:

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

顯然,您應該驗證用戶是否確實輸入了兩者,並且必須用真實的字眼來查看用戶名是否已被使用等。

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?"),""))

將打印您給它的所有名稱,直到不給它任何字符串

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM