Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách tạo ô nhập mật khẩu (Password Entry) trong Tkinter


Để ô Entry hiển thị mật khẩu dưới dạng dấu sao (*), thiết lập thuộc tính show="*".

Ví dụ minh họa

from tkinter import *
from tkinter import ttk

win = Tk()
win.geometry("700x350")

def show():
    p = password.get()
    ttk.Label(win, text="Mật khẩu của bạn: " + str(p)).pack()

password = StringVar()
Entry(win, width=25, textvariable=password, show="*").pack(pady=10)
ttk.Button(win, text="Hiện mật khẩu", command=show).pack()

win.mainloop()

Kết quả đầu ra

Ô nhập mật khẩu hiển thị *, nút Hiện mật khẩu in ra mật khẩu thật.

Cách tạo ô nhập mật khẩu (Password Entry) trong Tkinter

Nhấn nút hiện mật khẩu.

Cách tạo ô nhập mật khẩu (Password Entry) trong Tkinter