如何使用Tkinter创建密码输入字段

Hic*_*ick 34 python tkinter

我正在尝试使用Tkinter编写登录窗口,但我无法以星号格式隐藏密码文本.这意味着密码输入是纯文本,必须避免.知道怎么做吗?

Fed*_*oca 64

快速谷歌搜索产生了这一点

widget = Entry(parent, show="*", width=15)
Run Code Online (Sandbox Code Playgroud)

其中widget是文本字段,parent是父窗口小部件(窗口,框架,等等),show是要回显的字符(即显示的字符Entry),width是窗口小部件的宽度.

  • 无论如何,我可以在使用 Python 3.7 的 Mac 上对此进行讨论:复制仅复制 *,因此安全性似乎足以满足大多数用途。 (2认同)
  • 我是使用 Python 3.11.4 的 Windows 11 用户,可以确认复制该字段也会在此处复制一堆星号。 (2认同)

Shu*_*ule 21

如果您不想创建全新的Entry小部件,则可以执行以下操作:

myEntry.config(show="*");
Run Code Online (Sandbox Code Playgroud)

要使其恢复正常,请执行以下操作:

myEntry.config(show="");
Run Code Online (Sandbox Code Playgroud)

我通过检查前面的答案,并使用Python解释器中的帮助功能(例如帮助(tkinter.Entry)导入后(从扫描那里的文档)发现了这一点.我承认我只是想知道如何让它恢复正常.


小智 7

widget-name = Entry(parent,show="*")
Run Code Online (Sandbox Code Playgroud)

您还可以使用项目符号:

bullet = "\u2022" #specifies bullet character
widget-name = Entry(parent,show=bullet)#shows the character bullet
Run Code Online (Sandbox Code Playgroud)


lox*_*sat 7

这是一个小的,非常简单的演示应用程序,使用Tkinter隐藏和获取密码.

#Python 3.4 (For 2.7 change tkinter to Tkinter)

from tkinter import * 

def show():
    p = password.get() #get password from entry
    print(p)


app = Tk()   
password = StringVar() #Password variable
passEntry = Entry(app, textvariable=password, show='*').pack() 
submit = Button(app, text='Show Console',command=show).pack()      
app.mainloop() 
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!