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

50 lines
1.7 KiB
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 -*-
"""
View 层。
View 负责界面控件的创建、显示和刷新。这里使用 PyQt5 的 QMainWindow
作为主窗口,并把 QTextBrowser 包装成一个 LogView方便接入 MVVM 绑定。
"""
from PyQt5.QtWidgets import *
from mvvm.framework import *
from design import main_win
class LogView(BaseView):
"""日志视图:把收到的日志文本追加显示到 QTextBrowser 中。"""
def __init__(self, topic: str, browser: QTextBrowser):
# topic 表示这个 View 自己发布变化时使用的主题。
super().__init__(topic)
# 保存真正负责显示日志的 PyQt 控件。
self.browser = browser
def signal_proxy(self, log: str):
"""
接收 Model 发来的日志文本,并刷新界面。
在本 demo 中StringModel.value 被修改后会发布 log 文本,
框架事件通道最终会调用这个函数。
"""
super().signal_proxy()
# 将新日志追加到文本浏览器。
self.browser.append(log)
# 文本框显示到底部,让用户总能看到最新日志。
self.browser.moveCursor(self.browser.textCursor().End)
class MainWinView(QMainWindow, main_win.Ui_MainWindow):
"""主窗口 View继承 Qt 主窗口和 Qt Designer 生成的界面类。"""
def __init__(self, parent=None):
super(MainWinView, self).__init__(parent)
# setupUi 会创建 LogBrowser、YesBtn、NoBtn 等控件。
self.setupUi(self)
# 设置窗口标题。
self.setWindowTitle("log演示demo")
# 包装日志控件,让它成为可绑定的 View 对象。
self.view_log = LogView('view_to_log', self.LogBrowser)