Darren Enns wrote:
Hello all!

I am slowly learning the Python language, and now I am moving into developing GUI apps using PyGTK. I am bumbling/stumbling around with 'spinbutton'. I know how to make the spinbutton appear, but I don't know how to *properly* return the final value for a spinbutton back to my mainline code. Can someone provide a very simple example of:

1) A mainline creating a window and a button
2) When pushed, the button should call a module which opens another (temporary) window to display a 'spinbutton' 3) The final value of the spinbutton when the 2nd window is closed should return (somehow) the value back to the calling mainline

Here is the minimum that I am currently using to accomplish this (please don't laugh!):

#!/usr/bin/env python2.5
import gtk

def get_value(widget,spin,value):
   value = "%d" % spin.get_value_as_int()
   print "value=",value
   return

def get_spin(widget):
   window = gtk.Window()
   adjustment = gtk.Adjustment(0, -90, 90, 1, 1, 1)
   spinbutton = gtk.SpinButton(adjustment,0,0)
   value = 0
   spinbutton.connect("changed",get_value,spinbutton,value)
   print "the final value is: ",value
   spinbutton.show()
   window.add(spinbutton)
   window.show()
   return

def main():
   window = gtk.Window(gtk.WINDOW_TOPLEVEL)
   window.connect("destroy", gtk.main_quit)
   window.show_all()
   table = gtk.Table(2,1,False)
   button = gtk.Button("Get Spin Value")
   button.connect("clicked",get_spin)
   table.attach(button,0,1,0,1)
   table.show()
   button.show()
   window.add(table)
   window.show_all()
   gtk.main()
   return

if __name__ == '__main__': main()

Dare
_______________________________________________
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/


Hello Dare,

Here is one way, that works. The main problem with the way you had the code structured, is that the 'value' variable in get_spin() and the 'value' variable in main() where two different things.

Separating the two windows into separate classes, makes it is easy to make sure that there is only one 'value' variable. And the code becomes easier to understand. As in the main window class it is obvious (see print_value()) that we are refereing to the variable set by the spin_window() class.

Hope this helps.

Neil.
#!/usr/bin/env python2.5
import gtk

value = ""

class spin_window :   
	value = 0
	def get_value(self,widget):
	   self.value = "%d" % widget.get_value_as_int()
	   return

	def __init__(self):
	   print "the final value is: ",value
	   window = gtk.Window()
	   adjustment = gtk.Adjustment(0, -90, 90, 1, 1, 1)
	   spinbutton = gtk.SpinButton(adjustment,0,0)
	   spinbutton.connect("changed",self.get_value)
	   spinbutton.show()
	   window.add(spinbutton)
	   window.show()
	   return

class main_window:
	def print_value(self,widget):
	   print "window value=",self.window.value
	   return

	def get_spin(self,widget) :
		self.window = spin_window()

	def __init__(self):
	   window = gtk.Window(gtk.WINDOW_TOPLEVEL)
	   window.connect("destroy", gtk.main_quit)
	   table = gtk.Table(2,1,False)

	   button = gtk.Button("Show window")
	   button.connect("clicked",self.get_spin)
	   table.attach(button,0,1,0,1)

	   button2 = gtk.Button("Print value")
	   button2.connect("clicked",self.print_value)
	   table.attach(button2,1,2,0,1)

	   window.add(table)
	   window.show_all()

	def main(self):
	   gtk.main()
	   return

if __name__ == '__main__': 
	main = main_window()
	main.main()
_______________________________________________
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to