2025年3月10日 星期一

字首字根字尾組字練習



 import tkinter as tk

from tkinter import ttk

import pyttsx3

import requests

from bs4 import BeautifulSoup


def build_word():

    prefix = prefix_var.get()

    root = root_var.get()

    suffix = suffix_var.get()

    

    if prefix in prefixes and root in roots and suffix in suffixes:

        word = prefix + root + suffix

        meaning = f"{prefixes[prefix]} + {roots[root]} + {suffixes[suffix]}"

        chinese_meaning = f"{prefix_ch[prefix]} + {root_ch[root]} + {suffix_ch[suffix]}"

        dictionary_meaning = fetch_yahoo_definition(word)

        

        result_label.config(text=f"你的新單字: {word}\n英文解釋: {meaning}\n中文解釋: {chinese_meaning}\n字典解釋: {dictionary_meaning}")

        speak_word(word)

    else:

        result_label.config(text="無效的組合,請選擇有效的選項。")


def speak_word(word):

    engine = pyttsx3.init()

    engine.say(word)

    engine.runAndWait()


def fetch_yahoo_definition(word):

    try:

        url = f"https://tw.dictionary.search.yahoo.com/search?p={word}"

        headers = {"User-Agent": "Mozilla/5.0"}

        response = requests.get(url, headers=headers)

        soup = BeautifulSoup(response.text, "html.parser")

        

        definition = soup.find("span", class_="fz-14 lh-22").text if soup.find("span", class_="fz-14 lh-22") else "無字典解釋"

        return definition

    except:

        return "查詢失敗,請檢查網絡。"


prefixes = {

    "un": "not, opposite of",

    "re": "again, back",

    "pre": "before",

    "mis": "wrongly",

    "dis": "opposite of",

    "sub": "under, below",

    "inter": "between",

    "auto": "self",

    "trans": "across"

}


prefix_ch = {

    "un": "不,相反的",

    "re": "再,回",

    "pre": "前,預先",

    "mis": "錯誤地",

    "dis": "相反的",

    "sub": "下,次級",

    "inter": "之間",

    "auto": "自己",

    "trans": "跨,穿越"

}


roots = {

    "act": "do, perform",

    "form": "shape",

    "ject": "throw",

    "port": "carry",

    "rupt": "break",

    "scribe": "write",

    "struct": "build",

    "vis": "see",

    "dict": "say"

}


root_ch = {

    "act": "行動,執行",

    "form": "形狀,形成",

    "ject": "投擲,拋",

    "port": "運輸,攜帶",

    "rupt": "破壞,中斷",

    "scribe": "書寫,記錄",

    "struct": "建造,結構",

    "vis": "看,視覺",

    "dict": "說話,命令"

}


suffixes = {

    "er": "one who",

    "able": "capable of",

    "ion": "act, process",

    "ment": "state or quality",

    "ful": "full of",

    "less": "without",

    "ness": "state of being",

    "ity": "quality of",

    "ive": "tending to"

}


suffix_ch = {

    "er": "從事...的人",

    "able": "能夠...的",

    "ion": "行為,過程",

    "ment": "狀態,品質",

    "ful": "充滿...的",

    "less": "沒有...的",

    "ness": "狀態,性質",

    "ity": "品質,特性",

    "ive": "具有...性質的"

}


root = tk.Tk()

root.geometry("600x300")

root.title("字首字根字尾組字練習")


tk.Label(root, text="字首").grid(row=0, column=0, padx=20)

tk.Label(root, text="字根").grid(row=0, column=1, padx=20)

tk.Label(root, text="字尾").grid(row=0, column=2, padx=20)


prefix_var = tk.StringVar()

root_var = tk.StringVar()

suffix_var = tk.StringVar()


prefix_menu = ttk.Combobox(root, textvariable=prefix_var, values=list(prefixes.keys()))

prefix_menu.grid(row=1, column=0, padx=20)


root_menu = ttk.Combobox(root, textvariable=root_var, values=list(roots.keys()))

root_menu.grid(row=1, column=1, padx=20)


suffix_menu = ttk.Combobox(root, textvariable=suffix_var, values=list(suffixes.keys()))

suffix_menu.grid(row=1, column=2, padx=20)


tk.Button(root, text="組字", command=build_word).grid(row=4, column=0, columnspan=3, pady=10)


result_label = tk.Label(root, text="", wraplength=500, justify="left")

result_label.grid(row=5, column=0, columnspan=3, pady=10)


root.mainloop()

沒有留言:

張貼留言