from tkinter import *
from tkinter.messagebox import *
def show_answer1():
# fahrenheit = (celsius * 1.8) + 32
Ans = (float(num1.get()) * 1.8 + 32 )
Ans = "{:.3f}".format(Ans)
blank1.delete('0', END)
blank1.insert(0, Ans)
Label(main, text = "華氏溫度").grid(row=2,column=2)
def show_answer2():
# celsius = (fahrenheit-32) * 5/9
Ans = ((float(num1.get())- 32 ) * 5/9 )
Ans = "{:.3f}".format(Ans)
blank1.delete('0', END)
blank1.insert(0, Ans)
Label(main, text = "攝氏溫度").grid(row=2,column=2)
# 華氏= 攝氏*(9/5)+32
# 攝氏= (華氏-32)*5/9
main = Tk()
main.title("Tkinter 攝氏溫度華氏溫度互換 ")
Label(main, text = "輸入溫度 :").grid(row=0)
Label(main, text = "轉換的溫度 =").grid(row=2)
num1 = Entry(main)
blank1 = Entry(main)
num1.grid(row=0, column=1)
blank1.grid(row=2, column=1)
Button(main, text='離開', command=main.destroy).grid(row=4, column=0, sticky=W, pady=4)
Button(main, text='攝氏轉華氏', command=show_answer1).grid(row=4, column=1, sticky=W, pady=4)
Button(main, text='華氏轉攝氏', command=show_answer2).grid(row=4, column=2, sticky=W, pady=4)
mainloop()
沒有留言:
張貼留言