Computer >> Máy Tính >  >> Lập trình >> Python

Từ điển Word sử dụng Python Tkinter


Trong bài viết này, chúng tôi sẽ tạo một từ điển dựa trên GUI bằng PyDictionary và TkinterModule.

PyDictionary là một Mô-đun Python giúp nhận bản dịch nghĩa, từ trái nghĩa và từ đồng nghĩa của từ. Nó sử dụng Mạng từ để nhận nghĩa, Google để tìm bản dịch và từ khóa đồng nghĩa.com để nhận các từ đồng nghĩa và trái nghĩa. PyDictionary sử dụng mô-đun BeautifulSoup, Request làm phần phụ thuộc.

Để tạo ứng dụng, trước tiên, chúng tôi sẽ cài đặt các mô-đun này trong môi trường của chúng tôi bằng cách sử dụng pip install PyDictionary

Sau khi cài đặt, chúng tôi sẽ tạo một khung tkinter và một số phần tử khác.

Ví dụ

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

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

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

win.title("Python Dictionary")

#Define Helper Function to use the other atributes 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=("Aerial", 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()

Đầu ra

Chạy đoạn mã trên sẽ tạo và hiển thị Ứng dụng Từ điển. Tuy nhiên, sử dụng PyDictionary, chúng ta có thể thêm các thuộc tính khác như tìm từ đồng nghĩa, trái nghĩa, v.v.

Từ điển Word sử dụng Python Tkinter

Bây giờ, hãy nhập “Xin chào” vào hộp văn bản và nhấp vào nút “Tìm”. Nó sẽ kéo nghĩa của từ “Xin chào” khỏi từ điển.

Từ điển Word sử dụng Python Tkinter