|  | 
| #修改了提示时出错的问题,修改后将以成语最后一个汉字查找成语 
 复制代码import tkinter as tk
from tkinter import messagebox, scrolledtext, simpledialog
import random
import requests
import json
import time
from pypinyin import lazy_pinyin, Style
class IdiomSolitaireGame:
    def __init__(self, root):
        self.root = root
        self.root.title("成语接龙游戏")
        self.root.geometry("800x600")
        
        # 游戏状态变量
        self.current_idiom = ""
        self.history = []
        self.game_active = True
        self.last_api_call_time = 0
        self.api_call_interval = 1  # 1秒间隔防止API调用过于频繁
        self.hint_count = 5  # 提示次数限制
        self.max_hint_count = 5  # 默认最大提示次数
        
        # 成语库(用于初始成语和本地验证)
        self.idiom_list = [
            "一帆风顺", "顺水推舟", "舟车劳顿", "顿开茅塞", "塞翁失马",
            "马到成功", "功成名就", "就地取材", "材优干济", "济世之才",
            "才高八斗", "斗转星移", "移花接木", "木已成舟", "舟中敌国",
            "国泰民安", "安步当车", "车水马龙", "龙飞凤舞", "舞文弄墨",
            "墨守成规", "规行矩步", "步履维艰", "艰苦卓绝", "绝无仅有",
            "有志竟成", "成竹在胸", "胸有成竹", "竹报平安", "安土重迁",
            "迁客骚人", "人山人海", "海阔天空", "空穴来风", "风和日丽"
        ]
        
        # 已验证的成语缓存
        self.verified_idioms = set(self.idiom_list)  # 预先将本地成语库中的成语标记为已验证
        self.invalid_idioms = set()  # 无效成语缓存
        self.idiom_explanations = {}  # 成语解释缓存
        
        # 创建界面
        self.create_widgets()
        
        # 开始游戏(不再询问提示次数)
        self.start_game()
    def get_char_pinyin(self, char):
        """获取汉字的拼音(忽略声调)"""
        pinyin_list = lazy_pinyin(char, style=Style.NORMAL)
        return pinyin_list[0] if pinyin_list else char
    def get_char_pinyin_without_tone(self, char):
        """获取汉字的拼音并去除翘舌音和前后鼻音的区别"""
        pinyin = self.get_char_pinyin(char)
        # 简化处理,忽略翘舌音和前后鼻音的区别
        pinyin = pinyin.replace('zh', 'z').replace('ch', 'c').replace('sh', 's')
        pinyin = pinyin.replace('ing', 'in').replace('eng', 'en').replace('ang', 'an').replace('ong', 'on')
        return pinyin
    def create_widgets(self):
        # 标题
        title_label = tk.Label(self.root, text="成语接龙游戏", font=("仿宋", 16, "bold"))
        title_label.pack(pady=10)
        
        # 当前成语显示
        self.current_idiom_frame = tk.Frame(self.root)
        self.current_idiom_frame.pack(pady=10)
        
        tk.Label(self.current_idiom_frame, text="当前成语:", font=("仿宋", 12)).pack()
        self.current_idiom_label = tk.Label(
            self.current_idiom_frame, 
            text="", 
            font=("仿宋", 14, "bold"), 
            fg="blue"
        )
        self.current_idiom_label.pack()
        
        # 输入区域
        input_frame = tk.Frame(self.root)
        input_frame.pack(pady=10)
        
        tk.Label(input_frame, text="请输入接龙成语:", font=("仿宋", 12)).pack()
        self.entry = tk.Entry(input_frame, font=("仿宋", 12), width=20)
        self.entry.pack(pady=5)
        
        # 按钮区域
        button_frame = tk.Frame(input_frame)
        button_frame.pack(pady=5)
        
        # 提交按钮
        self.submit_button = tk.Button(
            button_frame, 
            text="提交", 
            command=self.submit_idiom,
            font=("仿宋", 12),
            bg="lightblue"
        )
        self.submit_button.pack(side=tk.LEFT, padx=5)
        
        # 提示按钮
        self.hint_button = tk.Button(
            button_frame,
            text="提示",
            command=self.show_hint,
            font=("仿宋", 12),
            bg="lightyellow"
        )
        self.hint_button.pack(side=tk.LEFT, padx=5)
        
        # 重新开始按钮
        self.restart_button = tk.Button(
            button_frame,
            text="重新开始",
            command=self.restart_game,
            font=("仿宋", 12),
            bg="lightgreen"
        )
        self.restart_button.pack(side=tk.LEFT, padx=5)
        
        # 结束游戏按钮
        self.quit_button = tk.Button(
            button_frame,
            text="结束游戏",
            command=self.quit_game,
            font=("仿宋", 12),
            bg="lightcoral"
        )
        self.quit_button.pack(side=tk.LEFT, padx=5)
        
        # 设置提示次数按钮
        self.set_hint_button = tk.Button(
            button_frame,
            text="设置提示次数",
            command=self.set_hint_count,
            font=("仿宋", 12),
            bg="lightgray"
        )
        self.set_hint_button.pack(side=tk.LEFT, padx=5)
        
        # 提示次数显示
        self.hint_count_label = tk.Label(
            input_frame,
            text=f"剩余提示次数: {self.hint_count}",
            font=("仿宋", 10),
            fg="gray"
        )
        self.hint_count_label.pack()
        
        # 提示结果显示区域
        self.hint_frame = tk.Frame(self.root)
        self.hint_frame.pack(pady=5)
        
        tk.Label(self.hint_frame, text="提示:", font=("仿宋", 12)).pack()
        self.hint_text = tk.Label(
            self.hint_frame,
            text="",
            font=("仿宋", 12),
            fg="green",
            wraplength=500
        )
        self.hint_text.pack()
        
        # 历史记录显示 - 修改为左右布局
        history_frame = tk.Frame(self.root)
        history_frame.pack(pady=10, fill=tk.BOTH, expand=True)
        
        tk.Label(history_frame, text="接龙历史与成语解释:", font=("仿宋", 12)).pack()
        
        # 创建左右分栏
        history_paned_window = tk.PanedWindow(history_frame, orient=tk.HORIZONTAL)
        history_paned_window.pack(fill=tk.BOTH, expand=True, padx=10, pady=5)
        
        # 左边 - 接龙历史
        history_list_frame = tk.Frame(history_paned_window)
        history_paned_window.add(history_list_frame)
        
        tk.Label(history_list_frame, text="接龙历史", font=("仿宋", 10, "bold")).pack()
        
        self.history_text = scrolledtext.ScrolledText(
            history_list_frame, 
            width=25, 
            height=10, 
            font=("仿宋", 14)  # 改为14号字体
        )
        self.history_text.pack(fill=tk.BOTH, expand=True)
        self.history_text.config(state=tk.DISABLED)
        
        # 右边 - 成语解释
        explanation_frame = tk.Frame(history_paned_window)
        history_paned_window.add(explanation_frame)
        
        tk.Label(explanation_frame, text="成语解释", font=("仿宋", 10, "bold")).pack()
        
        self.explanation_text = scrolledtext.ScrolledText(
            explanation_frame, 
            width=35, 
            height=10, 
            font=("仿宋", 14)  # 改为14号字体
        )
        self.explanation_text.pack(fill=tk.BOTH, expand=True)
        self.explanation_text.config(state=tk.DISABLED)
        
        # 状态栏
        self.status_var = tk.StringVar()
        self.status_var.set("游戏准备中...")
        self.status_bar = tk.Label(
            self.root, 
            textvariable=self.status_var, 
            bd=1, 
            relief=tk.SUNKEN, 
            anchor=tk.W,
            font=("仿宋", 20, "bold")
        )
        self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)
    
    def set_hint_count(self):
        """让用户在游戏过程中设置提示次数"""
        if not self.game_active:
            messagebox.showwarning("警告", "游戏已结束,请重新开始后再设置提示次数")
            return
            
        try:
            hint_input = tk.simpledialog.askinteger(
                "设置提示次数", 
                f"请输入提示次数 (当前剩余{self.hint_count}次):", 
                initialvalue=self.max_hint_count
            )
            if hint_input is not None and hint_input > 0:
                # 更新最大提示次数
                self.max_hint_count = hint_input
                
                # 更新当前剩余提示次数为新的最大值(如果当前剩余次数不为0)
                # 或者如果游戏刚开始还没用过提示,则设置为最大值
                if self.hint_count > 0 or len(self.history) == 1:
                    self.hint_count = self.max_hint_count
                
                # 更新提示次数显示
                self.hint_count_label.config(text=f"剩余提示次数: {self.hint_count}")
                self.update_hint_button_state()
                messagebox.showinfo("提示", f"提示次数已设置为 {self.max_hint_count} 次")
            elif hint_input is not None and hint_input <= 0:
                messagebox.showwarning("警告", "提示次数必须大于0")
        except Exception as e:
            messagebox.showerror("错误", f"设置提示次数时出现错误: {str(e)}")
    def start_game(self):
        """开始游戏,随机选择一个初始成语"""
        self.current_idiom = random.choice(self.idiom_list)
        self.history = [self.current_idiom]
        self.game_active = True
        self.hint_count = self.max_hint_count  # 使用默认提示次数
        self.hint_text.config(text="")  # 清空提示文本
        self.update_hint_button_state()  # 更新提示按钮状态
        self.update_display()
        last_char = self.current_idiom[-1]
        last_char_pinyin = self.get_char_pinyin_without_tone(last_char)
        self.status_var.set(f"游戏开始!请接龙成语,以'{last_char}({last_char_pinyin})'拼音开头")
    def submit_idiom(self):
        """处理用户提交的成语"""
        if not self.game_active:
            self.status_var.set("游戏已结束,请重新开始")
            return
            
        user_idiom = self.entry.get().strip()
        # 不清空输入框,让用户可以修改输入内容
        
        if not user_idiom:
            self.status_var.set("请输入一个成语")
            return
            
        if len(user_idiom) != 4:
            self.status_var.set("成语必须是四个字")
            return
            
        # 验证成语接龙规则
        if not self.validate_idiom_chain(user_idiom):
            last_char = self.current_idiom[-1]
            last_char_pinyin = self.get_char_pinyin_without_tone(last_char)
            self.status_var.set(f"'{user_idiom}'不符合接龙规则!请接一个以'{last_char}({last_char_pinyin})'拼音开头的成语。")
            # 不结束游戏,让用户重新输入
            return
            
        # 检查是否为有效成语
        validation_result = self.is_valid_idiom(user_idiom)
        if validation_result == "network_error":
            # 网络错误,询问用户是否继续
            result = messagebox.askyesno("网络错误", f"无法连接到成语验证服务,是否跳过验证并继续游戏?\n\n成语: {user_idiom}")
            if not result:
                self.status_var.set("用户选择不继续游戏")
                return
        elif validation_result == "validation_failed":
            # API验证失败,明确告知用户
            result = messagebox.askyesno("验证失败", f"无法通过在线服务验证成语有效性,是否跳过验证并继续游戏?\n\n成语: {user_idiom}")
            if not result:
                self.status_var.set("用户选择不继续游戏")
                return
        elif not validation_result:
            self.status_var.set(f"'{user_idiom}'不是有效成语!请重新输入。")
            # 不结束游戏,让用户重新输入
            return
            
        # 检查是否已经使用过
        if user_idiom in self.history:
            self.status_var.set(f"成语'{user_idiom}'已经使用过了!请重新输入。")
            # 不结束游戏,让用户重新输入
            return
            
        # 清空输入框
        self.entry.delete(0, tk.END)
        
        # 获取成语解释
        explanation_data = self.get_idiom_explanation(user_idiom)
        
        # 更新游戏状态
        self.current_idiom = user_idiom
        self.history.append(user_idiom)
        # 保存成语解释到缓存
        if explanation_data and isinstance(explanation_data, dict):
            self.idiom_explanations[user_idiom] = explanation_data
        self.hint_text.config(text="")  # 清空提示文本
        self.update_display()
        last_char = self.current_idiom[-1]
        last_char_pinyin = self.get_char_pinyin_without_tone(last_char)
        self.status_var.set(f"很好!请接龙成语,以'{last_char}({last_char_pinyin})'拼音开头")
    
    def validate_idiom_chain(self, user_idiom):
        """验证成语接龙规则(基于拼音)"""
        # 防御性检查:确保当前成语和用户输入的成语都至少有一个字符
        if not self.current_idiom or not user_idiom:
            return False
            
        # 获取当前成语的最后一个字
        last_char = self.current_idiom[-1]
        # 获取用户输入成语的第一个字
        first_char = user_idiom[0]
        
        # 获取这两个字的拼音(忽略声调和部分发音区别)
        last_char_pinyin = self.get_char_pinyin_without_tone(last_char)
        first_char_pinyin = self.get_char_pinyin_without_tone(first_char)
        
        # 检查是否符合接龙规则(首字与前一个成语末字拼音相同)
        return last_char_pinyin == first_char_pinyin
    
    def show_hint(self):
        """显示提示"""
        if not self.game_active:
            return
            
        if self.hint_count <= 0:
            self.status_var.set("提示次数已用完")
            return
            
        # 减少提示次数
        self.hint_count -= 1
        
        # 获取当前成语的最后一个字及其拼音
        last_char = self.current_idiom[-1]
        last_char_pinyin = self.get_char_pinyin_without_tone(last_char)
        
        # 尝试从成语库中找到一个匹配的成语作为提示
        matching_idioms = []
        for idiom in self.idiom_list:
            if idiom not in self.history:
                first_char = idiom[0]
                first_char_pinyin = self.get_char_pinyin_without_tone(first_char)
                if last_char_pinyin == first_char_pinyin:
                    matching_idioms.append(idiom)
        
        if matching_idioms:
            hint_idiom = random.choice(matching_idioms)
            self.hint_text.config(text=f"参考成语: {hint_idiom}")
            # 将提示的成语直接显示在输入框中
            self.entry.delete(0, tk.END)
            self.entry.insert(0, hint_idiom)
        else:
            # 如果在本地成语库中找不到,尝试通过API获取提示
            hint_idiom = self.get_hint_from_api(last_char)
            if hint_idiom and hint_idiom != "network_error" and hint_idiom != "validation_failed":
                # 验证API返回的成语是否符合拼音规则
                first_char = hint_idiom[0]
                first_char_pinyin = self.get_char_pinyin_without_tone(first_char)
                if last_char_pinyin == first_char_pinyin:
                    self.hint_text.config(text=f"参考成语: {hint_idiom}")
                    # 将提示的成语直接显示在输入框中
                    self.entry.delete(0, tk.END)
                    self.entry.insert(0, hint_idiom)
                else:
                    self.hint_text.config(text="抱歉,暂时无法提供提示")
            elif hint_idiom == "network_error":
                self.hint_text.config(text="网络错误,无法获取提示")
            elif hint_idiom == "validation_failed":
                self.hint_text.config(text="API服务异常,无法获取提示")
            else:
                self.hint_text.config(text="抱歉,暂时无法提供提示")
        
        # 更新提示次数显示和按钮状态
        self.hint_count_label.config(text=f"剩余提示次数: {self.hint_count}")
        self.update_hint_button_state()
    
    def get_hint_from_api(self, start_char):
        """通过API获取以指定字符开头的成语作为提示,并保存成语解释信息"""
        # 直接使用汉字字符进行查询
        try:
            # 使用提供的API地址和认证信息
            url = f"http://101.35.2.25/api/zici/cyjl.php?id=10007393&key=12d0b1f4c9bed27db2f69ea0de0dd6c8&word={start_char}"
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
            }
            
            # 添加调试信息
            print(f"提示API调试 - 请求URL: {url}")
            print(f"提示API调试 - 请求头: {headers}")
            
            response = requests.get(url, headers=headers, timeout=5)
            
            # 添加响应调试信息
            print(f"提示API调试 - 响应状态码: {response.status_code}")
            print(f"提示API调试 - 响应头: {response.headers}")
            print(f"提示API调试 - 响应内容: {response.text}")
            
            if response.status_code == 200:
                try:
                    data = response.json()
                    print(f"提示API调试 - 解析后的JSON: {data}")
                    # 检查API是否返回了成功的结果
                    if data.get("code") == 200:
                        # 根据实际API返回结构调整处理逻辑
                        if "words" in data:
                            # API返回的是单个成语字符串
                            idiom = data["words"]
                            # 检查这个成语是否已经使用过
                            if idiom not in self.history:
                                # 保存成语解释信息到缓存
                                self.idiom_explanations[idiom] = data
                                print(f"提示API调试 - 选中的成语: {idiom},并已保存解释信息")
                                return idiom
                            else:
                                print("提示API调试 - 返回的成语已使用过")
                                return None
                        elif "data" in data and isinstance(data["data"], list):
                            # 兼容处理:如果data是列表形式
                            idioms = data["data"]
                            print(f"提示API调试 - 获取到的成语列表: {idioms}")
                            # 过滤掉已经使用过的成语
                            available_idioms = [idiom for idiom in idioms if idiom not in self.history]
                            print(f"提示API调试 - 过滤后的可用成语列表: {available_idioms}")
                            if available_idioms:
                                # 随机选择一个未使用过的成语
                                selected_idiom = random.choice(available_idioms)
                                # 尝试获取该成语的详细信息并保存
                                explanation_data = self.get_idiom_explanation(selected_idiom)
                                if explanation_data and isinstance(explanation_data, dict):
                                    self.idiom_explanations[selected_idiom] = explanation_data
                                print(f"提示API调试 - 选中的成语: {selected_idiom},并已保存解释信息")
                                return selected_idiom
                            else:
                                print("提示API调试 - 没有可用的成语")
                                return None
                        else:
                            # 如果没有预期的字段,尝试直接获取返回的字符串(如果整个响应就是成语)
                            if isinstance(data, str) and len(data) == 4:
                                if data not in self.history:
                                    # 为直接返回的字符串创建基本的解释结构
                                    basic_explanation = {"words": data, "jieshi": "通过提示API获取的成语"}
                                    self.idiom_explanations[data] = basic_explanation
                                    print(f"提示API调试 - 直接返回字符串成语: {data},并已保存基础解释信息")
                                    return data
                                else:
                                    print("提示API调试 - 直接返回的成语已使用过")
                                    return None
                            print("提示API调试 - 响应数据格式不符合预期")
                            return "validation_failed"
                    else:
                        # API调用成功但返回验证失败
                        print(f"提示API调试 - API返回错误,code: {data.get('code')}, msg: {data.get('msg', '无错误信息')}")
                        return "validation_failed"
                except json.JSONDecodeError as e:
                    # JSON解析失败
                    print(f"提示API调试 - JSON解析失败: {str(e)}")
                    return "validation_failed"
            else:
                # HTTP状态码不是200,API调用失败
                print(f"提示API调试 - HTTP状态码错误: {response.status_code}")
                return "validation_failed"
                
        except requests.exceptions.Timeout as e:
            print(f"提示API调试 - 请求超时: {str(e)}")
            return "network_error"
        except requests.exceptions.ConnectionError as e:
            print(f"提示API调试 - 连接错误: {str(e)}")
            return "network_error"
        except requests.exceptions.RequestException as e:
            print(f"提示API调试 - 请求异常: {str(e)}")
            return "network_error"
        except Exception as e:
            print(f"提示API调试 - 其他异常: {str(e)}")
            return "validation_failed"
    
    def restart_game(self):
        """重新开始游戏"""
        # 启用按钮
        self.submit_button.config(state=tk.NORMAL)
        self.hint_button.config(state=tk.NORMAL)
        self.update_hint_button_state()  # 更新提示按钮状态
        
        # 重新开始游戏
        self.start_game()
    def update_hint_button_state(self):
        """更新提示按钮状态"""
        if self.hint_count <= 0:
            self.hint_button.config(state=tk.DISABLED, bg="lightgray")
        else:
            self.hint_button.config(state=tk.NORMAL, bg="lightyellow")
    
    def is_valid_idiom(self, idiom):
        """验证是否为有效成语,并保存其详细解释信息"""
        # 首先检查本地成语库
        if idiom in self.verified_idioms:
            return True
            
        if idiom in self.invalid_idioms:
            return False
            
        # 控制API调用频率
        current_time = time.time()
        if current_time - self.last_api_call_time < self.api_call_interval:
            time_to_wait = self.api_call_interval - (current_time - self.last_api_call_time)
            time.sleep(time_to_wait)
        
        api_validated = False
        try:
            # 1. 首先尝试使用已验证有效的API接口(使用words参数)
            url = f"https://cn.apihz.cn/api/zici/chacy.php?id=10007393&key=12d0b1f4c9bed27db2f69ea0de0dd6c8&words={idiom}"
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
            }
            
            # 添加调试信息
            print(f"调试信息 - 请求URL: {url}")
            print(f"调试信息 - 请求头: {headers}")
            
            response = requests.get(url, headers=headers, timeout=5)
            self.last_api_call_time = time.time()
            
            # 添加响应调试信息
            print(f"调试信息 - 响应状态码: {response.status_code}")
            print(f"调试信息 - 响应头: {response.headers}")
            print(f"调试信息 - 响应内容: {response.text}")
            
            if response.status_code == 200:
                try:
                    # 解析JSON响应
                    data = response.json()
                    print(f"调试信息 - 解析后的JSON: {data}")
                    
                    # 根据API返回的code判断结果
                    if data.get("code") == 200:
                        self.verified_idioms.add(idiom)  # 添加到有效缓存
                        api_validated = True
                        # 保存成语解释信息,避免后续重复查询
                        self.idiom_explanations[idiom] = data
                        return True
                    elif data.get("code") == 400:
                        # API明确表示查询失败或结果为空,这可能意味着输入不是有效成语
                        self.invalid_idioms.add(idiom)
                        self.status_var.set(f"成语验证失败: {data.get('msg', '查询失败')}")
                        # 这种情况属于内容无效,而非验证服务问题
                        return False
                    else:
                        # 其他API错误,视为验证服务问题
                        self.invalid_idioms.add(idiom)
                        error_msg = data.get('msg', '未知错误')
                        self.status_var.set(f"API验证服务异常: {error_msg}")
                        return "validation_failed"
                except json.JSONDecodeError as e:
                    # JSON解析失败,API调用有问题
                    self.status_var.set(f"API响应解析失败: {str(e)}")
                    print(f"调试信息 - JSON解析错误: {str(e)}")
                    return "validation_failed"
            else:
                # HTTP状态码不是200,API调用失败
                self.status_var.set(f"API调用失败: HTTP {response.status_code}")
                return "validation_failed"
                
        except requests.exceptions.Timeout:
            self.status_var.set("网络连接超时,无法验证成语")
            print("调试信息 - 网络连接超时")
            return "network_error"
        except requests.exceptions.ConnectionError:
            self.status_var.set("网络连接错误,无法验证成语")
            print("调试信息 - 网络连接错误")
            return "network_error"
        except requests.exceptions.RequestException as e:
            self.status_var.set(f"网络请求错误: {str(e)}")
            print(f"调试信息 - 网络请求错误: {str(e)}")
            return "network_error"
        except Exception as e:
            self.status_var.set(f"验证过程中发生错误: {str(e)}")
            print(f"调试信息 - 验证过程中发生错误: {str(e)}")
            # 出现其他错误时,如果成语在本地库中则认为有效
            return idiom in self.verified_idioms
    
    def update_display(self):
        """更新界面显示"""
        # 更新当前成语
        self.current_idiom_label.config(text=self.current_idiom)
        
        # 更新历史记录
        self.history_text.config(state=tk.NORMAL)
        self.history_text.delete(1.0, tk.END)
        for i, idiom in enumerate(self.history, 1):
            self.history_text.insert(tk.END, f"{i}. {idiom}\n")
        self.history_text.config(state=tk.DISABLED)
        
        # 显示最新成语的解释(如果有的话)
        if self.current_idiom in self.idiom_explanations:
            explanation = self.idiom_explanations[self.current_idiom]
            self.show_explanation(explanation)
            
    def get_idiom_explanation(self, idiom):
        """获取成语的详细解释"""
        try:
            # 使用成语查询API获取详细解释
            url = f"https://cn.apihz.cn/api/zici/chacy.php?id=10007393&key=12d0b1f4c9bed27db2f69ea0de0dd6c8&words={idiom}"
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
            }
            
            response = requests.get(url, headers=headers, timeout=5)
            
            if response.status_code == 200:
                data = response.json()
                if data.get("code") == 200:
                    # 返回完整的数据对象,包含成语的各种信息
                    return data
                else:
                    # API返回错误,但仍返回一个基本的解释结构
                    return {"words": idiom, "jieshi": "暂无详细解释"}
            else:
                # HTTP请求失败,返回基本结构
                return {"words": idiom, "jieshi": "网络错误,无法获取详细解释"}
                
        except Exception as e:
            # 出现异常时,返回基本结构
            return {"words": idiom, "jieshi": "获取解释时发生错误"}
    
    def show_explanation(self, explanation_data):
        """显示成语解释"""
        self.explanation_text.config(state=tk.NORMAL)
        self.explanation_text.delete(1.0, tk.END)
        
        if isinstance(explanation_data, dict):
            # 显示详细的成语解释
            if "words" in explanation_data:
                self.explanation_text.insert(tk.END, f"成语: {explanation_data['words']}\n\n")
            
            if "pingyin" in explanation_data:
                self.explanation_text.insert(tk.END, f"拼音: {explanation_data['pingyin']}\n\n")
                
            if "jieshi" in explanation_data:
                self.explanation_text.insert(tk.END, f"解释: {explanation_data['jieshi']}\n\n")
                
            if "chuchu" in explanation_data:
                self.explanation_text.insert(tk.END, f"出处: {explanation_data['chuchu']}\n\n")
                
            if "liju" in explanation_data:
                self.explanation_text.insert(tk.END, f"例句: {explanation_data['liju']}\n\n")
        else:
            # 显示简单的文本解释
            self.explanation_text.insert(tk.END, explanation_data)
            
        self.explanation_text.config(state=tk.DISABLED)
    
    def game_over(self, message):
        """游戏结束处理"""
        self.game_active = False
        self.status_var.set("游戏结束")
        messagebox.showinfo("游戏结束", message + "\n\n接龙历史:\n" + "\n".join(self.history))
        # 禁用提交按钮和提示按钮
        self.submit_button.config(state=tk.DISABLED)
        self.hint_button.config(state=tk.DISABLED)
    
    def quit_game(self):
        """结束游戏并关闭窗口"""
        if messagebox.askyesno("确认", "确定要结束游戏并退出吗?"):
            self.root.quit()
            self.root.destroy()
def main():
    root = tk.Tk()
    game = IdiomSolitaireGame(root)
    root.mainloop()
if __name__ == "__main__":
    main()
 | 
 |