Converting Fahrenheit to Celsius

The code for converting the other way (from Fahrenheit to Celsius) is very similar. The formula for that conversion is . ,c , cel = (fahr - 32) * 5.0 / 9

It goes in the event handler for the Fahrenheit to Celsius button:

def on_btnFtoC_mouseClick(self, event)

We need to get the Fahrenheit temperature from the spinner:

fahr = self.components.spinFahr.value

This value is already an integer, so we don't have to do any type conversion. Then we apply the formula: , , . c . , 0

Finally, we convert this to a string and put it in the Celsius text box: seif.components.tfcel.text = str(cei) The whole thing should look like listing 20.2.

Listing 20.2 Complete temperature-conversion program from PythonCard import model class MainWindow(model.Background):

def on_btnCtoF_mouseClick(self, event):

cel = float(self.components.tfCel.text) fahr = cel * 9.0 / 5 + 32

self.components.spinFahr.value = int(fahr)

def on_btnFtoC_mouseClick(self, event):

fahr = self.components.spinFahr.value cel = (fahr - 32) * 5.0 / 9 <- Line 12

self.components.tfCel.text = str(cel) <- Line 13

app = model.Application(MainWindow) app.MainLoop()

Save this program as You can run it and try out the GUI.

100 Bowling Tips

100 Bowling Tips


Get My Free Ebook

Post a comment