在Python TKinter中将条目转换为字符串

问题描述 投票:-1回答:1

我正在尝试在TKinter中编写一个简单的日志记录应用程序。基本上我需要比较具有给定字符串的条目(正确的传递)。不幸的是,每当我输入密码时,“entry.get()”方法的结果会使输入变得不同 - 打印它返回:

<bound method Entry.get of <tkinter.Entry object .!entry>>

代码

def start_page():
    start_window = Tk()
    ent_1 = Entry(start_window)
    but_1 = Button(start_window, text='Login', command=partial(login, ent_1.get))
    but_1.grid(row=1,column=1)
    ent_1.grid(row=0, column=1)
    start_window.mainloop()

def login(input_password):
    correct_password = 'password'
    if correct_password == input_password:
        coefficient_calc()
        start_window.destroy()
    else:
        print('You typed:', input_password)
        print('Please type the correct password.')

知道如何转换它或以期望的形式“获取”条目?我发现很少有类似的话题,但没有人回答这个问题。

python tkinter
1个回答
1
投票

login()的定义移到start_window()的范围内可以解决问题。无需将ent_1(和start_window)传递给login(),也无需使用functools.partial

def start_page():
    def login():
        input_password = ent_1.get()
        correct_password = 'password'
        if correct_password == input_password:
            coefficient_calc()
            start_window.destroy()
        else:
            print('You typed:', input_password)
            print('Please type the correct password.')

    start_window = Tk()
    ent_1 = Entry(start_window)
    but_1 = Button(start_window, text='Login', command=login)
    but_1.grid(row=1,column=1)
    ent_1.grid(row=0, column=1)
    start_window.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.