-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadcell.py
92 lines (74 loc) · 3.42 KB
/
loadcell.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
#MUST BE PYTHON 3.8.8
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from PyQt5.QtCore import Qt, QRect, QBasicTimer, QPoint # Import QPoint
from PyQt5.QtGui import QPainter, QPen, QFont, QColor, QPolygon
class Load_Cell(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setMaximumSize(100, 60) # Increased height to accommodate the reading text
self.value = 0
self.max_value = 500
self.value_colors =[(0,Qt.yellow),(150, Qt.green), (300, Qt.yellow), (350, Qt.red), (500, Qt.red)]
self.direction = 1 # 1 for increasing, -1 for decreasing
self.timer = QBasicTimer()
self.timer.start(10, self)
# def timerEvent(self, event):
# if self.value >= 1700:
# self.direction = -1
# elif self.value <= 1400:
# self.direction = 1
# self.value += self.direction
# self.update()
def setValue(self, value):
self.value = value
self.update()
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
# Draw gauge background
gauge_rect = QRect(10, 10, self.width() - 20, self.height() - 50) # Decreased height for the gauge
total_range = self.value_colors[-1][0] - self.value_colors[0][0]
for i in range(len(self.value_colors) - 1):
start_threshold, start_color = self.value_colors[i]
end_threshold, end_color = self.value_colors[i + 1]
start_pos = int(gauge_rect.left() + gauge_rect.width() * start_threshold / total_range)
end_pos = int(gauge_rect.left() + gauge_rect.width() * end_threshold / total_range)
section_rect = QRect(start_pos, gauge_rect.top(), end_pos - start_pos, gauge_rect.height())
painter.setPen(QPen(start_color, 2))
painter.setBrush(start_color)
painter.drawRect(section_rect)
# Calculate dial position within the valid range
dial_pos = int(gauge_rect.left() + gauge_rect.width() * self.value / self.max_value)
dial_pos = max(gauge_rect.left(), min(dial_pos, gauge_rect.right() - 10))
# Draw dial as a triangle
dial_triangle = QPolygon([
QPoint(dial_pos, gauge_rect.bottom() - 2), # Bottom point of the triangle
QPoint(dial_pos + 5, gauge_rect.bottom() + 8), # Top-right point of the triangle
QPoint(dial_pos - 5, gauge_rect.bottom() + 8) # Top-left point of the triangle
])
painter.setPen(QPen(Qt.black, 2))
painter.setBrush(Qt.black)
painter.drawPolygon(dial_triangle)
# Draw value text below the gauge
value_text = f'{self.value} lbf'
font = QFont(self.font())
font.setBold(True)
painter.setFont(font)
painter.setPen(QPen(Qt.black))
text_rect = QRect(0, self.height() - 40, self.width(), 40)
painter.drawText(text_rect, Qt.AlignCenter, value_text)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Linear Gauge")
self.setGeometry(100, 100, 300, 80)
self.gauge = P10_Gauge(self)
self.setCentralWidget(self.gauge)
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
input_value = 423
mainWindow.gauge.setValue(input_value)
sys.exit(app.exec_())