-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.py
161 lines (133 loc) · 6.28 KB
/
mainwindow.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import sys
import serial
import serial.tools.list_ports
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from drawwindow import DrawWindow
from aboutwindow import AboutWindow
from serialhandler import SerialHandler
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle('NoidHost')
self.setWindowIcon(QIcon('gml.png'))
self.statusbar = self.statusBar()
self.statusbar.showMessage('等待连接')
menubar = self.menuBar()
# 菜单栏-文件
exit_action = QAction('&退出', self)
exit_action.setShortcut('Ctrl+Q')
exit_action.setStatusTip('退出应用')
exit_action.triggered.connect(lambda: sys.exit())
menubar_file = menubar.addMenu('文件')
menubar_file.addAction(exit_action)
# 菜单栏-曲线
draw_node2_action = QAction('&节点2', self)
draw_node3_action = QAction('&节点3', self)
draw_node4_action = QAction('&节点4', self)
draw_node5_action = QAction('&节点5', self)
self.node2_drawwindow = DrawWindow()
self.node3_drawwindow = DrawWindow()
self.node4_drawwindow = DrawWindow()
self.node5_drawwindow = DrawWindow()
menubar_draw = menubar.addMenu('曲线')
menubar_draw.addAction(draw_node2_action)
menubar_draw.addAction(draw_node3_action)
menubar_draw.addAction(draw_node4_action)
menubar_draw.addAction(draw_node5_action)
draw_node2_action.triggered.connect(
lambda: self.drawStart(2))
draw_node3_action.triggered.connect(
lambda: self.drawStart(3))
draw_node4_action.triggered.connect(
lambda: self.drawStart(4))
draw_node5_action.triggered.connect(
lambda: self.drawStart(5))
# 菜单栏-关于
self.aboutwindow = AboutWindow()
about_action = QAction('&关于', self)
about_action.setShortcut('Ctrl+A')
about_action.setStatusTip('关于')
about_action.triggered.connect(lambda: self.aboutwindow.show())
menubar_about = menubar.addMenu('关于')
menubar_about.addAction(about_action)
self.label_node2 = QLabel(
'节点:2 湿度: % 温度: °C 光照度: lx ')
self.label_node3 = QLabel(
'节点:3 湿度: % 温度: °C 光照度: lx ', self)
self.label_node4 = QLabel(
'节点:4 湿度: % 温度: °C 光照度: lx ', self)
self.label_node5 = QLabel(
'节点:5 湿度: % 温度: °C 光照度: lx ', self)
self.combobox = QComboBox()
self.port_list = serial.tools.list_ports.comports()
for port in self.port_list:
self.combobox.addItem(port.description)
self.btnStart = QPushButton('开始')
self.btnExit = QPushButton('退出')
self.btnScanPort = QPushButton('刷新串口')
# 实例化多线程对象
self.handler = SerialHandler(self.port_list[0].name)
# 把控件放置在栅格布局中
layout = QGridLayout()
layout.addWidget(self.label_node2, 1, 1, 1, 4)
layout.addWidget(self.label_node3, 2, 1, 1, 4)
layout.addWidget(self.label_node4, 3, 1, 1, 4)
layout.addWidget(self.label_node5, 4, 1, 1, 4)
layout.addWidget(self.btnScanPort, 5, 1, 1, 1)
layout.addWidget(self.combobox, 5, 2, 1, 1)
layout.addWidget(self.btnStart, 5, 3, 1, 1)
layout.addWidget(self.btnExit, 5, 4, 1, 1)
self.widget = QWidget()
self.widget.setLayout(layout)
self.widget.setStyleSheet("QLabel{font-size: 18pt;}")
self.setCentralWidget(self.widget)
# 信号与槽函数的连接
self.handler.sign_node.connect(self.slotUpdateNode)
self.combobox.currentIndexChanged.connect(self.slotSelectPort)
self.btnScanPort.clicked.connect(self.slotScanPort)
self.btnStart.clicked.connect(self.slotStart)
self.btnExit.clicked.connect(lambda: sys.exit())
def slotScanPort(self):
self.port_list = serial.tools.list_ports.comports()
self.combobox.clear()
for port in self.port_list:
self.combobox.addItem(port.description)
self.handler.port_name = self.port_list[0].name
def slotSelectPort(self, index):
self.handler.port_name = self.port_list[index].name
def slotUpdateNode(self, node_seq: int, node_data: dict):
# 更新
if (node_seq == 2):
self.label_node2.setText(
f"节点:2 湿度:{node_data['humi']}% 温度:{node_data['temp']}°C 光照度:{node_data['light']}lx")
if (node_seq == 3):
self.label_node3.setText(
f"节点:3 湿度:{node_data['humi']}% 温度:{node_data['temp']}°C 光照度:{node_data['light']}lx")
if (node_seq == 4):
self.label_node4.setText(
f"节点:4 湿度:{node_data['humi']}% 温度:{node_data['temp']}°C 光照度:{node_data['light']}lx")
if (node_seq == 5):
self.label_node5.setText(
f"节点:5 湿度:{node_data['humi']}% 温度:{node_data['temp']}°C 光照度:{node_data['light']}lx")
def slotStart(self):
self.combobox.setEnabled(False)
self.btnScanPort.setEnabled(False)
self.btnStart.setEnabled(False)
self.btnExit.setEnabled(True)
self.statusbar.showMessage('已连接')
self.handler.start()
def drawStart(self, node_seq: int):
if node_seq == 2:
self.handler.sign_node.connect(self.node2_drawwindow.addPoint)
self.node2_drawwindow.start(node_seq)
elif node_seq == 3:
self.handler.sign_node.connect(self.node3_drawwindow.addPoint)
self.node3_drawwindow.start(node_seq)
elif node_seq == 4:
self.handler.sign_node.connect(self.node4_drawwindow.addPoint)
self.node4_drawwindow.start(node_seq)
elif node_seq == 5:
self.handler.sign_node.connect(self.node5_drawwindow.addPoint)
self.node5_drawwindow.start(node_seq)