|
| 1 | +from tkinter import * |
| 2 | +import tkinter.messagebox as tmsg |
| 3 | + |
| 4 | +def calculate(): |
| 5 | + c = c_value.get() |
| 6 | + f = f_value.get() |
| 7 | + k = k_value.get() |
| 8 | + if c !=0: |
| 9 | + f = (c*(9/5)) + 32 |
| 10 | + k = c + 273.15 |
| 11 | + f_value.set(f) |
| 12 | + k_value.set(k) |
| 13 | + elif f != 0: |
| 14 | + c = (f-32) * (5/9) |
| 15 | + k = ((5/9) + f) + 459.67 |
| 16 | + c_value.set(c) |
| 17 | + k_value.set(k) |
| 18 | + elif k != 0: |
| 19 | + c = k - 273.15 |
| 20 | + f = ((k - 273.15) * (9/5)) + 32 |
| 21 | + c_value.set(c) |
| 22 | + f_value.set(f) |
| 23 | + else: |
| 24 | + tmsg.showerror("Error","Enter 1 value and press calculate") |
| 25 | + |
| 26 | +def show(): |
| 27 | + show_root = Tk() |
| 28 | + show_root.title("Formula List") |
| 29 | + show_root.geometry("300x300") |
| 30 | + show_root.minsize(300,300) |
| 31 | + show_root.maxsize(300,300) |
| 32 | + show_root.configure(bg = "powder blue") |
| 33 | + lbs = Label(show_root,bg = "powder blue", text="Formulae: " ,font ="comicsansms 12 bold", padx=5, pady=5) |
| 34 | + lbs.grid(row=0, column=3) |
| 35 | + lbs = Label(show_root,bg = "powder blue", text="Celsius conversion: \nF = (9/5 x C) + 32 \nK = C + 273.15 " ,font ="comicsansms 10 bold",padx=5) |
| 36 | + lbs.grid(row=1, column=3) |
| 37 | + lbs = Label(show_root,bg = "powder blue", text="Fahrenheit conversion: \nC = (F - 32) x 5/9 \nK = (5/9 x F) + 459.67 " ,font ="comicsansms 10 bold",padx=5) |
| 38 | + lbs.grid(row=2, column=3) |
| 39 | + lbs = Label(show_root,bg = "powder blue", text="Kelvin conversion: \nC = K - 273.15 \nF = ((K - 273.15) x 9/5) + 32 ", font="comicsansms 10 bold", padx=5) |
| 40 | + lbs.grid(row=3, column=3) |
| 41 | + show_root.mainloop() |
| 42 | + |
| 43 | +def reset(): |
| 44 | + c_value.set(0) |
| 45 | + f_value.set(0) |
| 46 | + k_value.set(0) |
| 47 | + |
| 48 | +root = Tk() |
| 49 | +root.title("Temperature Converter") |
| 50 | +root.geometry("500x500") |
| 51 | +root.minsize(500,500) |
| 52 | +root.maxsize(500,500) |
| 53 | +root.configure(bg="orange") |
| 54 | + |
| 55 | +lb = Label(root, text="Temperature converter", bg="orange", fg="white", font = "comicsansms 20 bold", padx=10, pady=20) |
| 56 | +lb.grid(row=0,column=3) |
| 57 | + |
| 58 | +lb_c = Label(root, text="Celsius", bg="orange", padx=10, pady=10, font="comicsansms 14", fg="white") |
| 59 | +lb_c.grid(row=1, column=2) |
| 60 | +lb_f = Label(root, text="Fahrenheit", bg="orange", padx=10, pady=10, font="comicsansms 14", fg="white") |
| 61 | +lb_f.grid(row=2, column=2) |
| 62 | +lb_k = Label(root, text="Kelvin", bg="orange", padx=10, pady=10, font="comicsansms 14", fg="white") |
| 63 | +lb_k.grid(row=3, column=2) |
| 64 | + |
| 65 | +c_value = IntVar() |
| 66 | +f_value = IntVar() |
| 67 | +k_value = IntVar() |
| 68 | + |
| 69 | +c_entry = Entry(root,textvariable=c_value) |
| 70 | +c_entry.grid(row=1,column=3) |
| 71 | +f_entry = Entry(root,textvariable=f_value) |
| 72 | +f_entry.grid(row=2,column=3) |
| 73 | +k_entry = Entry(root,textvariable=k_value) |
| 74 | +k_entry.grid(row=3,column=3) |
| 75 | + |
| 76 | +fr = Frame(root) |
| 77 | +fr.grid(row=5,column=3) |
| 78 | +l = Label(bg="orange") #Empty label |
| 79 | +l.grid(row=4) |
| 80 | +b = Button(fr, text="Calculate", command=calculate, width=15) |
| 81 | +b.grid(row=5, column=3) |
| 82 | + |
| 83 | +fr = Frame(root) |
| 84 | +fr.grid(row=7,column=3) |
| 85 | +l = Label(bg="orange") #Empty label |
| 86 | +l.grid(row=6) |
| 87 | +b = Button(fr, text="Reset", command=reset, width=15) |
| 88 | +b.grid(row=7, column=3) |
| 89 | + |
| 90 | +fr = Frame(root) |
| 91 | +fr.grid(row=5,column=2) |
| 92 | +b = Button(fr, text="Show conversion formulas", command=show) |
| 93 | +b.grid(row=5, column=2) |
| 94 | + |
| 95 | +root.mainloop() |
0 commit comments