为什么我的Tkinter登录按钮在程序中不起作用?

时间:2018-11-21 17:25:10

标签: python tkinter

这是一个简单的登录屏幕。我不知道这是怎么回事。当我单击登录时,什么都没有发生。请告知。

from tkinter import *
from tkinter import messagebox

window = Tk()
window.title("PyOS")
window.geometry('1000x500')


user = Label(window,text="Username")
pass1 = Label(window,text="Password")
login = Button(window,text="Login")
def clicked(login):
    wrong = Label(window,text="Wrong Password")
    wrong.grid(column=1,row=2)
userentry = Entry(window,font="Sanserif",width=30)
passentry = Entry(window,font="Marlett",show="n",width=17)
userentry.grid(column=1, row=0)
passentry.grid(column=1, row=1)
user.grid(column=0, row=0)
pass1.grid(column=0, row=1)
login.grid(column=0, row=2)

window.mainloop()

1 个答案:

答案 0 :(得分:1)

您需要告诉按钮在按下时执行哪个功能。这是通过使用Button的command参数来完成的。

from tkinter import *
from tkinter import messagebox

window = Tk()
window.title("PyOS")

def clicked():
    wrong = Label(window,text="Wrong Password")
    wrong.grid(column=1,row=2)

user = Label(window,text="Username")
pass1 = Label(window,text="Password")
login = Button(window,text="Login", command=clicked)

userentry = Entry(window,font="Sanserif")
passentry = Entry(window,font="Marlett", show="*")
userentry.grid(column=1, row=0)
passentry.grid(column=1, row=1)
user.grid(column=0, row=0)
pass1.grid(column=0, row=1)
login.grid(column=0, row=2)

window.mainloop()

enter image description here