Files
mvvm--learn/demo/mvvm/view_model.py
2026-05-18 11:33:59 +08:00

27 lines
864 B
Python
Raw Permalink 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.
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ViewModel 层。
ViewModel 连接界面操作与业务数据:按钮点击后不直接操作界面,
而是修改 ModelModel 再通过事件通道通知 View 刷新。
"""
from mvvm.model import *
class MainWinVM(object):
"""主窗口 ViewModel保存主窗口需要的数据模型和按钮事件逻辑。"""
def __init__(self):
# 创建日志内容 Model。
# 主题 "log_to_view" 表示:这个 Model 的变化要通知日志 View。
self.model_log = StringModel('log_to_view', '启动日志')
def event_log_yes(self):
"""Yes 按钮点击事件:写入一条 yes 日志。"""
self.model_log.value = 'log: yes!!!'
def event_log_no(self):
"""No 按钮点击事件:写入一条 no 日志。"""
self.model_log.value = 'log: no~~~'