forked from zzccchen/at_to_wt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatwt.py
406 lines (360 loc) · 10.2 KB
/
atwt.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import tkinter as tk
from periodictable import elements
# Helper function to get atomic mass
def get_atomic_mass(symbol):
for element in elements:
if hasattr(element, "symbol") and element.symbol == symbol:
return element.mass
return None
# Atomic percent to weight percent conversion
def at_to_wt(at_percents, atomic_masses):
total_mass = sum(at_percents[i] * atomic_masses[i] for i in range(len(at_percents)))
return [
at_percents[i] * atomic_masses[i] / total_mass * 100
for i in range(len(at_percents))
]
# Weight percent to atomic percent conversion
def wt_to_at(wt_percents, atomic_masses):
total_moles = sum(
wt_percents[i] / atomic_masses[i] for i in range(len(wt_percents))
)
return [
wt_percents[i] / atomic_masses[i] / total_moles * 100
for i in range(len(wt_percents))
]
# Create the main window
root = tk.Tk()
root.title("Element Weight and Atomic Percent Calculator")
# Variables to store symbols and entries
element_symbols = []
entries = {}
# Function to create input fields
def create_input_fields(symbols):
for index, symbol in enumerate(symbols):
if symbol not in entries:
# Create a row frame for each element
row = tk.Frame(root)
row.grid(
row=periodic_table_last_row + 2 + index,
column=0,
sticky="ew",
padx=3,
pady=3,
)
root.grid_rowconfigure(periodic_table_last_row + 2 + index, weight=1)
# Create weight percentage labels and input boxes
label_wt = tk.Label(row, text=f"{symbol} (wt.%): ")
entry_wt = tk.Entry(row)
entries[symbol] = {"wt": entry_wt} # Storage weight percentage input box
label_wt.grid(row=0, column=0, sticky="w", padx=3, pady=3)
entry_wt.grid(row=0, column=1, sticky="ew", padx=3, pady=3)
# Create atomic percentage labels and input boxes
label_at = tk.Label(row, text=f"{symbol} (at.%): ")
entry_at = tk.Entry(row)
entries[symbol]["at"] = entry_at # Storage atomic percentage input box
label_at.grid(row=0, column=2, sticky="w", padx=3, pady=3)
entry_at.grid(row=0, column=3, sticky="ew", padx=3, pady=3)
# Create a label to display the atomic mass
atomic_mass = get_atomic_mass(symbol)
label_mass = tk.Label(row, text=f"Atomic Mass: {atomic_mass:.3f}")
label_mass.grid(row=0, column=4, sticky="w", padx=3, pady=3)
# Configure the weight of the column where the input box is located
row.grid_columnconfigure(1, weight=1)
row.grid_columnconfigure(3, weight=1)
# Function to handle element button click
def element_button_clicked(symbol):
if symbol not in element_symbols:
element_symbols.append(symbol)
create_input_fields(element_symbols)
# Layout periodic table by rows
periodic_table_layout = [
[
"H",
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
"He",
],
[
"Li",
"Be",
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
"B",
"C",
"N",
"O",
"F",
"Ne",
],
[
"Na",
"Mg",
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
"Al",
"Si",
"P",
"S",
"Cl",
"Ar",
],
[
"K",
"Ca",
"Sc",
"Ti",
"V",
"Cr",
"Mn",
"Fe",
"Co",
"Ni",
"Cu",
"Zn",
"Ga",
"Ge",
"As",
"Se",
"Br",
"Kr",
],
[
"Rb",
"Sr",
"Y",
"Zr",
"Nb",
"Mo",
"Tc",
"Ru",
"Rh",
"Pd",
"Ag",
"Cd",
"In",
"Sn",
"Sb",
"Te",
"I",
"Xe",
],
[
"Cs",
"Ba",
None,
"Hf",
"Ta",
"W",
"Re",
"Os",
"Ir",
"Pt",
"Au",
"Hg",
"Tl",
"Pb",
"Bi",
"Po",
"At",
"Rn",
],
[
"Fr",
"Ra",
None,
"Rf",
"Db",
"Sg",
"Bh",
"Hs",
"Mt",
"Ds",
"Rg",
"Cn",
"Nh",
"Fl",
"Mc",
"Lv",
"Ts",
"Og",
],
# The lanthanides
[
None,
None,
None,
"La",
"Ce",
"Pr",
"Nd",
"Pm",
"Sm",
"Eu",
"Gd",
"Tb",
"Dy",
"Ho",
"Er",
"Tm",
"Yb",
"Lu",
],
# The actinides
[
None,
None,
None,
"Ac",
"Th",
"Pa",
"U",
"Np",
"Pu",
"Am",
"Cm",
"Bk",
"Cf",
"Es",
"Fm",
"Md",
"No",
"Lr",
],
]
periodic_table_last_row = len(periodic_table_layout) + 2
# Define the uniform width of the button
button_width = 3
# Create GUI layout based on periodic table layout
for period, row_elements in enumerate(periodic_table_layout, start=1):
frame = tk.Frame(root)
frame.grid(row=period, column=0, sticky="ew", padx=1, pady=1)
# Configure frame to expand with the window
root.grid_rowconfigure(period, weight=1)
root.grid_columnconfigure(0, weight=1)
for col, symbol in enumerate(row_elements, start=1):
frame.grid_columnconfigure(col, weight=1)
if symbol:
btn = tk.Button(
frame,
text=symbol,
command=lambda s=symbol: element_button_clicked(s),
width=button_width,
)
btn.grid(row=0, column=col, sticky="ew", padx=1, pady=1)
else:
# For None placeholder, create an empty label for alignment
label = tk.Label(frame, text=" ", width=button_width)
label.grid(row=0, column=col, sticky="ew", padx=1, pady=1)
# Create the Reset button and corresponding click event handler function
def reset_button_clicked():
global entries, element_symbols
# Clear the recorded element symbol list
element_symbols.clear()
# Delete all input controls and their frames
for symbol in list(entries.keys()):
# Delete Entry control
entries[symbol]["wt"].master.destroy()
entries[symbol]["at"].master.destroy()
# Remove the element from the entries dictionary
del entries[symbol]
# Function to handle wt to at conversion
def convert_wt2at_button_clicked():
try:
wt_percents = [
float(entries[symbol]["wt"].get()) if entries[symbol]["wt"].get() else None
for symbol in entries
]
symbols = list(entries.keys())
# If the input of the first element is empty,
# calculate the sum of the weight percentages of the remaining elements
if wt_percents[0] is None:
remaining_sum = sum(filter(None, wt_percents[1:]))
wt_percents[0] = 100.0 - remaining_sum
# Fill the calculated value back into the input box of the first element
entries[symbols[0]]["wt"].delete(0, tk.END)
entries[symbols[0]]["wt"].insert(0, str(wt_percents[0]))
atomic_masses = [get_atomic_mass(symbol) for symbol in symbols]
at_percents = wt_to_at(wt_percents, atomic_masses)
for i, symbol in enumerate(symbols):
entries[symbol]["at"].delete(0, tk.END)
entries[symbol]["at"].insert(0, str(at_percents[i]))
except ValueError:
# Handle input errors
reset_button_clicked()
# Function to handle at to wt conversion
def convert_at2wt_button_clicked():
try:
at_percents = [
float(entries[symbol]["at"].get()) if entries[symbol]["at"].get() else None
for symbol in entries
]
symbols = list(entries.keys())
# If the input of the first element is empty,
# calculate the sum of the atomic percentages of the remaining elements
if at_percents[0] is None:
remaining_sum = sum(filter(None, at_percents[1:]))
at_percents[0] = 100.0 - remaining_sum
# Fill the calculated value back into the input box of the first element
entries[symbols[0]]["at"].delete(0, tk.END)
entries[symbols[0]]["at"].insert(0, str(at_percents[0]))
atomic_masses = [get_atomic_mass(symbol) for symbol in symbols]
wt_percents = at_to_wt(at_percents, atomic_masses)
for i, symbol in enumerate(symbols):
entries[symbol]["wt"].delete(0, tk.END)
entries[symbol]["wt"].insert(0, str(wt_percents[i]))
except ValueError:
# Handle input errors
reset_button_clicked()
# Create a new Frame for placing buttons
button_frame = tk.Frame(root)
button_frame.grid(row=periodic_table_last_row, column=0, sticky="ew", padx=1, pady=1)
# Configure the column weight of the button Frame so that the button can equally divide the space
button_frame.grid_columnconfigure(0, weight=2)
button_frame.grid_columnconfigure(1, weight=2)
button_frame.grid_columnconfigure(2, weight=1)
# Create two convert buttons and put them into the button Frame
convert_button = tk.Button(
button_frame, text="wt.% to at.%", command=convert_wt2at_button_clicked
)
convert_button.grid(row=0, column=0, sticky="ew", padx=1, pady=1)
convert_button = tk.Button(
button_frame, text="at.% to wt.%", command=convert_at2wt_button_clicked
)
convert_button.grid(row=0, column=1, sticky="ew", padx=1, pady=1)
# Create a Reset button and put it into the button Frame
reset_button = tk.Button(button_frame, text="Reset", command=reset_button_clicked)
reset_button.grid(row=0, column=2, sticky="ew", padx=1, pady=1)
root.mainloop()