38.7k views
5 votes
Write a GUI-based program that allows the user to convert temperature values between degrees Fahrenheit and degrees Celsius. The interface should have labeled entry fields for these two values. These components should be arranged in a grid where the labels occupy the first row and the corresponding fields occupy the second row. At start-up, the Fahrenheit field should contain 32.0, and the Celsius field should contain 0.0. The third row in the window contains two command buttons, labeled >>>> and <<<<. When the user presses the first button, the program should use the data in the Celsius field to compute the Fahrenheit value, which should then be output to the Fahrenheit field. The second button should perform the inverse function.

User Pjco
by
4.8k points

2 Answers

5 votes

Answer:

def CelsiusToFahenheit(Celsius, entry):

#Celsius = int(input("Enter a temperature in Celsius: "))

Fahrenheit = 9.0/5.0 * float(Celsius) + 32

entry. delete(0, 'end')

entry.insert(0, str(Fahrenheit))

def FahenheitToCelsius(Fahrenheit, entry):

#!Fahrenheit = int(input("Enter a temperature in Fahrenheit: "))

Celsius = (float(Fahrenheit) - 32) * 5.0/9.0

entry. delete(0, 'end')

entry.insert(0, str(Celsius))

def main():

window = Tkinter.Tk()

window.title("Grid")

lb = Tkinter.Label(window, text='Fahrenheit')

lb.grid (row=0,column=0)

lb1=Tkinter.Label(window,text='Celsius:')

lb1.grid (row=0,column=1)

en =Tkinter. Entry(window, justify='right')

en.grid(row=1,column=0)

en.insert(0, "32.0")

en1=Tkinter.Entry(window, justify='right')

en1.grid(row=1,column=1)

en1.insert(0, "0.0")

btnFtoc =Tkinter.Button(window, text='>>>>', command=lambda: FahenheitToCelsius(en.get(), en1))

btnFtoc.grid(row=2,column=0)

btnCtoF =Tkinter.Button(window, text='<<<<', command=lambda: CelsiusToFahenheit(en1.get(), en))

btnCtoF.grid(row=2,column=1)

window.mainloop()

if __name__ == '__main__':

main()

User Ivan Hamilton
by
5.6k points
3 votes

Answer:

import tkinter as tk

from functools import partial

# global variable

tempVal = "Celsius"

# getting drop down value

def store_temp(sel_temp):

global tempVal

tempVal = sel_temp

# the main conversion

def call_convert(rlabel1, rlabe12, inputn):

tem = inputn.get()

if tempVal == 'Celsius':

f = float((float(tem) * 9 / 5) + 32)

k = float((float(tem) + 273.15))

rlabel1.config(text="%f Fahrenheit" % f)

rlabe12.config(text="%f Kelvin" % k)

if tempVal == 'Fahrenheit':

c = float((float(tem) - 32) * 5 / 9)

k = c + 273

rlabel1.config(text="%f Celsius" % c)

rlabe12.config(text="%f Kelvin" % k)

if tempVal == 'Kelvin':

c = float((float(tem) - 273.15))

f = float((float(tem) - 273.15) * 1.8000 + 32.00)

rlabel1.config(text="%f Celsius" % c)

rlabe12.config(text="%f Fahrenheit" % f)

return

# app window configuration and UI

root = tk.Tk()

root.geometry('400x150+100+200')

root.title('Temperature Converter')

root.configure(background='#09A3BA')

root.resizable(width=False, height=False)

root.grid_columnconfigure(1, weight=1)

root.grid_rowconfigure(0, weight=1)

numberInput = tk.StringVar()

var = tk.StringVar()

# label and entry field

input_label = tk.Label(root, text="Enter temperature", background='#09A3BA', foreground="#FFFFFF")

input_entry = tk.Entry(root, textvariable=numberInput)

input_label.grid(row=1)

input_entry.grid(row=1, column=1)

# result label's for showing the other two temperatures

result_label1 = tk.Label(root, background='#09A3BA', foreground="#FFFFFF")

result_label1.grid(row=3, columnspan=4)

result_label2 = tk.Label(root, background='#09A3BA', foreground="#FFFFFF")

result_label2.grid(row=4, columnspan=4)

# drop down initalization and setup

dropDownList = ["Celsius", "Fahrenheit", "Kelvin"]

dropdown = tk.OptionMenu(root, var, *dropDownList, command=store_temp)

var.set(dropDownList[0])

dropdown.grid(row=1, column=3)

dropdown.config(background='#09A3BA', foreground="#FFFFFF")

dropdown["menu"].config(background='#09A3BA', foreground="#FFFFFF")

# button click

call_convert = partial(call_convert, result_label1, result_label2, numberInput)

result_button = tk.Button(root, text="Convert", command=call_convert, background='#09A3BA', foreground="#FFFFFF")

result_button.grid(row=2, columnspan=4)

root.mainloop()

Step-by-step explanation:

User Loading
by
5.9k points