[Tutor] help with GUI

Christopher Spears cspears2002 at yahoo.com
Thu Jun 22 05:29:54 CEST 2006


I am trying to write a GUI that consists of a
scrollbar and two buttons.  One button is called
Convert, and the other is called Quit.  The vertical
scrollbar represents degrees Fahrenheit starting from
212 at the top and ending at 32 at the bottom.  I want
to be able to pick degrees Fahrenheit with the the
scrollbar.  Then I would click Convert, and the script
should print the conversion into degrees Celsius on
the screen.

My first problem is that the knob on the scrollbar
doesn't move when I drag it.  I would like my GUI to
by wider as well.  Finally, I need to figure out how
to get the number selected from the scrollbar to the
function that does the conversion.

Here is what I have written so far:

#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk

def convert_to_celsius(self,widget,data=None):
	degC = (degF - 32)/1.8
	print "Degrees Celsius: .2%f" % degC
	
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)
	
class Conversion_GUI:
	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")
		
		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, 212.0, 32.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)
		
		quit_button = gtk.Button("Quit")
		quit_button.connect("clicked", lambda
w:gtk.main_quit())
		convert_button = gtk.Button("Convert")
		#convert_button.connect("clicked",
convert_to_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