Python的tkinter智能入口密码
Python's tkinter smart entry password
我想创建一个密码条目。
一个简单的解决方案是:
password = Entry(root, font="Verdana 22")
password.config(show="*");
但问题是为了避免拼写错误,我想显示被点击的项目仅显示几秒钟,而其他所有内容都被隐藏。几秒钟后,一切都被隐藏了。
用 Tkinter 做你想做的事情并不容易,但这里有一些接近的东西:当你按下一个键时,它会显示条目的全部内容,但一秒钟后文本会再次隐藏。
我在 Python 2 上开发了这个;在 Python 上使用它 3 将 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()
仅显示最后输入的字符会很棘手。您必须手动修改显示的字符串,同时在单独的变量中维护真实的密码字符串,这有点繁琐,因为用户可以随时移动插入点光标。
最后一点,我真的不建议这样做任何事情。将密码隐藏 所有 次!如果要减少新选择的密码出现错别字的几率,通常的做法是让用户输入两次密码。
这是一个使用复选按钮显示密码的简单技巧。
选中后密码将可见
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()
我想创建一个密码条目。
一个简单的解决方案是:
password = Entry(root, font="Verdana 22")
password.config(show="*");
但问题是为了避免拼写错误,我想显示被点击的项目仅显示几秒钟,而其他所有内容都被隐藏。几秒钟后,一切都被隐藏了。
用 Tkinter 做你想做的事情并不容易,但这里有一些接近的东西:当你按下一个键时,它会显示条目的全部内容,但一秒钟后文本会再次隐藏。
我在 Python 2 上开发了这个;在 Python 上使用它 3 将 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()
仅显示最后输入的字符会很棘手。您必须手动修改显示的字符串,同时在单独的变量中维护真实的密码字符串,这有点繁琐,因为用户可以随时移动插入点光标。
最后一点,我真的不建议这样做任何事情。将密码隐藏 所有 次!如果要减少新选择的密码出现错别字的几率,通常的做法是让用户输入两次密码。
这是一个使用复选按钮显示密码的简单技巧。 选中后密码将可见
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()