第一:不要这样做
usr = Entry().pack()
因为它有助于
None
到
usr
因为
pack()
总是返回
没有
然后你就没有机会
Entry
以获取用户名及其密码。你必须分两步做
usr = Entry()
usr.pack()
对于标签和按钮,您甚至可以跳过变量,因为您不需要它们。
第二:您可以使用
command=function_name
. 必须是没有名字的
()
(所谓)
"callback"
)
tk.Button(mainframe, text="Log in", command=ask_user)
当你点击按钮时
mainloop
将运行
ask_user()
里面
ask_user
你可以从
条目
使用
.get()
username = usr.get()
password = pwd.get()
完整代码:
import tkinter as tk
# --- functions ---
def ask_user():
username = usr.get()
password = pwd.get()
if username == "Ed" and password == "true":
app()
if username == "buxton" and password == "logix":
app()
else:
root.destroy()
# --- main ---
root = tk.Tk()
mainframe = tk.Frame(root)
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
mainframe.pack(pady=100, padx=100)
tk.Label(mainframe, text="Username").pack()
usr = tk.Entry(mainframe)
usr.pack()
tk.Label(mainframe, text="Password").pack()
pwd = tk.Entry(mainframe)
pwd.pack()
tk.Button(mainframe, text="Log in", command=ask_user).pack()
root.mainloop()