Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 162de46

Browse files
Added weight converter
1 parent d92f6c1 commit 162de46

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# import libraries
2+
from tkinter import *
3+
4+
# initialized window
5+
root = Tk()
6+
root.geometry('480x350')
7+
root.resizable(0,0)
8+
root.title('Weight Converter')
9+
10+
# defining the function for converting weights
11+
def WeightConv():
12+
13+
# making textbox user-friendly that is editable
14+
t1.configure(state = 'normal')
15+
t1.delete("1.0", END)
16+
17+
t2.configure(state = 'normal')
18+
t2.delete("1.0", END)
19+
20+
t3.configure(state = 'normal')
21+
t3.delete("1.0", END)
22+
23+
t4.configure(state='normal')
24+
t4.delete("1.0", END)
25+
26+
t5.configure(state='normal')
27+
t5.delete("1.0", END)
28+
29+
t6.configure(state='normal')
30+
t6.delete("1.0", END)
31+
32+
# exception handling
33+
try:
34+
kilograms = float(e1.get())
35+
36+
# insert the output in textboxes correct upto 2 places after decimal
37+
t1.insert(END, "%.2f Carat" % (kilograms * 5000))
38+
t2.insert(END, "%.2f Gram" % (kilograms * 1000))
39+
t3.insert(END, "%.2f Ounce" % (kilograms * 35.274))
40+
t4.insert(END, "%.2f Pound" % (kilograms * 2.20462))
41+
t5.insert(END, "%.2f Quintal" % (kilograms * 0.01))
42+
t6.insert(END, "%.2f Tonne" % (kilograms * 0.001))
43+
44+
# if blank or invalid input is given then exception is thrown
45+
except ValueError:
46+
t1.insert(END, " ~ Invalid input ~ ")
47+
t2.insert(END, " ~ Invalid input ~ ")
48+
t3.insert(END, " ~ Invalid input ~ ")
49+
t4.insert(END, " ~ Invalid input ~ ")
50+
t5.insert(END, " ~ Invalid input ~ ")
51+
t6.insert(END, " ~ Invalid input ~ ")
52+
53+
# making textbox uneditable
54+
t1.configure(state='disabled')
55+
t2.configure(state='disabled')
56+
t3.configure(state='disabled')
57+
t4.configure(state='disabled')
58+
t5.configure(state='disabled')
59+
t6.configure(state='disabled')
60+
61+
# creating a label to display
62+
l1 = Label(root, text = "Enter the weight in kilograms (kg) : ")
63+
l1.grid(row = 1, column = 1,columnspan = 2)
64+
value = StringVar()
65+
66+
# creating a entry box for input
67+
e1 = Entry(root, textvariable=value)
68+
e1.grid(row = 1, column = 3,columnspan = 2)
69+
70+
# create a button for conversion
71+
button = Button(root, text = "Convert", command = WeightConv)
72+
button.grid(row = 2, column = 2, columnspan = 2, rowspan = 2)
73+
74+
# make labels for textbox
75+
t1l1 = Label(root, text = "kg to ct : ")
76+
t1l1.grid(row = 4, column = 1, columnspan=1)
77+
78+
t2l2 = Label(root, text = "kg to g : ")
79+
t2l2.grid(row = 5, column = 1, columnspan=1)
80+
81+
t3l3 = Label(root, text = "kg to oz : ")
82+
t3l3.grid(row = 6, column = 1, columnspan=1)
83+
84+
t4l4 = Label(root, text = "kg to lb : ")
85+
t4l4.grid(row = 7, column = 1, columnspan=1)
86+
87+
t5l5 = Label(root, text = "kg to q : ")
88+
t5l5.grid(row = 8, column = 1, columnspan=1)
89+
90+
t6l6 = Label(root, text = "kg to t : ")
91+
t6l6.grid(row = 9, column = 1, columnspan=1)
92+
93+
# creating textbox and defining grid to show output
94+
t1 = Text(root, height = 1, width = 20)
95+
t1.grid(row = 4, column = 2, columnspan = 2)
96+
97+
t2 = Text(root, height = 1, width = 20)
98+
t2.grid(row = 5, column = 2, columnspan = 2)
99+
100+
t3 = Text(root, height = 1, width = 20)
101+
t3.grid(row = 6, column = 2, columnspan = 2)
102+
103+
t4 = Text(root, height = 1, width = 20)
104+
t4.grid(row = 7, column = 2, columnspan = 2)
105+
106+
t5 = Text(root, height = 1, width = 20)
107+
t5.grid(row = 8, column = 2, columnspan = 2)
108+
109+
t6 = Text(root, height = 1, width = 20)
110+
t6.grid(row = 9, column = 2, columnspan = 2)
111+
112+
# making blank spaces in GUI
113+
for r in range(10):
114+
root.grid_rowconfigure(r,minsize = 30)
115+
for c in range(6):
116+
root.grid_columnconfigure(c,minsize = 50)
117+
118+
# infinite loop to run program
119+
root.mainloop()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /