|
| 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" % (kilograms * 5000)) |
| 38 | + t2.insert(END, "%.2f" % (kilograms * 1000)) |
| 39 | + t3.insert(END, "%.2f" % (kilograms * 35.274)) |
| 40 | + t4.insert(END, "%.2f" % (kilograms * 2.20462)) |
| 41 | + t5.insert(END, "%.2f" % (kilograms * 0.01)) |
| 42 | + t6.insert(END, "%.2f" % (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 | +t1r1 = Label(root, text = "Carat") |
| 94 | +t1r1.grid(row = 4, column = 4, columnspan=1) |
| 95 | + |
| 96 | +t2r2 = Label(root, text = "Gram") |
| 97 | +t2r2.grid(row = 5, column = 4, columnspan=1) |
| 98 | + |
| 99 | +t3r3 = Label(root, text = "Ounce") |
| 100 | +t3r3.grid(row = 6, column = 4, columnspan=1) |
| 101 | + |
| 102 | +t4r4 = Label(root, text = "Pound") |
| 103 | +t4r4.grid(row = 7, column = 4, columnspan=1) |
| 104 | + |
| 105 | +t5r5 = Label(root, text = "Quintal") |
| 106 | +t5r5.grid(row = 8, column = 4, columnspan=1) |
| 107 | + |
| 108 | +t6r6 = Label(root, text = "Tonne") |
| 109 | +t6r6.grid(row = 9, column = 4, columnspan=1) |
| 110 | + |
| 111 | +# creating textbox and defining grid to show output |
| 112 | +t1 = Text(root, height = 1, width = 20) |
| 113 | +t1.grid(row = 4, column = 2, columnspan = 2) |
| 114 | + |
| 115 | +t2 = Text(root, height = 1, width = 20) |
| 116 | +t2.grid(row = 5, column = 2, columnspan = 2) |
| 117 | + |
| 118 | +t3 = Text(root, height = 1, width = 20) |
| 119 | +t3.grid(row = 6, column = 2, columnspan = 2) |
| 120 | + |
| 121 | +t4 = Text(root, height = 1, width = 20) |
| 122 | +t4.grid(row = 7, column = 2, columnspan = 2) |
| 123 | + |
| 124 | +t5 = Text(root, height = 1, width = 20) |
| 125 | +t5.grid(row = 8, column = 2, columnspan = 2) |
| 126 | + |
| 127 | +t6 = Text(root, height = 1, width = 20) |
| 128 | +t6.grid(row = 9, column = 2, columnspan = 2) |
| 129 | + |
| 130 | +# making blank spaces in GUI |
| 131 | +for r in range(10): |
| 132 | + root.grid_rowconfigure(r,minsize = 30) |
| 133 | +for c in range(6): |
| 134 | + root.grid_columnconfigure(c,minsize = 50) |
| 135 | + |
| 136 | +# infinite loop to run program |
| 137 | +root.mainloop() |
0 commit comments