Amazing-Python-Scripts
140 строк · 3.7 Кб
1# import libraries
2from tkinter import *3
4# initialized window
5root = Tk()6root.geometry('480x350')7root.resizable(0, 0)8root.title('Weight Converter')9
10# defining the function for converting weights
11
12
13def WeightConv():14
15# making textbox user-friendly that is editable16t1.configure(state='normal')17t1.delete("1.0", END)18
19t2.configure(state='normal')20t2.delete("1.0", END)21
22t3.configure(state='normal')23t3.delete("1.0", END)24
25t4.configure(state='normal')26t4.delete("1.0", END)27
28t5.configure(state='normal')29t5.delete("1.0", END)30
31t6.configure(state='normal')32t6.delete("1.0", END)33
34# exception handling35try:36kilograms = float(e1.get())37
38# insert the output in textboxes correct upto 2 places after decimal39t1.insert(END, "%.2f" % (kilograms * 5000))40t2.insert(END, "%.2f" % (kilograms * 1000))41t3.insert(END, "%.2f" % (kilograms * 35.274))42t4.insert(END, "%.2f" % (kilograms * 2.20462))43t5.insert(END, "%.2f" % (kilograms * 0.01))44t6.insert(END, "%.2f" % (kilograms * 0.001))45
46# if blank or invalid input is given then exception is thrown47except ValueError:48t1.insert(END, " ~ Invalid input ~ ")49t2.insert(END, " ~ Invalid input ~ ")50t3.insert(END, " ~ Invalid input ~ ")51t4.insert(END, " ~ Invalid input ~ ")52t5.insert(END, " ~ Invalid input ~ ")53t6.insert(END, " ~ Invalid input ~ ")54
55# making textbox uneditable56t1.configure(state='disabled')57t2.configure(state='disabled')58t3.configure(state='disabled')59t4.configure(state='disabled')60t5.configure(state='disabled')61t6.configure(state='disabled')62
63
64# creating a label to display
65l1 = Label(root, text="Enter the weight in kilograms (kg) : ")66l1.grid(row=1, column=1, columnspan=2)67value = StringVar()68
69# creating a entry box for input
70e1 = Entry(root, textvariable=value)71e1.grid(row=1, column=3, columnspan=2)72
73# create a button for conversion
74button = Button(root, text="Convert", command=WeightConv)75button.grid(row=2, column=2, columnspan=2, rowspan=2)76
77# make labels for textbox
78t1l1 = Label(root, text="kg to ct : ")79t1l1.grid(row=4, column=1, columnspan=1)80
81t2l2 = Label(root, text="kg to g : ")82t2l2.grid(row=5, column=1, columnspan=1)83
84t3l3 = Label(root, text="kg to oz : ")85t3l3.grid(row=6, column=1, columnspan=1)86
87t4l4 = Label(root, text="kg to lb : ")88t4l4.grid(row=7, column=1, columnspan=1)89
90t5l5 = Label(root, text="kg to q : ")91t5l5.grid(row=8, column=1, columnspan=1)92
93t6l6 = Label(root, text="kg to t : ")94t6l6.grid(row=9, column=1, columnspan=1)95
96t1r1 = Label(root, text="Carat")97t1r1.grid(row=4, column=4, columnspan=1)98
99t2r2 = Label(root, text="Gram")100t2r2.grid(row=5, column=4, columnspan=1)101
102t3r3 = Label(root, text="Ounce")103t3r3.grid(row=6, column=4, columnspan=1)104
105t4r4 = Label(root, text="Pound")106t4r4.grid(row=7, column=4, columnspan=1)107
108t5r5 = Label(root, text="Quintal")109t5r5.grid(row=8, column=4, columnspan=1)110
111t6r6 = Label(root, text="Tonne")112t6r6.grid(row=9, column=4, columnspan=1)113
114# creating textbox and defining grid to show output
115t1 = Text(root, height=1, width=20)116t1.grid(row=4, column=2, columnspan=2)117
118t2 = Text(root, height=1, width=20)119t2.grid(row=5, column=2, columnspan=2)120
121t3 = Text(root, height=1, width=20)122t3.grid(row=6, column=2, columnspan=2)123
124t4 = Text(root, height=1, width=20)125t4.grid(row=7, column=2, columnspan=2)126
127t5 = Text(root, height=1, width=20)128t5.grid(row=8, column=2, columnspan=2)129
130t6 = Text(root, height=1, width=20)131t6.grid(row=9, column=2, columnspan=2)132
133# making blank spaces in GUI
134for r in range(10):135root.grid_rowconfigure(r, minsize=30)136for c in range(6):137root.grid_columnconfigure(c, minsize=50)138
139# infinite loop to run program
140root.mainloop()141