2017-01-16 42 views
1

當我按下某個鍵時,如何在Genie中停止此小部件?停止Gtk.Spinner?

 
// 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 

編輯:感謝阿爾·托馬斯誰提供的解決方案(這是範圍問題):

 
// 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 

回答

2

你已經不能完全適用範圍的概念。 在你的構造,該行:

var spinner = new Gtk.Spinner()

創建一個新的變量,spinner,在構造函數的範圍。取出var關鍵字,它會工作:

spinner = new Gtk.Spinner()

現在將使用在類的範圍內聲明的變量微調,所以它會在你tecla類方法可用。

我也添加了下劃線來使變量私人,所以它是 只在類的範圍內可見,而不是實例化類的程序 的任何部分。

// 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 
+0

謝謝,當我編輯我的問題時,你回答了。 – Webierta