[pygtk] placing info from scrollbar into variable

Christopher Spears cspears2002 at yahoo.com
Fri Jun 23 13:30:44 WST 2006


I am trying to create a gui that consists of a
vertical scrollbar and two buttons.  I want to be able
to pick a number representing degrees Fahrenheit using
the scrollbar.  When I click the Convert button after
picking a number, the gui would convert the number
into degrees Celsius and print the answer to the
screen.  Here is what I have written so far:

#!/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):
		#print "Data: ", adj.value
		degC = (adj.value - 32)/1.8
		return degC
		
	
	def print_celsius(self, widget, data=None):
		#degC = self.convert_to_celsius(adj1)
		print data
		
		
	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, data)
		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()

Basically, the scrollbar feeds information into
convert_to_celsius, which does the conversion.  What I
want to do is somehow store the information generated
in convert_to_celsius in a variable and send it to
print_celsius to print out the answer after the
Convert button is pressed.

I've looked at the tutorials and the docs and couldn't
find anything, so I thought someone on this mailing
list could give me advice.


More information about the pygtk mailing list