-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
114 lines (96 loc) · 4.42 KB
/
app.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
# This file is part of the GrowthRateApp project.
#
# Copyright (C) 2024 Kristian Kinderlöv
#
# This program is free software; you can redistribute it and/or modify it under the terms of the
# GNU Lesser General Public License as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along with this program;
# if not, see <http://www.gnu.org/licenses/>.
#
# Author: Kristian Kinderlöv
# Email: [email protected]
import sys
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QGroupBox, QToolTip
from PySide6.QtGui import QFont, QDoubleValidator
from PySide6.QtCore import Qt
class GrowthRateApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Growth Rate Calculator')
self.setFixedSize(300, 300) # Set fixed size of the window
main_layout = QVBoxLayout()
# Font customization
result_font = QFont("Arial", 14, QFont.Bold)
# GroupBox for input fields
input_groupbox = QGroupBox("Input Values")
input_layout = QVBoxLayout()
# Current Value
current_layout = QVBoxLayout()
current_label = QLabel("Current:")
self.current_textbox = QLineEdit(self)
self.current_textbox.setValidator(QDoubleValidator(0.0, 1000000.0, 2))
self.current_textbox.textChanged.connect(self.calculate_growth_rate)
current_layout.addWidget(current_label)
current_layout.addWidget(self.current_textbox)
# Initial Value
initial_layout = QVBoxLayout()
initial_label = QLabel("Initial:")
self.initial_textbox = QLineEdit(self)
self.initial_textbox.setValidator(QDoubleValidator(0.0, 1000000.0, 2))
self.initial_textbox.textChanged.connect(self.calculate_growth_rate)
initial_layout.addWidget(initial_label)
initial_layout.addWidget(self.initial_textbox)
# Year Value
year_layout = QVBoxLayout()
year_label = QLabel("Year:")
self.year_textbox = QLineEdit(self)
self.year_textbox.setValidator(QDoubleValidator(0.0, 1000.0, 2))
self.year_textbox.setToolTip("Number of years")
self.year_textbox.textChanged.connect(self.calculate_growth_rate)
year_layout.addWidget(year_label)
year_layout.addWidget(self.year_textbox)
# Add input fields to the input groupbox layout
input_layout.addLayout(current_layout)
input_layout.addLayout(initial_layout)
input_layout.addLayout(year_layout)
input_groupbox.setLayout(input_layout)
# GroupBox for result
result_groupbox = QGroupBox("CAGR")
result_layout = QVBoxLayout()
#self.result_label = QLabel("Growth Rate: ")
self.result_label = QLabel()
self.result_label.setAlignment(Qt.AlignHCenter)
self.result_label.setAlignment(Qt.AlignVCenter)
self.result_label.setFont(result_font)
result_layout.addWidget(self.result_label, alignment=Qt.AlignCenter)
result_groupbox.setLayout(result_layout)
# Add groupboxes to the main layout
main_layout.addWidget(input_groupbox)
main_layout.addWidget(result_groupbox)
self.setLayout(main_layout)
self.show()
def calculate_growth_rate(self):
try:
current_value = float(self.current_textbox.text())
initial_value = float(self.initial_textbox.text())
years = float(self.year_textbox.text())
if initial_value == 0 or years == 0:
self.result_label.setText("Error: Initial value and years must be non-zero.")
return
growth_rate = ((current_value / initial_value) ** (1 / years) - 1) * 100
# self.result_label.setText(f"Growth Rate: {growth_rate:.2f}%")
self.result_label.setText(f"{growth_rate:.2f}%")
except ValueError:
self.result_label.setText("")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = GrowthRateApp()
sys.exit(app.exec())