使用Python Tkinter的單詞字典


在本文中,我們將使用PyDictionary和Tkinter模組建立一個基於GUI的字典。

PyDictionary是一個Python模組,有助於獲取詞義、同義詞和反義詞。它使用WordNet獲取釋義,使用谷歌翻譯,使用synonym.com獲取同義詞和反義詞。PyDictionary使用BeautifulSoup和Requests模組作為依賴項。

為了建立該應用程式,我們將首先使用pip install PyDictionary在我們的環境中安裝這些模組。

安裝後,我們將建立一個Tkinter框架以及其他一些元素。

示例

# 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()

輸出

執行以上程式碼將建立並顯示字典應用程式。但是,使用PyDictionary,我們可以新增其他屬性,如查詢同義詞、反義詞等。

現在,在文字框中輸入“Hello”,然後單擊“查詢”按鈕。它將從字典中提取“Hello”的含義。

更新於:2021年3月6日

3000+次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告