我的目标是使刻度中的滑块与旋转中的值同步,反之亦然。我已经尝试了一些替代方案,但我没有找到一种方法来获得比例所在的值,以便我可以设置新的旋转位置。
小练习有一个小的复选按钮,可以同步两个小部件。我试图通过切换功能进行同步,但我现在卡住了。
以下是目前的代码:
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()
任何人都可以给我一个关于如何解决这个问题的指针吗?
答案 0 :(得分: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()
您的示例很好,显然当您运行它时,您将了解如何改进用户交互。
对于Genie代码本身有几点:
_spin:SpinButton = new Gtk.SpinButton(spinAdjustment, 1.0,1)
应该是_spin = new Gtk.SpinButton(spinAdjustment, 1.0,1)
,因为您已经宣布_spin
。这同样适用于_scale
。get_value_pos
的{{1}}方法,这会导致类型不匹配错误:Gtk.Scale
。 Gtk.Scale的Vala文档说"要使用它,除了GtkScale本身的方法之外,您可能还想研究其基类Range的方法。"在Equality operation: 'double' and 'Gtk.PositionType' are incompatible
下,有get_value方法返回Gtk.Range
,与double
中的get_value
匹配所需的类型相同。这是继承的概念,已经用于SpinButton