如何在 Rust 中获取 gtk::RadioButton 的选定状态?

How do I get the selected state of a gtk::RadioButton in Rust?

在Python的Gtk模块中,您可以使用Gtk.RadioButton.get_active()来获取单选按钮是否被选中。但是,我在 Rust 的等效方法 gtk::RadioButton 中找不到任何这样的方法。当我尝试使用 RadioButton.active() 时,我收到一条错误消息,指出 gtk::RadioButton 不满足特征界限。我尝试在文档中搜索 get(activecheckedstateselected,但无济于事。这应该是不言自明的,所以我认为我不需要提供代码示例。有谁知道如何在 Rust 中获取选定的 RadioButton(最好不使用回调)?

如果你看 one-and-only official Gtk3 documentation, you'll see that RadioButton does not define an active property. Instead it is inherited from its base class ToggleButton.

AIUI,在gtk-rs中,继承的函数和属性实际上是关联接口的一部分,而不是类型本身。所以你的 属性 是 ToggleButtonExt::is_active().

理论上,您的 RadioButton 值实现了 IsA<ToggleButton>,因此它也实现了 ToggleButtonExt,并且 is_active() 方法应该随时可用。

如果它说特征不在范围内,也许你忘记了 use gtk::prelude::*;?