Python';s tkinter智能输入密码
我想创建一个密码条目 一个简单的解决方案是:Python';s tkinter智能输入密码,python,python-3.x,tkinter,passwords,Python,Python 3.x,Tkinter,Passwords,我想创建一个密码条目 一个简单的解决方案是: password = Entry(root, font="Verdana 22") password.config(show="*"); 但问题是,为了避免输入错误,我希望显示单击的项目,使其仅在几秒钟内可见,而其他所有内容都隐藏起来。几秒钟后,所有内容都被隐藏。用Tkinter做你想做的事情并不容易,但有一点很接近:当你按下一个键时,它会显示条目的全部内容,但一秒钟后,文本再次被隐藏 我在Python2上开发了这个;要在Python3上使用它,请
password = Entry(root, font="Verdana 22")
password.config(show="*");
但问题是,为了避免输入错误,我希望显示单击的项目,使其仅在几秒钟内可见,而其他所有内容都隐藏起来。几秒钟后,所有内容都被隐藏。用Tkinter做你想做的事情并不容易,但有一点很接近:当你按下一个键时,它会显示条目的全部内容,但一秒钟后,文本再次被隐藏 我在Python2上开发了这个;要在Python3上使用它,请将
Tkinter
更改为Tkinter
import Tkinter as tk
class PasswordTest(object):
''' Password Entry Demo '''
def __init__(self):
root = tk.Tk()
root.title("Password Entry Demo")
self.entry = e = tk.Entry(root)
e.pack()
e.bind("<Key>", self.entry_cb)
b = tk.Button(root, text="show", command=self.button_cb)
b.pack()
root.mainloop()
def entry_cb(self, event):
#print(`event.char`, event.keycode, event.keysym )
self.entry.config(show='')
#Hide text after 1000 milliseconds
self.entry.after(1000, lambda: self.entry.config(show='*'))
def button_cb(self):
print('Contents:', repr(self.entry.get()))
PasswordTest()
将Tkinter作为tk导入
类密码测试(对象):
''密码输入演示''
定义初始化(自):
root=tk.tk()
root.title(“密码输入演示”)
self.entry=e=tk.entry(根)
e、 包()
e、 绑定(“,self.entry\u cb)
b=tk.Button(root,text=“show”,command=self.Button\u cb)
b、 包()
root.mainloop()
def入口_cb(自身、事件):
#打印(`event.char`,event.keycode,event.keysym)
self.entry.config(show='')
#1000毫秒后隐藏文本
self.entry.after(1000,lambda:self.entry.config(show='*'))
def按钮_cb(自身):
打印('Contents:',repr(self.entry.get())
密码测试()
只显示输入的最后一个字符是很棘手的。您必须手动修改显示的字符串,同时在单独的变量中维护真正的密码字符串,这有点麻烦,因为用户可以随时移动插入点光标
最后,我真的不建议做这样的事情。始终隐藏密码!如果您想减少新选择的密码中出现打字错误的机会,通常的做法是让用户输入两次密码 这是一个用复选按钮显示密码的简单技巧。 选中后,密码将可见
from Tkinter import *
from tkinter import ttk
def show_hide_psd():
if(check_var.get()):
entry_psw.config(show="")
else:
entry_psw.config(show="*")
window = Tk()
window.wm_title("Password")
window.geometry("300x100+30+30")
window.resizable(0,0)
entry_psw = Entry(window, width=30, show="*", bd=3)
entry_psw.place(x = 5, y = 25)
check_var = IntVar()
check_show_psw = Checkbutton(window, text = "Show", variable = check_var, \
onvalue = 1, offvalue = 0, height=2, \
width = 5, command = show_hide_psd)
check_show_psw.place(x = 5, y = 50)
window.mainloop()
问题的哪一部分需要帮助?或者你是要求我们为你编写整个代码?我已经默认隐藏了所有条目,但我遇到的问题是只显示添加到条目的最后一项隐藏了几秒钟。我不认为它在tkinter中是现成的。您可以使用
root.after
并记住用户的输入来实现它。或者添加一个显示/隐藏密码的复选框。谢谢。你能详细说明一下吗?之后很好,但不是OP要求的。