2025年3月11日 星期二

組字程式 III


# -*- coding: utf-8 -*-

# -*- coding: utf-8 -*-
import tkinter as tk
from tkinter import ttk
import pyttsx3
import random

# 初始化語音引擎
engine = pyttsx3.init()

# 字首、字根、字尾選項
prefixes = sorted("ante,anti,auto,ambi,apo,be,bene,bi,circ,co,com,col,con,contra,counter,de,dia,dis,en,ex,extra,hetero,homo,hyper,il,im,in,inter,intra,intro,ir,mal,mega,micro,mis,mono,multi,neo,non,ob,para,pan,per,poly,post,pre,pro,re,se,semi,sub,super,supra,sur,sym,syn,tele,trans,tri,uni".split(','))

roots = sorted("ann,audi,bio,cap,ceive,cip,cept,ced,ceed,cess,cede,circ,cyc,dem,demo,dict,divid,duc,equ,flect,flex,form,gram,graph,gress,ject,manu,mob,mot,mov,nov,ped,pict,port,pos,press,rupt,scribe,serve,sist,spec,spir,tain,tract,vid,vise,viv,volv,astro,bell,cardi,chron,corp,cred,geo,hydr,luc,phon,therm".split(','))

suffixes = sorted("er,or,an,ian,ant,ent,ar,ate,ee,eer,ese,ess,ic,ics,ician,ist,ster,al,age,ance,ence,ancy,ency,dom,hood,ism,ity,ty,logy,ment,ness,ory,ship,tion,ation,sion,ure,ture,ate,en,fy,ify,ish,ize,ise,le,able,ible,ary,ful,ical,id,ior,ive,less,like,ly,ous,proof,ward,wards,way,ways,wise,acious,tude,phobia,escence,ory,cule,ling".split(','))

# 建立主視窗
root = tk.Tk()
root.title("字根組合器")
root.geometry("550x450")

# 設定標籤
tk.Label(root, text="選擇字首:").grid(row=0, column=0, padx=10, pady=10)
tk.Label(root, text="選擇字根:").grid(row=0, column=1, padx=10, pady=10)
tk.Label(root, text="選擇字尾:").grid(row=0, column=2, padx=10, pady=10)
tk.Label(root, text="組合的單字:").grid(row=4, column=0, padx=10, pady=10)

# 建立 Combobox 下拉選單
prefix_var = tk.StringVar()
prefix_combobox = ttk.Combobox(root, textvariable=prefix_var, values=prefixes, state="readonly")
prefix_combobox.grid(row=1, column=0, padx=10, pady=10)

root_var = tk.StringVar()
root_combobox = ttk.Combobox(root, textvariable=root_var, values=roots, state="readonly")
root_combobox.grid(row=1, column=1, padx=10, pady=10)

suffix_var = tk.StringVar()
suffix_combobox = ttk.Combobox(root, textvariable=suffix_var, values=suffixes, state="readonly")
suffix_combobox.grid(row=1, column=2, padx=10, pady=10)

# 顯示組合後的單字
word_label = tk.Label(root, text="", font=("Arial", 14, "bold"), fg="blue")
word_label.grid(row=4, column=1, padx=10, pady=10)

# 更新單字的函式
def update_word():
    combined_word = f"{prefix_var.get()}{root_var.get()}{suffix_var.get()}"
    word_label.config(text=combined_word)
    engine.say(combined_word)
    engine.runAndWait()

# 隨機生成 10 個單字並朗讀
def generate_random_words():
    words = [f"{random.choice(prefixes)}{random.choice(roots)}{random.choice(suffixes)}" for _ in range(10)]
    word_label.config(text="\n".join(words))
    for word in words:
        engine.say(word)
    engine.runAndWait()

# 設定按鈕,點擊後更新顯示
combine_button = tk.Button(root, text="組合單字", command=update_word)
combine_button.grid(row=3, column=1, padx=10, pady=10)

# 設定隨機練習按鈕
auto_practice_button = tk.Button(root, text="自動練習 10 字", command=generate_random_words)
auto_practice_button.grid(row=5, column=1, padx=10, pady=10)

# 啟動主循環
root.mainloop()

沒有留言:

張貼留言