使 Gtk.Scale 与 Gtk.Spin 具有相同的值

Make a Gtk.Scale have the same value as a Gtk.Spin

我的 objective 是让刻度中的滑块与旋转中的值同步,反之亦然。我已经尝试了一些替代方法,但我没有找到一种方法来获取比例所在的值,以便我可以设置新的旋转位置。

这个小练习有一个小复选按钮,可以同步两个小部件。我正在尝试通过切换功能进行此同步,但我现在卡住了。

目前的代码如下:

uses
    Gtk

class TestWindow:Window
    _spin:Gtk.SpinButton
    _scale:Gtk.Scale
    construct ()

        //General aspects of the window
        title = "Spin and scale"
        window_position = WindowPosition.CENTER
        destroy.connect( Gtk.main_quit )

        //create the spin button
        spinAdjustment:Gtk.Adjustment = new Gtk.Adjustment(50, 0, 100, 1, 5, 0)
        scaleAdjustment:Gtk.Adjustment = new Gtk.Adjustment(50, 0, 100, 1, 5, 0)
        _spin:SpinButton = new Gtk.SpinButton(spinAdjustment, 1.0,1)

        //create the horizontal scale
        _scale:Scale = new Gtk.Scale(Gtk.Orientation.HORIZONTAL, scaleAdjustment);

        //create the check button
        var check =  new Gtk.CheckButton.with_label ("Sync both scales!")
        check.toggled.connect(toggled)

        // organize it in a box
        var box = new Box( Orientation.VERTICAL, 0 )
        box.pack_start(_spin, true, true, 0 )
        box.pack_start(_scale, true, true, 0)
        box.pack_start(check, true, true, 0)
        add( box )

    def toggled ()
        message(_spin.get_value().to_string())
        //if _scale.get_value_pos() !=  _spin.get_value()
        //  _scale.set_value_pos(_spin.get_value())



init
    Gtk.init( ref args )
    var test = new TestWindow()
    test.show_all()
    Gtk.main()

任何人都可以告诉我如何解决这个问题吗?

这是一个基于您的代码的工作示例:

[indent=4]
uses
    Gtk

class TestWindow:Window

    _spin:SpinButton
    _scale:Scale

    construct ()
        //General aspects of the window
        title = "Spin and scale"
        window_position = WindowPosition.CENTER
        destroy.connect( main_quit )

        //create the spin button
        spinAdjustment:Adjustment = new Adjustment( 50, 0, 100, 1, 5, 0 )
        scaleAdjustment:Adjustment = new Adjustment( 50, 0, 100, 1, 5, 0 )
        _spin = new SpinButton( spinAdjustment, 1.0, 1 )

        //create the horizontal scale
        _scale = new Scale( Orientation.HORIZONTAL, scaleAdjustment );

        //create the check button
        var check =  new CheckButton.with_label ( "Sync both scales!" )
        check.toggled.connect( toggled )

        // organize it in a box
        var box = new Box( Orientation.VERTICAL, 0 )
        box.pack_start( _spin, true, true, 0 )
        box.pack_start( _scale, true, true, 0 )
        box.pack_start( check, true, true, 0 )
        add( box )

    def toggled ()
        message(_spin.get_value().to_string())
        if _scale.get_value() !=  _spin.get_value()
            _scale.set_value(_spin.get_value())

init
    Gtk.init( ref args )
    var test = new TestWindow()
    test.show_all()
    Gtk.main()

您的示例很好,显然当您 运行 它时,您将了解如何改进用户交互。

对于 Genie 代码本身,有几点:

  • 当您声明一个变量标识符及其类型时,这是一个新的变量声明。 Vala 编译器通常会警告您,但当在 class 的方法以及整个 class 的范围内声明具有相同名称的变量时,它不会警告您。所以 _spin:SpinButton = new Gtk.SpinButton(spinAdjustment, 1.0,1) 应该只是 _spin = new Gtk.SpinButton(spinAdjustment, 1.0,1) 因为你已经声明了 _spin。同样适用于 _scale.
  • 您使用了 Gtk.Scaleget_value_pos 方法,这导致类型不匹配错误:Equality operation: 'double' and 'Gtk.PositionType' are incompatibleGtk.Scale says "To use it, you’ll probably want to investigate the methods on its base class, Range, in addition to the methods for GtkScale itself." Under Gtk.Range there is a get_value 方法的 Vala 文档中说 returns 一个 double,相同的类型你需要从 SpinButton 匹配 get_value。这就是继承的概念,已经被用于Window