在tkinter入口小部件上使用get()函数

时间:2019-01-27 18:26:52

标签: python tkinter

我正在尝试创建标准的用户ID / PASS登录。当我使用下一个功能检查输入的密码和名称是否正确时,总是收到“输入错误的值”消息。基本上,变量entry_1和entry_2不存储输入文本,我想要一种解决方案。也许你们每个人都可以为此提出解决方案?

我尝试将entry_1和entry_2分配给变量,但没有成功。

from tkinter import *

root = Tk() # creates a window and initializes the interpreter
root.geometry("500x300")

name = Label(root, text = "Name")
password = Label(root, text = "Password")
entry_1 = Entry(root)
entry_2 = Entry(root)



name.grid(row = 0, column = 0, sticky = E) # for name to be at right use sticky = E (E means east)
entry_1.grid(row = 0, column =1)
x = "Taha"
password.grid(row = 1, column = 0)
entry_2.grid(row = 1, column =1)
y = "123"
c = Checkbutton(root, text = "Keep in logged in").grid(columnspan = 2 ) # mergers the two columns


def next():
    if a == entry_1 and b == entry_2:
        print ("Proceed")
    else:
        print("wrong values entered")

def getname():
    return name


Next = Button(root, text = "Next", command=next).grid(row = 3, column = 1)




root.mainloop() # keep runing the code

我希望程序在输入正确的值后返回“继续”。

2 个答案:

答案 0 :(得分:0)

在您的代码中,您无需检查任何地方的用户输入。您应该使用get()返回用户输入。我已经相应地修改了您的代码。现在,如果您输入Taha作为用户名,输入123作为密码,您将收到“继续”消息。

from tkinter import *

root = Tk()  # creates a window and initializes the interpreter
root.geometry("500x300")

name = Label(root, text="Name")
password = Label(root, text="Password")
entry_1 = Entry(root)
entry_2 = Entry(root)

name.grid(row=0, column=0, sticky=E)  # for name to be at right use sticky = E (E means east)
entry_1.grid(row=0, column=1)
x = "Taha"
password.grid(row=1, column=0)
entry_2.grid(row=1, column=1)
y = "123"
c = Checkbutton(root, text="Keep in logged in").grid(columnspan=2)  # mergers the two columns


def next_window():
    user_name = entry_1.get()
    user_pass = entry_2.get()
    if x == user_name and y == user_pass:
        print("Proceed")
    else:
        print("wrong values entered")


def get_name():
    return name


Next = Button(root, text="Next", command=next_window).grid(row=3, column=1)

root.mainloop()

答案 1 :(得分:0)

感谢提供帮助的人员,在您的帮助下,我可以找到代码中缺少的部分。我应该使用.get()函数才能取回输入的文本。 这是经过改进的升级代码。

structure(list(`Indicator Name` = c("Alpha", "Beta", "Beta", 
"Beta", "Charlie", "Charlie", "Charlie", "Delta", "Echo", NA, 
NA, "Foxtrot", "Foxtrot"), Examine = c(NA, 2013, 2017, NA, 2013, 
2017, NA, 2016, 2016, NA, NA, 2007, NA)), row.names = c(NA, 13L
), class = "data.frame")