Files
mvvm--learn/README.md
2026-05-18 11:33:59 +08:00

56 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Python-MVVM
这是一个用 Python 实现的极简 MVVM 示例项目。它通过“发布-订阅”模式实现
Model 与 View 的数据绑定,并用 PyQt5 做了一个日志窗口 demo。
## 项目结构
```text
Python-MVVM-master/
├── framework.py # 可单独复制使用的 MVVM 小框架
├── demo/
│ ├── start_up.py # demo 程序入口
│ ├── design/
│ │ ├── main_win.ui # Qt Designer 原始界面文件
│ │ └── main_win.py # 由 .ui 生成的 Python 界面代码
│ └── mvvm/
│ ├── framework.py # demo 内使用的框架代码
│ ├── model.py # Model 层
│ ├── view.py # View 层
│ ├── view_model.py # ViewModel 层
│ └── binder.py # 绑定 View 与 ViewModel
└── docs/
└── MVVM学习教程.md # 详细学习文档/课件
```
## 运行 demo
进入 `demo` 目录运行:
```bash
python start_up.py
```
如果提示缺少 PyQt5需要先安装
```bash
pip install PyQt5
```
## 核心思想
MVVM 的目标是让 View 不直接处理业务逻辑,让 ViewModel 不直接操作界面控件。
本项目中,一次按钮点击的大致流程是:
```text
用户点击按钮
-> PyQt clicked 信号
-> ViewModel.event_log_yes/event_log_no
-> 修改 StringModel.value
-> Model 发布 log_to_view 事件
-> EventChannel 分发事件
-> LogView.signal_proxy 刷新 QTextBrowser
```
详细讲解见 [docs/MVVM学习教程.md](docs/MVVM学习教程.md)。