登录按钮功能

时间:2018-11-25 16:26:45

标签: python tkinter raspberry-pi3

我正在使用python中的tkinter

此刻,我已经创建了一个脚本,该脚本创建了一个没有任何功能的登录窗口。我尝试使用def callback():command=callback。因此,我尝试这样做,以便在您按下“登录”按钮时可以执行某些操作(例如,显示正在加载...并清除文本框。)

代码如下:

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

1 个答案:

答案 0 :(得分:2)

存在三个问题:

  1. 需要将回调函数分配给Button小部件上的command选项。
  2. 两个条目窗口小部件需要不同的变量名才能在回调中访问
  3. 回调函数需要执行某些操作的代码主体。

代码应为

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()
相关问题