Question

Write a Python application that allows the user to convert between temperatures in Fahrenheit and temperatures in Celsius. Be

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Make sure the below files are saved as model.py, view.py and controller.py

#code

#model.py file containing Model class

class Model:
    #method that converts temperature in celsius to fahrenheit
   
def convertToFahrenheit(self, celsius):
        return (celsius*(9/5))+32

    # method that converts temperature in fahrenheit to celsius
   
def convertToCelsius(self, fahrenheit):
        return (fahrenheit-32)*(5/9)

#view.py file containing View class



from tkinter import *


#View class is extended from Frame class of tkinter

class View(Frame):
    #constructor
    def __init__(self, root=None):
        #invoking super class constructor
        super(View, self).__init__(root)
        #setting up StringVar and IntVar variables for accessing/mutating values of UI components
        self.inputText = StringVar()
        self.outputText = StringVar()
        self.radioChoice = IntVar()
        #creating required labels entries and radio buttons
        Label(self, text='Input Temperature: ').grid(row=0, column=0)
        #note that inputText StringVar is linked with below entry, so the contents of below
        #Entry object can be accessed by calling inputText.get() and can be mutated by calling
        #inputText.set(...). the same applies to other variables
        Entry(self, textvariable=self.inputText).grid(row=0, column=1,columnspan=2,sticky='ew')
        Label(self,text='Source: ').grid(row=1,column=0)
        Radiobutton(self,text='Celsius',variable=self.radioChoice,value=1).grid(row=1,column=1)
        Radiobutton(self, text='Fahrenheit', variable=self.radioChoice, value=2).grid(row=1, column=2)
        Label(self, text='Output Temperature: ').grid(row=2, column=0)
        Label(self, textvariable=self.outputText).grid(row=2, column=1,columnspan=2,sticky='ew')
        #selecting value 1 for radio choice, which will select celsius as default source temperature
        self.radioChoice.set(1)



    #returns the input temperature converted to float
    def getInputTemperature(self):
        return float(self.inputText.get())



    #updates the output label with the text provided
    def setOutput(self, output):
        self.outputText.set(output)



    #returns True if celsius is selected as the current source temperature
    def isCelsiusSelected(self):
        return self.radioChoice.get()==1
#controller.py containing Controller class


import model,viewimport tkinter

class Controller:
    #constructor initializing GUI
    def __init__(self):
        #creating a model
        self._model = model.Model()
        #creating a root window
        root = tkinter.Tk()
        #creating a view
        self._view = view.View(root)
        #adding view to the gui
        self._view.pack()
        #creating a convert button, that calls convert() method
        b=tkinter.Button(root,text='Convert',command=self.convert).pack()
        #staying until user clicks close button
        root.mainloop()


    #method to perform conversion
    def convert(self):
        try:
            #getting input temperature
            tempSource=self._view.getInputTemperature()
            #getting the source temperature type
            if self._view.isCelsiusSelected():
                #celsius is selected, converting to fahrenheit
                tempDest=self._model.convertToFahrenheit(tempSource)
                #displaying output with temperature rounded to 2 decimal places
                self._view.setOutput('{:.2f}F'.format(tempDest))
            else:
                #converting to celsius
                tempDest = self._model.convertToCelsius(tempSource)
                self._view.setOutput('{:.2f}C'.format(tempDest))
        except:
            #invalid input entered
            self._view.setOutput('Invalid input!')


#initializing GUI, binding view and model
c=Controller()

#output

tk Input Temperature: 123 CelsiusFahrenheit Source: Output Temperature: 253.40F Convert X

Add a comment
Know the answer?
Add Answer to:
Write a Python application that allows the user to convert between temperatures in Fahrenheit and temperatures...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (in python) Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user s......

    (in python) Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user s... Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user should be able to enter a Celsius temperature, click a button, and then see the equivalent Fahrenheit temperature. Use the following formula to make the conversion ( F=9/5 C + 32) where F is the Fahrenheit temperature and C is the Celsius temperature

  • IN JAVA…Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user should be...

    IN JAVA…Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user should be able to enter a Celsius temperature, click a button, and then see the equiva-lent Fahrenheit temperature. Use the following formula to make the conversion: F = (9/5)C + 32 F is the Fahrenheit temperature and C is the Celsius temperature. Instead of only converting from Celsius to Fahrenheit, also convert from Fahrenheit to Celsius depending on the user's choice. Some hints: Use JTextField and...

  • In Python, write a program that converts Celsius temperatures to Fahrenheit temperatures. The formula is as...

    In Python, write a program that converts Celsius temperatures to Fahrenheit temperatures. The formula is as follows: F=(9/5)C+32 The program should ask the user to enter a temperature in Celsius, then display the temperature converted to Fahrenheit.

  • in Python, prompt the user to enter the temperature to convert to Celsius. Hint, you will...

    in Python, prompt the user to enter the temperature to convert to Celsius. Hint, you will need input() and data type conversion to convert the value that input function returns to integer or float. See the last slide of chapter 2, it includes a sample program of the floor conversion. Sample program: Enter temperature in Celsius: >>> 64 64 in Fahrenheit is 17.7 in Celsius

  • Programming Exercise 8.3 | Instructions Write a GUI-based program that allows the user to convert temperature...

    Programming Exercise 8.3 | Instructions 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. breezypythongui.py temperatureconvert... + 1 2 File: temperatureconverter.py 3 Project 8.3 4 Temperature conversion between Fahrenheit and Celsius. 5 Illustrates the use of numeric data fields. 6 • These components should be arranged in a grid where the labels occupy the first row and the corresponding fields...

  • FOR PYTHON Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice...

    FOR PYTHON Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions. Both of these functions should be defined in a custom module named temps. Custom functionc_to_f should be a void function defined to take a Celsius temperature as a parameter. It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places. Custom function f_to_c should be a...

  • write in python idle Write full program that asks the user to enter his/her name then...

    write in python idle Write full program that asks the user to enter his/her name then repeatedly ask to enter the temperature in Fahrenheit and convert it to Celsius, the program should then prompt the user if he/she wants to continue or exit the program. The formula for conversion is: °C = (°F - 32) x 5/9 The program should use a function for the conversion. An Example of a sample run should appear on the screen like the text...

  • Write python program using IDLE Write a full program that asks the user to enter his/her...

    Write python program using IDLE Write a full program that asks the user to enter his/her name then repeatedly ask to enter the temperature in Fahrenheit and convert it to Celsius, the program should then prompt the user if he/she wants to continue or exit the program. The formula for the conversion is: °C = (°F - 32) x 5/9 The program should use a function for the conversion. An Example of a sample run should appear on the screen...

  • Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The...

    Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions. Both of these functions should be defined in a custom module named temps. Custom function c_to_f should be a void function defined to take a Celsius temperature as a parameter. It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places. Custom function f_to_c should be a value-returning...

  • i need a python code For an exact conversion between "Fahrenheit temperature scale" and "Celsius temperature...

    i need a python code For an exact conversion between "Fahrenheit temperature scale" and "Celsius temperature scale", the following formulas can be applied. Here, F is the value in Fahrenheit and the value in Celsius: Temperature Conversions C = 5 9 (F - 32) F = (š xc) + 3 Write a Python program that uses the above formula to convert Celsius to Fahrenheit and vice versa. We ask user to input "Enter a degree: ", and then we get...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT