Search code examples
gtk3genie

Stop Gtk.Spinner?


how can I stop this widget in Genie when I press a key?

// compila con valac --pkg gtk+-3.0 nombre_archivo.gs
uses Gtk
init    
    Gtk.init (ref args) 
    var test = new TestVentana ()   
    test.show_all ()    
    Gtk.main ()

class TestVentana: Window

    spinner: Gtk.Spinner    

    init        
        title = "Ejemplo Gtk"       
        default_height = 300
        default_width = 300
        border_width = 50       
        window_position = WindowPosition.CENTER     
        destroy.connect(Gtk.main_quit)

        var spinner = new Gtk.Spinner ()        
        spinner.active = true       
        add (spinner)

        //key_press_event += tecla // OBSOLETO
        key_press_event.connect(tecla)  

    def tecla(key : Gdk.EventKey):bool      
        //spinner.active = false   ???
        //spinner.stop ()          ???
        return true

EDIT: Thanks to Al Thomas who provided the solution (it was a problem of scope):

// compila con valac --pkg gtk+-3.0 nombre_archivo.gs
uses Gtk
init    
    Gtk.init (ref args) 
    var test = new TestVentana ()   
    test.show_all ()    
    Gtk.main ()

class TestVentana: Window

    spinner: Gtk.Spinner        

    init        
        title = "Ejemplo Gtk"       
        default_height = 300
        default_width = 300
        border_width = 50       
        window_position = WindowPosition.CENTER     
        destroy.connect(Gtk.main_quit)

        spinner = new Gtk.Spinner ()        
        spinner.active = true       
        add (spinner)

        // key_press_event += tecla // OBSOLETO
        key_press_event.connect(tecla)  

    def tecla(key : Gdk.EventKey):bool      
        spinner.active = false      
        return true

Solution

  • You've not fully applied the concept of scope. In your constructor, the line:

    var spinner = new Gtk.Spinner()

    creates a new variable, spinner, in the scope of your constructor. Remove the var keyword and it will work:

    spinner = new Gtk.Spinner()

    It will now use the spinner variable declared in the scope of the class and so it will be available in your tecla class method.

    I've also added the underscore to make the variable private, so it is only visible in the scope of the class and not to any part of the program that instantiates the class.

    // compila con valac --pkg gtk+-3.0 nombre_archivo.gs
    [indent=4]
    uses Gtk
    
    init
        Gtk.init( ref args )
        var test = new TestVentana()
        test.show_all()
        Gtk.main()
    
    class TestVentana:Window
    
        _spinner: Gtk.Spinner
    
        construct()
            title = "Ejemplo Gtk"
            default_height = 300
            default_width = 300
            border_width = 50
            window_position = WindowPosition.CENTER
            destroy.connect( Gtk.main_quit )
    
            _spinner = new Gtk.Spinner()
            _spinner.active = true
            add( _spinner )
    
            key_press_event.connect( tecla )
    
        def tecla( key:Gdk.EventKey ):bool
            _spinner.active = false
            return true