登录按钮功能
Login Button Function
我正在 python 中试验 tkinter
。
目前我创建了一个脚本,它创建了一个登录名 window,但它从来没有任何功能。我试过 def callback():
和 command=callback
。所以我试着让它在你按下 "Login" 按钮时,你可以做一些事情(例如显示加载...和清除文本框。)
代码如下:
import tkinter
window = tkinter.Tk()
window.title("Login")
window.geometry("250x150")
window.configure(background="#FFFFFF")
label = tkinter.Label(window, text="Please Login to continue:", bg="#FFFFFF", font=("PibotoLt", 16))
label.pack()
label = tkinter.Label(window, text="username:", bg="#FFFFFF")
label.pack()
entry = tkinter.Entry(window)
entry.pack()
label = tkinter.Label(window, text="password:", bg="#FFFFFF")
label.pack()
entry = tkinter.Entry(window)
entry.pack()
def callback():
button = tkinter.Button(window, text="Login", fg="#FFFFFF", bg="#000000")
button.pack()
label = tkinter.Label(window, text="Loading...", bg="#FFFFFF", command=callback)
window.mainloop()
存在三个问题:
- 需要将回调函数分配给按钮小部件上的命令选项。
- 两个入口小部件在回调中需要不同的变量名才能访问
- 回调函数需要一段代码来做某事。
代码应该是
import tkinter
window = tkinter.Tk()
window.title("Login")
window.geometry("250x150")
window.configure(background="#FFFFFF")
label = tkinter.Label(window, text="Please Login to continue:", bg="#FFFFFF", font=("PibotoLt", 16))
label.pack()
label = tkinter.Label(window, text="username:", bg="#FFFFFF")
label.pack()
entry0 = tkinter.Entry(window) # Renamed entry0 to find in callback
entry0.pack()
label = tkinter.Label(window, text="password:", bg="#FFFFFF")
label.pack()
entry1 = tkinter.Entry(window) # Renamed entry1 to differentiate from entry0
entry1.pack()
def callback():
""" Callback to process a button click. This will be called whenever the button is clicked.
As a simple example it simply prints username and password.
"""
print("Username: ", entry0.get(), " Password: ", entry1.get())
button = tkinter.Button(window, text="Login", fg="#FFFFFF", bg="#000000", command=callback)
button.pack()
window.mainloop()
我正在 python 中试验 tkinter
。
目前我创建了一个脚本,它创建了一个登录名 window,但它从来没有任何功能。我试过 def callback():
和 command=callback
。所以我试着让它在你按下 "Login" 按钮时,你可以做一些事情(例如显示加载...和清除文本框。)
代码如下:
import tkinter
window = tkinter.Tk()
window.title("Login")
window.geometry("250x150")
window.configure(background="#FFFFFF")
label = tkinter.Label(window, text="Please Login to continue:", bg="#FFFFFF", font=("PibotoLt", 16))
label.pack()
label = tkinter.Label(window, text="username:", bg="#FFFFFF")
label.pack()
entry = tkinter.Entry(window)
entry.pack()
label = tkinter.Label(window, text="password:", bg="#FFFFFF")
label.pack()
entry = tkinter.Entry(window)
entry.pack()
def callback():
button = tkinter.Button(window, text="Login", fg="#FFFFFF", bg="#000000")
button.pack()
label = tkinter.Label(window, text="Loading...", bg="#FFFFFF", command=callback)
window.mainloop()
存在三个问题:
- 需要将回调函数分配给按钮小部件上的命令选项。
- 两个入口小部件在回调中需要不同的变量名才能访问
- 回调函数需要一段代码来做某事。
代码应该是
import tkinter
window = tkinter.Tk()
window.title("Login")
window.geometry("250x150")
window.configure(background="#FFFFFF")
label = tkinter.Label(window, text="Please Login to continue:", bg="#FFFFFF", font=("PibotoLt", 16))
label.pack()
label = tkinter.Label(window, text="username:", bg="#FFFFFF")
label.pack()
entry0 = tkinter.Entry(window) # Renamed entry0 to find in callback
entry0.pack()
label = tkinter.Label(window, text="password:", bg="#FFFFFF")
label.pack()
entry1 = tkinter.Entry(window) # Renamed entry1 to differentiate from entry0
entry1.pack()
def callback():
""" Callback to process a button click. This will be called whenever the button is clicked.
As a simple example it simply prints username and password.
"""
print("Username: ", entry0.get(), " Password: ", entry1.get())
button = tkinter.Button(window, text="Login", fg="#FFFFFF", bg="#000000", command=callback)
button.pack()
window.mainloop()