簡體   English   中英

如何借助按鈕顯示和隱藏Tkinter中的密碼?

[英]How to show and hide password in Tkinter with the help of button?

我想在 Tkinter 中創建一個顯示和隱藏按鈕,單擊它可以使密碼可見,單擊隱藏它會隱藏密碼。 首先,我使用了一個 Entry 小部件來輸入密碼,並在 (show="*") 的幫助下,將密碼輸入為隱藏密碼的 * 格式。 但是如果我想檢查我輸入的內容,我需要一個顯示按鈕,然后在確認后再次隱藏它。

您可以使用show=''顯示密碼並使用show='*'隱藏密碼:

import tkinter as tk

def toggle_password():
    if passwd_entry.cget('show') == '':
        passwd_entry.config(show='*')
        toggle_btn.config(text='Show Password')
    else:
        passwd_entry.config(show='')
        toggle_btn.config(text='Hide Password')

root = tk.Tk()

passwd_entry = tk.Entry(root, show='*', width=20)
passwd_entry.pack(side=tk.LEFT)

toggle_btn = tk.Button(root, text='Show Password', width=15, command=toggle_password)
toggle_btn.pack(side=tk.LEFT)

root.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM