[pygtk] help with script
Christopher Spears
cspears2002 at yahoo.com
Tue Jun 27 01:10:04 WST 2006
Here is a script that I wrote:
#!/usr/bin/python
import pygtk
pygtk.require('2.0')
import gtk
def scale_set_default_values(scale):
scale.set_update_policy(gtk.UPDATE_CONTINUOUS)
scale.set_digits(1)
scale.set_value_pos(gtk.POS_LEFT)
scale.set_draw_value(True)
scale.set_sensitive(True)
class Conversion_GUI:
def convert_to_celsius(self, adj):
self.degC = (adj.value - 32)/1.8
return self.degC
def print_celsius(self, widget):
print "Degrees Celsius: %.2f" % self.degC
def __init__(self):
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,240)
box1 = gtk.VBox(False, 0)
self.window.add(box1)
box2 = gtk.HBox(False, 10)
box2.set_border_width(10)
box1.pack_end(box2, True, True, 0)
box3 = gtk.HBox(False, 10)
box3.set_border_width(10)
box1.pack_end(box3, True, True, 0)
adj1 = gtk.Adjustment(32.0, 32.0, 213.0, 0.1, 1.0,
1.0)
self.vscale = gtk.VScale(adj1)
self.vscale.set_size_request(20, 300)
scale_set_default_values(self.vscale)
box1.pack_start(self.vscale, True, True, 0)
adj1.connect("value_changed",self.convert_to_celsius)
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_celsius)
box3.pack_start(convert_button, True, True, 0)
box2.pack_start(quit_button, True, True, 0)
self.vscale.show()
convert_button.show()
quit_button.show()
box3.show()
box2.show()
box1.show()
self.window.show()
def main(self):
gtk.main()
return 0
if __name__ == '__main__':
convert = Conversion_GUI()
convert.main()
The script creates a gui with a vertical scrollbar and
two buttons (Convert and Quit). Using the scrollbar,
I can select a value in degrees Fahrenheit. Then I
can convert this and print it to the screen using the
Convert button. However, when I first start the
program and press the convert button without moving
the scrollbar, I get an error message:
Traceback (most recent call last):
File "C:\Documents and Settings\Christopher Spears\My
Documents\programming\PythonScripts\convertCelsius.py",
line 22, in print_celsius
print "Degrees Celsius: %.2f" % self.degC
AttributeError: Conversion_GUI instance has no
attribute 'degC'
My guess is since the scrollbar has not been moved yet
degC has not been computed, which causes the error
when Convert is pressed. Can someone advise me on a
solution? Sorry if this is a basic question. I'm new
to pygtk.
More information about the pygtk
mailing list