Archived
0
0

fix: 改进

This commit is contained in:
2025-12-16 00:26:58 +08:00
parent ec5c8eb191
commit 5e4b0508eb
6 changed files with 70 additions and 62 deletions

View File

@@ -3,15 +3,16 @@ from textual.widgets import (
Label,
Button,
)
from textual.containers import ScrollableContainer
from textual.containers import ScrollableContainer, Container
from textual.widget import Widget
import heurams.kernel.particles as pt
import heurams.kernel.puzzles as pz
from .base_puzzle_widget import BasePuzzleWidget
from typing import TypedDict
from bidict import bidict
from heurams.services.hasher import hash
from heurams.services.logger import get_logger
logger = get_logger(__name__)
class Setting(TypedDict):
__origin__: str
@@ -46,7 +47,8 @@ class MCQPuzzle(BasePuzzleWidget):
)
self.inputlist = []
self.alia = alia
self.hashmap = bidict()
self.hashmap = dict()
self.cursor = 0
self._load()
def _load(self):
@@ -57,18 +59,23 @@ class MCQPuzzle(BasePuzzleWidget):
self.puzzle.refresh()
def compose(self):
self.atom.registry["nucleon"].do_eval()
setting: Setting = self.atom.registry["nucleon"].metadata["orbital"]["puzzles"][
self.alia
]
logger.debug(f"Puzzle Setting: {setting}")
yield Label(setting["primary"], id="sentence")
yield Label(self.puzzle.wording[len(self.inputlist)], id="puzzle")
yield Label(f"当前输入: {self.inputlist}", id="inputpreview")
# 渲染当前问题的选项
current_options = self.puzzle.options[len(self.inputlist)]
for i in current_options:
self.hashmap[str(hash(i))] = i
yield Button(i, id=f"select-{hash(i)}")
with Container(id="btn-container"):
for i in current_options:
self.hashmap[str(hash(i))] = i
btnid = f"sel{str(self.cursor).zfill(3)}-{hash(i)}"
logger.debug(f"建立按钮 {btnid}")
yield Button(i, id=f"{btnid}")
yield Button("退格", id="delete")
@@ -76,7 +83,7 @@ class MCQPuzzle(BasePuzzleWidget):
# 更新预览标签
preview = self.query_one("#inputpreview")
preview.update(f"当前输入: {self.inputlist}") # type: ignore
logger.debug("已经更新预览标签")
# 更新问题标签
puzzle_label = self.query_one("#puzzle")
current_question_index = len(self.inputlist)
@@ -95,11 +102,11 @@ class MCQPuzzle(BasePuzzleWidget):
self.refresh_buttons()
self.update_display()
elif button_id.startswith("select"): # type: ignore
elif button_id.startswith("sel"): # type: ignore
# 选项选择处理
answer_text = self.hashmap[button_id[7:]] # type: ignore
self.inputlist.append(answer_text)
logger.debug(f"{self.inputlist}")
# 检查是否完成所有题目
if len(self.inputlist) >= len(self.puzzle.answer):
is_correct = self.inputlist == self.puzzle.answer
@@ -120,22 +127,26 @@ class MCQPuzzle(BasePuzzleWidget):
def refresh_buttons(self):
"""刷新按钮显示(用于题目切换)"""
# 移除所有选项按钮
logger.debug("刷新按钮")
self.cursor += 1
container = self.query_one("#btn-container")
buttons_to_remove = [
child
for child in self.children
if hasattr(child, "id") and child.id and child.id.startswith("select")
for child in container.children
if hasattr(child, "id") and child.id and child.id.startswith("sel")
]
for button in buttons_to_remove:
self.remove_child(button) # type: ignore
logger.info(button)
container.remove_children("#"+button.id) # type: ignore
# 添加当前题目的选项按钮
current_question_index = len(self.inputlist)
if current_question_index < len(self.puzzle.options):
current_options = self.puzzle.options[current_question_index]
for option in current_options:
button_id = f"select-{hash(option)}"
button_id = f"sel{str(self.cursor).zfill(3)}-{hash(option)}"
if button_id not in self.hashmap:
self.hashmap[button_id] = option
new_button = Button(option, id=button_id)
self.mount(new_button)
container.mount(new_button)