0

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

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

这是到目前为止的代码:

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()

谁能给我一点关于如何解决这个问题的指导?

4

1 回答 1

2

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

[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()

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

对于精灵代码本身,有几点:

  • 当您声明变量标识符及其类型时,这是变量的新声明。通常 Vala 编译器会警告你,但当在类的方法以及整个类的作用域中声明了同名变量时,它不会。所以_spin:SpinButton = new Gtk.SpinButton(spinAdjustment, 1.0,1)应该只是_spin = new Gtk.SpinButton(spinAdjustment, 1.0,1)因为你已经声明了_spin. 这同样适用于_scale
  • 您使用了 的get_value_pos方法,Gtk.Scale这会导致类型不匹配错误:Equality operation: 'double' and 'Gtk.PositionType' are incompatibleGtk.Scale的 Vala 文档说:“要使用它,除了 GtkScale 本身的方法之外,您可能还需要研究其基类 Range 上的方法。” 下面Gtk.Range有一个get_value方法,它返回一个double,您需要匹配get_value来自 的相同类型SpinButton。这就是继承的概念,它已经被用于Window
于 2016-02-21T18:44:28.440 回答