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

โปรแกรมพจนานุกรม (Word Dictionary) ด้วย Python Tkinter

ในบทความนี้ เราจะสร้างโปรแกรมพจนานุกรมแบบ GUI โดยใช้ PyDictionary และโมดูล Tkinter

PyDictionary เป็นโมดูล Python ที่ช่วยให้ได้ความหมาย การแปล คำตรงข้าม (antonyms) และคำพ้องความหมาย (synonyms) ของคำศัพท์ ใช้ WordNet สำหรับความหมาย Google สำหรับการแปล และ synonym.com สำหรับคำพ้องความหมายและคำตรงข้าม PyDictionary ใช้ BeautifulSoup และ Requests module เป็น dependencies

เพื่อสร้างแอปพลิเคชัน เราจะติดตั้งโมดูลเหล่านี้ใน environment โดยใช้คำสั่ง pip install PyDictionary

หลังติดตั้งแล้ว เราจะสร้าง tkinter frame และองค์ประกอบอื่นๆ

ตัวอย่าง

# Import Required Libraries
from tkinter import *
from PyDictionary import PyDictionary

# Create instances and objects
dictionary = PyDictionary()
win = Tk()

# Define the size of the window
win.geometry("700x400")
win.title("Python Dictionary")

# Define Helper Function to use other attributes of PyDictionary Class
def dict():
    meaning.config(text=dictionary.meaning(word.get())['Noun'][0])

# Define Labels and Buttons
Label(win, text="Dictionary", font=("Times New Roman", 20)).pack(pady=20)

# Frame 1
frame = Frame(win)
Label(frame, text="Type any Word ", font=("Poppins bold", 15)).pack(side=LEFT)
word = Entry(frame, font=("Times New Roman", 15))
word.pack()
frame.pack(pady=10)

# Frame 2
frame1 = Frame(win)
Label(frame1, text="Meaning:", font=("Arial", 18)).pack(side=LEFT)
meaning = Label(frame1, text="", font=("Poppins", 15), width=30)
meaning.pack()
frame1.pack(pady=10)

Button(win, text="Find", font=("Poppins bold", 15), command=dict).pack()

# Execute Tkinter
win.mainloop()

ผลลัพธ์

การรันโค้ดข้างต้นจะสร้างและแสดงแอปพลิเคชันพจนานุกรม อย่างไรก็ตาม ด้วย PyDictionary เราสามารถเพิ่มคุณสมบัติอื่นๆ เช่น การหาคำพ้องความหมาย คำตรงข้าม เป็นต้น

โปรแกรมพจนานุกรม (Word Dictionary) ด้วย Python Tkinter

พิมพ์ "Hello" ในกล่องข้อความและคลิกปุ่ม "Find" จะดึงความหมายของ "Hello" จากพจนานุกรมมาแสดง

โปรแกรมพจนานุกรม (Word Dictionary) ด้วย Python Tkinter