1

当我按下一个键时,如何在 Genie 中停止这个小部件?

// 编译 con valac --pkg gtk+-3.0 nombre_archivo.gs
使用 Gtk
在里面    
    Gtk.init(参考参数)
    var 测试 = 新的 TestVentana ()   
    test.show_all()    
    gtk.main()

类TestVentana:窗口

    微调器:Gtk.Spinner    

    在里面        
        标题 = “Ejemplo Gtk”       
        默认高度 = 300
        默认宽度 = 300
        边框宽度 = 50       
        window_position = WindowPosition.CENTER     
        destroy.connect(Gtk.main_quit)

        var spinner = new Gtk.Spinner ()        
        spinner.active = true       
        添加(微调器)

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

    def tecla(key : Gdk.EventKey):bool      
        //spinner.active = false ???
        //spinner.stop () ???
        返回真

编辑:感谢提供解决方案的 Al Thomas(这是范围问题):

// 编译 con valac --pkg gtk+-3.0 nombre_archivo.gs
使用 Gtk
在里面    
    Gtk.init(参考参数)
    var 测试 = 新的 TestVentana ()   
    test.show_all()    
    gtk.main()

类TestVentana:窗口

    微调器:Gtk.Spinner        

    在里面        
        标题 = “Ejemplo Gtk”       
        默认高度 = 300
        默认宽度 = 300
        边框宽度 = 50       
        window_position = WindowPosition.CENTER     
        destroy.connect(Gtk.main_quit)

        微调器 = 新 Gtk.Spinner ()        
        spinner.active = true       
        添加(微调器)

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

    def tecla(key : Gdk.EventKey):bool      
        spinner.active = false      
        返回真
4

1 回答 1

2

您还没有完全应用范围的概念。在您的构造函数中,该行:

var spinner = new Gtk.Spinner()

spinner在构造函数的范围内创建一个新变量。删除var关键字,它将起作用:

spinner = new Gtk.Spinner()

它现在将使用在类范围内声明的 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
于 2017-01-16T13:51:00.767 回答