[Tutor] critique my script!

Christopher Spears cspears2002 at yahoo.com
Sat Jun 24 00:34:48 CEST 2006


I wrote a script that creates a gui using pygtk.  The
gui consists of a vertical scrollbar and two buttons. 
The user picks a number (degrees Fahrenheit) with the
scrollbar and then clicks the convert button.  A
functions converts the number to its equivalent in
degrees Celsius and prints a response to the screen. 
The quit buttons ends the script.  Tell me what you
think!

#!/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()



More information about the Tutor mailing list