-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.py
More file actions
215 lines (174 loc) · 5.62 KB
/
converter.py
File metadata and controls
215 lines (174 loc) · 5.62 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# converter.py
# -*- coding: utf-8 -*-
'''
Converter of lenght, mass, time and temperature
SI units and Imperial units
'''
# Modules
from PySide6.QtWidgets import (
QApplication, QWidget, QLabel, QLineEdit, QComboBox,
QPushButton, QVBoxLayout, QHBoxLayout
)
import sys
# Base units:
# - Length: meter (m)
# - Mass: kilogram (kg)
# - Time: second (s)
# Temperature uses formulas, not factors.
CONVERSION_FACTORS = {
"Length": {
# SI units
"m": 1,
"km": 1000,
"cm": 0.01,
"mm": 0.001,
# Imperial units
"in": 0.0254,
"ft": 0.3048,
"yd": 0.9144,
"mi": 1609.34
},
"Mass": {
# SI units
"kg": 1,
"g": 0.001,
"mg": 0.000001,
# Imperial units
"oz": 0.0283495,
"lb": 0.453592
},
"Time": {
"s": 1,
"min": 60,
"h": 3600
}
}
UNITS = {
"Length": ["m", "km", "cm", "mm", "in", "ft", "yd", "mi"],
"Mass": ["kg", "g", "mg", "oz", "lb"],
"Time": ["s", "min", "h"],
"Temperature": ["C", "F", "K"]
}
def convert_temperature(value, from_u, to_u):
'''Convert temperature between Celsius, Fahrenheit, and Kelvin'''
# Convert from source unit to Celsius
if from_u == "C":
c = value
elif from_u == "F":
c = (value - 32) * 5/9
elif from_u == "K":
c = value - 273.15
# Convert from Celsius to target unit
if to_u == "C":
return c
elif to_u == "F":
return c * 9/5 + 32
elif to_u == "K":
return c + 273.15
# Convert non-temperature units
def convert_generic(value, from_unit, to_unit, category):
'''Convert using base-unit normalization'''
factors = CONVERSION_FACTORS[category]
# Convert to base unit
value_in_base = value * factors[from_unit]
# Convert from base unit to target unit
result = value_in_base / factors[to_unit]
return result
# GUI Class
class Converter(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Unit Converter")
# Widgets
self.value_input = QLineEdit()
self.category_combo = QComboBox()
self.from_combo = QComboBox()
self.to_combo = QComboBox()
self.result_label = QLabel("Result: ")
self.convert_button = QPushButton("Convert")
# CSS for category ComboBox
self.category_combo.setStyleSheet("""
QComboBox {
background-color: #778899;
color: black;
padding: 4px;
border-radius: 4px;
}
QComboBox QAbstractItemView {
background-color: #BBBBBB;
color: black;
}
""")
# CSS for Convert button
self.convert_button.setStyleSheet("""
QPushButton {
background-color: #ADD8E6;
color: black;
padding: 6px;
border-radius: 4px;
}
QPushButton:hover {
background-color: #9CC7D6;
}
""")
self.category_combo.addItems(UNITS.keys())
# Layouts
layout = QVBoxLayout()
# Value row
row_value = QHBoxLayout()
row_value.addWidget(QLabel("Value:"))
row_value.addWidget(self.value_input)
layout.addLayout(row_value)
# Category row
row_cat = QHBoxLayout()
row_cat.addWidget(QLabel("Category:"))
row_cat.addWidget(self.category_combo)
layout.addLayout(row_cat)
# From row
row_from = QHBoxLayout()
row_from.addWidget(QLabel("From:"))
row_from.addWidget(self.from_combo)
layout.addLayout(row_from)
# To row
row_to = QHBoxLayout()
row_to.addWidget(QLabel("To:"))
row_to.addWidget(self.to_combo)
layout.addLayout(row_to)
# Convert button
layout.addWidget(self.convert_button)
# Result label
layout.addWidget(self.result_label)
self.setLayout(layout)
# Events
self.category_combo.currentTextChanged.connect(self.update_units)
self.convert_button.clicked.connect(self.do_conversion)
# Initialize units
self.update_units()
def update_units(self):
'''Update unit dropdowns when category changes'''
category = self.category_combo.currentText()
units = UNITS.get(category, [])
self.from_combo.clear()
self.to_combo.clear()
self.from_combo.addItems(units)
self.to_combo.addItems(units)
def do_conversion(self):
'''Perform the conversion and update the result label'''
try:
value = float(self.value_input.text())
category = self.category_combo.currentText()
from_unit = self.from_combo.currentText()
to_unit = self.to_combo.currentText()
if category == "Temperature": # Convert temperature units
result = convert_temperature(value, from_unit, to_unit)
else: # Convert non-temperature units
result = convert_generic(value, from_unit, to_unit, category)
self.result_label.setText(f"Result: {result:.6g} {to_unit}")
except ValueError:
self.result_label.setText("Error: invalid value")
# Main app
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Converter()
window.show()
sys.exit(app.exec())