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

Playing bowling with your friends can help you decide if it is indeed the hobby that you want to invest your time on today. Aside from that, it can help you get a better feel of the sport. More importantly, when you play with your friends, it would become a more fun activity, which you can look forward to each week.

Get My Free Ebook

Post a comment