Tkinter เป็นไลบรารี Python ที่รู้จักกันดีสำหรับการสร้างแอปพลิเคชันที่ใช้ GUI คุณสามารถสร้างแอปพลิเคชันที่มีคุณลักษณะครบถ้วนโดยใช้วิดเจ็ต ฟังก์ชัน และโมดูลที่มีอยู่แล้วในไลบรารี Tkinter
บางครั้ง การเลือก GUI ที่ถูกต้องของแอปพลิเคชันกลายเป็นงานที่น่ากลัวสำหรับพวกเราหลายคน Tkinter มาพร้อมกับชุดฟังก์ชันและส่วนขยายที่ฝังไว้เพื่อสร้าง GUI ที่ดูดี
โดยทั่วไป วิดเจ็ตเฟรมใน Tkinter จะจัดการวิดเจ็ตทั้งหมดในแอปพลิเคชันเป็นคอนเทนเนอร์ มันสืบทอดคุณสมบัติทั้งหมดที่หน้าต่างหลักสามารถมีได้ ในการออกแบบเลย์เอาต์ของวิดเจ็ต เราสามารถใช้ตัวจัดการเรขาคณิตใดก็ได้ Geometry Manager ช่วยในการสร้างเลย์เอาต์ของวิดเจ็ตและจัดวางในลำดับที่แน่นอน ตัวจัดการเรขาคณิตของกริดจะวางวิดเจ็ตทั้งหมดในรูปแบบของระบบพิกัด X และ Y เราสามารถจัดเตรียม แถว และ คอลัมน์ คุณสมบัติเพื่อวางวิดเจ็ตไว้ที่ใดก็ได้ในแอปพลิเคชัน
ตัวอย่าง
ในตัวอย่างนี้ เราจะสร้าง GUI ของแบบฟอร์มการลงทะเบียน
# Import the Required libraries
from tkinter import *
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
# Add a Frame widget
frame = Frame(win)
# Define a function to get the data and display a message
def on_key_up():
name = fname
frame.pack_forget()
win.configure(bg="green4")
Label(win, text="Hey " + fname.get() + " Welcome to TutorialsPoint",
font=('Segoe UI', 18, 'bold'),
background="white").pack(pady=30)
# Create a Label widget
Label(frame, text="Registration Form",
font=('Helvetica 16 bold'),
background="green3").grid(row=5, column=0, pady=30)
frame.pack()
# Add Field for First Value
Label(frame, text="First Name").grid(row=7, column=0, padx=5)
fname = Entry(frame)
fname.grid(row=10, column=0, padx=10)
# Add Field for Second Value
Label(frame, text="Family name").grid(row=12, column=0, padx=5)
sname = Entry(frame)
sname.grid(row=15, column=0, padx=10)
# Add Field for Email Address
Label(frame, text="Email address").grid(row=17, column=0, padx=5)
email = Entry(frame)
email.grid(row=20, column=0, padx=10)
# Add another field for accepting password value
Label(frame, text="Enter a Password").grid(row=22, column=0, padx=5)
password = Entry(frame, show="*")
password.grid(row=25, column=0, padx=10)
# Create a button
Button(frame, text="Register", command=on_key_up).grid(row=15,
column=1, padx=5)
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดข้างต้นจะแสดงเทมเพลตแบบฟอร์มการลงทะเบียนและปุ่มสำหรับลงทะเบียนข้อมูล
