[pygtk] NEWBIE QUESTION: Simple spinbutton example?

Neil Dugan pygtk at butterflystitches.com.au
Sun Mar 23 11:30:42 WST 2008


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 =3D "%d" % spin.get_value_as_int()
>    print "value=3D",value
>    return
> =

> def get_spin(widget):
>    window =3D gtk.Window()
>    adjustment =3D gtk.Adjustment(0, -90, 90, 1, 1, 1)
>    spinbutton =3D gtk.SpinButton(adjustment,0,0)
>    value =3D 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 =3D gtk.Window(gtk.WINDOW_TOPLEVEL)
>    window.connect("destroy", gtk.main_quit)
>    window.show_all()
>    table =3D gtk.Table(2,1,False)
>    button =3D 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__ =3D=3D '__main__': main()
> =

> Dare
> _______________________________________________
> pygtk mailing list   pygtk at 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: spin.py
Type: text/x-python
Size: 1192 bytes
Desc: not available
Url : http://www.daa.com.au/pipermail/pygtk/attachments/20080323/12e8af7a/s=
pin.py


More information about the pygtk mailing list