[pygtk] passing arguments to functions

Christopher Spears cspears2002 at yahoo.com
Fri Jul 21 08:44:39 WST 2006


I am trying to create a GUI in glade that will allow
me to convert temperatures.  Here is the code for the
backend:

#!/usr/bin/env python

import sys
try:
 	import pygtk
  	pygtk.require("2.0")
except:
  	pass
try:
	import gtk
  	import gtk.glade
except:
	sys.exit(1)

class convertTempGUI:
	"""A GUI to convert temperatures"""
		
	def on_mainWindow_delete(self, widget, event):
		#print "I made it vanish!"
		gtk.main_quit()
		
	def convert_to_celsius(self, temp):
		return (temp - 32)/1.8
		
	def convert_to_fahrenheit(self, temp):
		return temp * 1.8 + 32
		
	def print_temp(self, widget, entry, rb1):
		temp = float(entry.get_text())
		if rb1.get_active():
			val = '%.2f C' % self.convert_to_celsius(temp)
		else:
			val = '%.2f F' % self.convert_to_fahrenheit(temp)
		print "Degrees temp: ", val
		

	def __init__(self):
		
		#Set the Glade file
		self.gladefile = "convertempgui.glade"
	        self.wTree = gtk.glade.XML(self.gladefile) 
		
		#Create our dictionay and connect it
		dic = { "on_mainWindow_delete_event" :
self.on_mainWindow_delete,
			"on_entry1_activate" : (self.print_temp, entry1,
rb1)  }
		self.wTree.signal_autoconnect(dic)

if __name__ == "__main__":
	cTG = convertTempGUI()
	main = gtk.main()

However when I run the code, I get the following error
message:

io at io-station-1 ./converTempGUI 111> python
convertTemp01.py
Traceback (most recent call last):
  File "convertTemp01.py", line 49, in ?
    cTG = convertTempGUI()
  File "convertTemp01.py", line 44, in __init__
    dic = { "on_mainWindow_delete_event" :
self.on_mainWindow_delete,
NameError: global name 'entry1' is not defined

Obviously, entry1 needs to be defined in order for the
error message to go away.  However, I am confused
about what to do next.  Originally, I hard coded the
GUI with this script:

#!/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, temp):
		return (temp - 32)/1.8
		
	def convert_to_fahrenheit(self, temp):
		return temp * 1.8 + 32
		
	def print_temp(self, widget, entry, rb1):
		temp = float(entry.get_text())
		if rb1.get_active():
			val = '%.2f C' % self.convert_to_celsius(temp)
		else:
			val = '%.2f F' % self.convert_to_fahrenheit(temp)
		print "Degrees temp: ", val
		
		
	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 Temperatures")
		self.window.set_default_size(200,100)
		
		entry = gtk.Entry()
		entry.set_max_length(10)
		entry.set_text("0")
		label = gtk.Label("Enter Temp: ")
		label.set_justify(gtk.JUSTIFY_LEFT)
		entry.show()
		label.show()
		
		rb1 = gtk.RadioButton(None, "Fahrenheit")
		rb2 = gtk.RadioButton(rb1,"Celsius")
		rb1.show()
		rb2.show()
		
		box1 = gtk.VBox(False, 0)
		box2 = gtk.HBox(False, 0)
		box2.pack_start(rb1, False, False, 0)
		box2.pack_start(rb2, 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)
		
		entry.connect("activate", self.print_temp, entry,
rb1)
		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,
entry, rb1)
		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()


Is there a way for me to somehow extract entry1 from
the glade file?  I would normally check the docs, but
the PyGTK site seems to be down.


More information about the pygtk mailing list