Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีสร้างช่องป้อนรหัสผ่าน (Password Entry Field) ใน Tkinter


สมมติว่าคุณสร้าง Login Form สำหรับแอปพลิเคชัน Tkinter ในหลายกรณี การล็อกอินที่ดีต้องการรูปแบบมาตรฐานของ username, password และรายละเอียดผู้ใช้อื่นๆ ผู้ใช้สามารถป้อนรหัสผ่านในช่อง Entry ด้วยการผสมผสานตัวอักษรและตัวเลข โดยทั่วไป เพื่อสร้างการเชื่อมต่อที่ปลอดภัยระหว่างผู้ใช้และแอปพลิเคชัน ช่องรหัสผ่านจะเก็บอินพุตในรูปแบบตัวอักษร "*" เพื่อสร้างช่องที่รับอินพุตในรูปแบบ "*" ให้ใช้แอตทริบิวต์ show="*" ใน Widget Entry

ตัวอย่าง

ตัวอย่างต่อไปนี้มี Entry widget ที่รับรหัสผ่านในรูปแบบซ่อน หากคลิก "Show Password" จะแสดงรหัสผ่านบนหน้าจอ

# Import the required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")

# Define a function to show the entered password
def show():
    p = password.get()
    ttk.Label(win, text="Your Password is: " + str(p)).pack()

password = StringVar()

# Add an Entry widget for accepting User Password
entry = Entry(win, width=25, textvariable=password, show="*")
entry.pack(pady=10)

# Add a Button to reveal the password
ttk.Button(win, text="Show Password", command=show).pack()

win.mainloop()

ผลลัพธ์

การรันโค้ดจะแสดงช่องรหัสผ่านและปุ่มเปิดเผยรหัสผ่านบนหน้าจอ

วิธีสร้างช่องป้อนรหัสผ่าน (Password Entry Field) ใน Tkinter

คลิกปุ่ม "Show Password" เพื่อแสดงรหัสผ่านบนหน้าจอ

วิธีสร้างช่องป้อนรหัสผ่าน (Password Entry Field) ใน Tkinter