forked from HowardWhile/pyqt-hot-reload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (56 loc) · 2.14 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import sys
import importlib
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QVBoxLayout, QHBoxLayout, QGroupBox
from PySide6.QtCore import QEvent
from reload_handler import start_watcher
from widgets import my_widget
from console import DBG_PRINT
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt 熱重載 UI 測試")
self.setGeometry(100, 100, 600, 400)
# 主視窗 Layout
main_layout = QVBoxLayout()
# 上方的按鈕區
button_layout = QHBoxLayout()
btn1 = QPushButton("按鈕 1")
btn2 = QPushButton("按鈕 2")
btn3 = QPushButton("按鈕 3")
button_layout.addWidget(btn1)
button_layout.addWidget(btn2)
button_layout.addWidget(btn3)
main_layout.addLayout(button_layout)
# 下方的 GroupBox 容器
self.groupbox = QGroupBox("可熱重載 Widget")
self.groupbox_layout = QVBoxLayout()
self.groupbox.setLayout(self.groupbox_layout)
main_layout.addWidget(self.groupbox)
# 載入可熱重載的 Widget
self.reload_widget()
# 設定中央 Widget
central_widget = QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
def event(self, event):
if event.type() == QEvent.User:
self.reload_widget()
return True
return super().event(event)
def reload_widget(self):
DBG_PRINT("", tag="reload_widget")
# 清除舊 Widget
for i in reversed(range(self.groupbox_layout.count())):
self.groupbox_layout.itemAt(i).widget().setParent(None)
"""重新載入 GroupBox 內的 Widget"""
importlib.reload(my_widget) # 重新載入 UI
new_widget = my_widget.MyWidget()
# 加入新 Widget
self.groupbox_layout.addWidget(new_widget)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
# 啟動監聽(監測 `ui_module.py`,自動重載 Widget)
start_watcher(window)
sys.exit(app.exec())