基本完成组件现代化

This commit is contained in:
2025-11-09 12:26:00 +08:00
parent 8a39ddab09
commit 07dc70df93
4 changed files with 62 additions and 68 deletions

View File

@@ -1,26 +1,5 @@
from textual.app import App, ComposeResult
from textual.events import Event
from textual.widgets import (
Collapsible,
Header,
Footer,
Markdown,
ListView,
ListItem,
Label,
Static,
Button,
)
from textual.containers import Container, Horizontal, Center
from textual.screen import Screen
from textual.widget import Widget
import uuid
from typing import Tuple, Dict
import heurams.kernel.particles as pt
import heurams.kernel.puzzles as pz
import re
import random
import copy
class BasePuzzleWidget(Widget):
def __init__(self, *children: Widget, atom: pt.Atom, name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:

View File

@@ -1,31 +1,41 @@
from textual.widgets import (
Markdown,
Label,
Static,
Button,
)
from textual.containers import Container, Horizontal, Center
from textual.screen import Screen
from textual.containers import Container, Horizontal
from textual.widget import Widget
import heurams.kernel.particles as pt
import heurams.kernel.puzzles as pz
from .base_puzzle_widget import BasePuzzleWidget
from textual.message import Message
class BasicEvaluation(BasePuzzleWidget):
def __init__(self, *children: Widget, atom: pt.Atom, name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
super().__init__(*children, atom = atom, name=name, id=id, classes=classes, disabled=disabled, markup=markup)
def __init__(self, *children: Widget, atom: pt.Atom, alia: str = "", name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
super().__init__(*children, atom=atom, name=name, id=id, classes=classes, disabled=disabled, markup=markup)
class RatingChanged(Message):
def __init__(self, rating: int) -> None:
self.rating = rating # 评分值 (0-5)
super().__init__()
# 反馈映射表
feedback_mapping = {
"feedback_5": 5,
"feedback_4": 4,
"feedback_3": 3,
"feedback_2": 2,
"feedback_1": 1,
"feedback_0": 0,
"feedback_5": {"rating": 5, "text": "完美回想"},
"feedback_4": {"rating": 4, "text": "犹豫后正确"},
"feedback_3": {"rating": 3, "text": "困难地正确"},
"feedback_2": {"rating": 2, "text": "错误但熟悉"},
"feedback_1": {"rating": 1, "text": "错误且不熟"},
"feedback_0": {"rating": 0, "text": "完全空白"},
}
def compose(self):
# 显示主要内容
yield Label(self.atom.registry["nucleon"]["content"], id="main")
# 显示评估说明(可选)
yield Static("请评估你对这个内容的记忆程度:", classes="instruction")
# 按钮容器
with Container(id="button_container"):
btn = {}
btn["5"] = Button(
@@ -46,6 +56,41 @@ class BasicEvaluation(BasePuzzleWidget):
btn["0"] = Button(
"完全空白", variant="error", id="feedback_0", classes="choice"
)
# 布局按钮
yield Horizontal(btn["5"], btn["4"])
yield Horizontal(btn["3"], btn["2"])
yield Horizontal(btn["1"], btn["0"])
def on_button_pressed(self, event: Button.Pressed) -> None:
"""处理按钮点击事件"""
button_id = event.button.id
if button_id in self.feedback_mapping:
feedback_info = self.feedback_mapping[button_id]
self.post_message(self.RatingChanged(
rating=feedback_info["rating"],
))
event.button.add_class("selected")
self.disable_other_buttons(button_id)
def disable_other_buttons(self, selected_button_id: str) -> None:
for button in self.query("Button.choice"):
if button.id != selected_button_id:
button.disabled = True
def enable_all_buttons(self) -> None:
for button in self.query("Button.choice"):
button.disabled = False
def on_key(self, event) -> None:
if event.key in ["0", "1", "2", "3", "4", "5"]:
button_id = f"feedback_{event.key}"
if button_id in self.feedback_mapping:
# 模拟按钮点击
self.post_message(self.RatingChanged(
rating=self.feedback_mapping[button_id]["rating"],
))

View File

@@ -1,29 +1,11 @@
from textual.app import App, ComposeResult
from textual.events import Event
from textual.widgets import (
Collapsible,
Header,
Footer,
Markdown,
ListView,
ListItem,
Label,
Static,
Button,
)
from textual.containers import Container, Horizontal, Center
from textual.screen import Screen
from textual.widget import Widget
import uuid
from typing import Tuple, Dict
import heurams.kernel.particles as pt
import heurams.kernel.puzzles as pz
import re
import random
import copy
class Finished(Widget):
def __init__(self, *children: Widget, name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
def __init__(self, *children: Widget, alia = "", name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
self.alia = alia
super().__init__(*children, name=name, id=id, classes=classes, disabled=disabled, markup=markup)
def compose(self):

View File

@@ -1,26 +1,14 @@
from textual.app import App, ComposeResult
from textual.events import Event
from textual.widgets import (
Collapsible,
Header,
Footer,
Markdown,
ListView,
ListItem,
Label,
Static,
Button,
)
from textual.widget import Widget
class Placeholder(Widget):
def __init__(self, *children: Widget, name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
def __init__(self, *children: Widget, name: str | None = None, alia: str = "", id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
super().__init__(*children, name=name, id=id, classes=classes, disabled=disabled, markup=markup)
def compose(self):
yield Label("示例标签", id="testlabel")
yield Button("示例按钮", id="testbtn", classes="choice")
def handler(self, event, type_):
self.screen.query_one("#testlabel", Label).update("hi")