[pygtk] Where do I go from here?
Christopher Spears
cspears2002 at yahoo.com
Fri Jul 7 11:11:20 WST 2006
I am trying to create a GUI that convert temperatures
into degrees Celsius or Fahrenheit. I want the user
to enter a temperature in the entry and determine if
the temperature is in Celsius or Fahrenheit by
clicking on one of the check buttons. The conversion
is carried out by pressing the convert button.
For example, I want to convert 212 degrees Fahrenheit
into equivalent in Celsius. I would enter 212 in the
entry and click on Fahrenheit. After clicking the
convert button, "Degrees Celsius: 100.0" would be
written to the screen.
Here is what I have written so far:
#!/usr/bin/python
import pygtk
pygtk.require('2.0')
import gtk
class Conversion_GUI:
def get_text(self, widget, entry):
entry_text = entry.get_text()
temp = float(entry_text)
return temp
def convert_to_celsius(self, widget, data=None):
temp = float(data)
self.degC = (temp - 32)/1.8
return self.degC
def convert_to_farhenheit(self, temp):
self.degF = temp * 1.8 + 32
return self.degF
def print_temp(self, widget):
print "Degrees temp: %.2f" % self.temp
def __init__(self):
self.temp = 0
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("destroy", lambda w:
gtk.main_quit())
self.window.set_title("Convert to Celsius")
self.window.set_default_size(200,100)
entry = gtk.Entry()
entry.set_max_length(10)
entry.set_text("0")
entry.connect("changed", self.get_text, entry)
label = gtk.Label("Enter Temp: ")
label.set_justify(gtk.JUSTIFY_LEFT)
entry.show()
label.show()
checkF = gtk.CheckButton("Fahrenheit")
checkC = gtk.CheckButton("Celsius")
checkF.connect("toggled", self.convert_to_celsius,
self.get_text(widget, entry))
checkF.show()
checkC.show()
box1 = gtk.VBox(False, 0)
box2 = gtk.HBox(False, 0)
box2.pack_start(checkF, False, False, 0)
box2.pack_start(checkC, False, False, 0)
box1.pack_start(box2, False, False, 0)
box3 = gtk.HBox(False, 0)
self.window.add(box1)
box3.pack_start(label, False, False, 0)
box3.pack_start(entry, True, True, 0)
box1.pack_start(box3, False, False, 0)
quit_button = gtk.Button("Quit")
quit_button.connect("clicked", lambda
w:gtk.main_quit())
convert_button = gtk.Button("Convert")
convert_button.connect("clicked", self.print_temp)
convert_button.show()
quit_button.show()
box3.pack_start(convert_button, False, False, 0)
box3.pack_start(quit_button, False, True, 0)
box3.show()
box2.show()
box1.show()
self.window.show()
def main(self):
gtk.main()
return 0
if __name__ == '__main__':
convert = Conversion_GUI()
convert.main()
I'm not sure where to go from here. Am I on the right
track? How do I get the temperature to the right
equation? I've read the tutorial and the FAQ and
looked at the reference and played with the program,
but I'm still stuck. Any hints?
-Chris
More information about the pygtk
mailing list