[英]Python Pass Parameters using tkinter command button
我正在創建一個登錄系統,用戶輸入密碼和用戶名,按下按鈕后,它會轉到 function 以檢查密碼。
from tkinter import *
def login():
Usr = Tk()
Usr.title("Login")
Usr.geometry("200x150+860+400")
Usr.configure(bg="grey22")
usrBox = Label(Usr, text = "Username:", font=( "arial",12, "bold"), fg="white", bg="grey22").place(x=50, y=10)
passbox = Label(Usr, text = "Password:", font=( "arial",12, "bold"), fg="white", bg="grey22").place(x=50, y=60)
usrName = StringVar()
usernameInput = ""
passwordInput = ""
PssWord = StringVar()
usrName = Entry(Usr, textvariable= usernameInput, width=15, bg="lightgrey").place(x=50, y=37)
PssWord = Entry(Usr, textvariable= passwordInput, width=15, bg="lightgrey").place(x=50, y=87)
enter = Button(Usr, text = "login", width=11, height = 1, bg="lightgrey", activebackground="grey", font=("arial", 10, "bold"), command = checkPassword: action(usernameInput, passwordInput)).place(x=50, y=110)
Usr.mainloop()
def checkPassword(usernameInput, passwordInput):
print(usernameInput, passwordInput)
login()
該操作返回無效語法
您的代碼存在多個問題:
lambda
調用checkPassword
function。StringVar
,然后將純字符串傳遞給 Entry 字段並在回調中使用它們; 那些不會用實際值更新,請改用StringVar
。x = Widget(...).layout(...)
,則x
不是小部件而是None
,這是所有布局功能( pack
、 grid
、 place
等)的結果,這是不過,這不是問題,因為無論如何您都不會使用所有這些變量固定代碼(摘錄)
usernameInput = StringVar()
passwordInput = StringVar()
Entry(Usr, textvariable=usernameInput, ...).place(x=50, y=37)
Entry(Usr, textvariable=passwordInput, ...).place(x=50, y=87)
Button(Usr, text="login", ..., command=lambda: checkPassword(usernameInput, passwordInput)).place(x=50, y=110)
然后,在checkPassword
function 中,使用StringVar.get()
獲取實際值。
您可以使用:
usrName = Entry(Usr, bd=3)
usrName.place(x=75, y=35)
enter = Button(Usr, text="Send", width=15, height=2,
command=lambda: checkPassword(usrName.get()))
enter.place(x=25, y=75)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.