GTK&x2B;如何查找已选择的单选按钮?

GTK&x2B;如何查找已选择的单选按钮?,gtk,radio-button,Gtk,Radio Button,这里有教程 显示了如何设置单选按钮,但忽略了告诉您如何使用它们 然后如何找到已选择的单选按钮 我的解决方案: 使用以下命令初始化单选按钮: rbutton1 = gtk_radio_button_new_with_label(NULL, "button1"); gtk_box_pack_start(GTK_BOX(rbutton_box), rbutton1, TRUE, TRUE, 0); rbuttonGroup = gtk_radio_button_get_group(GTK_RADIO

这里有教程 显示了如何设置单选按钮,但忽略了告诉您如何使用它们

然后如何找到已选择的单选按钮

我的解决方案:

使用以下命令初始化单选按钮:

rbutton1 = gtk_radio_button_new_with_label(NULL, "button1");
gtk_box_pack_start(GTK_BOX(rbutton_box), rbutton1, TRUE, TRUE, 0);

rbuttonGroup = gtk_radio_button_get_group(GTK_RADIO_BUTTON(rbutton1)); /*not sure what I'd use this line for currently though*/
rbutton2 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(rbutton1), "button 2"); 
gtk_box_pack_start(GTK_BOX(rbutton_box), rbutton2, TRUE, TRUE, 0);

rbutton3 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(rbutton1), "button 3"); 
gtk_box_pack_start(GTK_BOX(rbutton_box), rbutton3, TRUE, TRUE, 0);
并更新一个变量,告诉您使用此方法选择了哪个单选按钮:

        void checkRadioButtons()
{
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(rbutton1))==TRUE) selectedRadioButton =1;
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(rbutton2))==TRUE) selectedRadioButton =2;
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(rbutton3))==TRUE) selectedRadioButton =3;
}

您可以改为连接到信号。在关联的回调中,您将能够更新变量。至于调用
gtk\u radio\u button\u get\u group
,只有在调用
gtk\u radio\u button\u new\u with_label
而不是
gtk\u radio\u button\u new\u with_label\u widget
时才需要它。让我们创建一系列按钮:

for severity in levels:
    radio = gtk.RadioButton(group=radioButtons, label=severity)
    if severity == actualLevel:
        radio.set_active(True)
    hBox.pack_start(radio, True, True, 3)
    radio.connect('toggled', self.radioButtonSelected, severity)
所有按钮都连接到同一个处理程序:

def radioButtonSelected(self, button, currentSeverity):
    # proceed with the task
    # as you can see, button is passed by as argument by the event handler
    # and you can, for example, get the button label :
    labelReadFromButton = button.getLabel()

使用lambda表达式如果您不想干扰恼人的方法,仍然必须使用connect,但它更易于阅读:

Enum RadioValues { A, B, C, none };

RadioValues values = RadioValues.none; // only needed if you dont have an initially selected radio button

MyConstructor()
{
   Build();
   // asumming you have 3 radio buttons: radioA, radioB, radioC:
   radioA.Toggled += (sender,e) => values = RadioValues.A;
   radioB.Toggled += (sender,e) => values = RadioValues.B;
   radioC.Toggled += (sender,e) => values = RadioValues.C;

}
也就是说,没有方法可以处理,你也不必局限于此,如果你需要更多的灵活性,你也可以使用匿名函数——当然,下一步就是使用方法。不幸的是,他们没有提供一个简单的.Checked属性,我的下一个建议是重写单选按钮本身,并在切换状态更改时链接一个Checked属性,模拟其他框架,如MFC、Qt和Winforms。。。等等

PS:为了简单起见,我省略了样板代码,这可能会使答案更混乱,您可能只想了解事实,而不是演示我是否可以正确调用构造函数:)

我就是这样做的

GtkRadioButton * radio_button;
GtkRadioButton * radio_button1;
GtkRadioButton * radio_button2;
...
GSList * tmp_list = gtk_radio_button_get_group (radio_button);//Get the group of them.
GtkToggleButton *tmp_button = NULL;//Create a temp toggle button.

while (tmp_list)//As long as we didn't reach the end of the group.
{
  tmp_button = tmp_list->data;//Get one of the buttons in the group.
  tmp_list = tmp_list->next;//Next time we're going to check this one.

  if (gtk_toggle_button_get_active(tmp_button))//Is this the one active?
    break;//Yes.

  tmp_button = NULL;//We've enumerated all of them, and none of them is active.
}
//Here. tmp_button holds the active one. NULL if none of them is active.
见讨论。
我不知道他们是否会将此函数添加到其中(似乎不会)。

谷歌将我带到这里进行python/pygtk/pygtk3搜索,因此我希望发布pygtk解决方案可以:

def _resolve_radio(self, master_radio):
    active = next((
        radio for radio in
        master_radio.get_group()
        if radio.get_active()
    ))
    return active

这将使用生成器返回第一个(仅应为处于活动状态的)处于活动状态的收音机盒。

我建议这样做:

void radio_button_selected (GtkWidget *widget, gpointer data) 
{
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))
    {
        GSLIST *group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget));
        g_print ("Index = %i%\n", g_slist_index (group, widget));
    }
}

我对
GTKmm
的解决方案非常简单

您只需调用以下函数:

my_radio_button.get_active(); \n

如果未激活,则返回0;如果激活,则返回1。

这是一个使用单选按钮的演示代码,您可以在其中找到如何查找选中的单选按钮:

#include <gtkmm/window.h>
#include <gtkmm/box.h>
#include <gtkmm/radiobutton.h>
#include <gtkmm/separator.h>
#include <gtkmm/application.h>
#include <iostream>

class ButtonWindow : public Gtk::Window
{
private:
   //Child widgets:
   Gtk::Box m_Box_Top, m_Box1, m_Box2;
   Gtk::RadioButton m_RadioButton1, m_RadioButton2, m_RadioButton3;
   Gtk::Separator m_Separator;
   Gtk::Button m_Button_Close;
   Gtk::RadioButton *m_SelectedButton{nullptr};

public:
   ButtonWindow()
      : m_Box_Top(Gtk::ORIENTATION_VERTICAL),
        m_Box1(Gtk::ORIENTATION_VERTICAL, 15),
        m_Box2(Gtk::ORIENTATION_VERTICAL, 0),
        m_RadioButton1("button 1"),
        m_RadioButton2("button 2"),
        m_RadioButton3("button 3"),
        m_Button_Close("close")
   {
      // Set title and border of the window
      set_title("radio buttons");
      set_border_width(0);

      // Put radio buttons 2 and 3 in the same group as 1:
      m_RadioButton2.join_group(m_RadioButton1);
      m_RadioButton3.join_group(m_RadioButton1);

      // Add outer box to the window (because the window
      // can only contain a single widget)
      add(m_Box_Top);

      //Put the inner boxes and the separator in the outer box:
      m_Box_Top.pack_start(m_Box1);
      m_Box_Top.pack_start(m_Separator);
      m_Box_Top.pack_start(m_Box2);

      // Set the inner boxes' borders
      m_Box1.set_border_width(20);
      m_Box2.set_border_width(10);

      // Put the radio buttons in Box1:
      m_Box1.pack_start(m_RadioButton1);
      m_Box1.pack_start(m_RadioButton2);
      m_Box1.pack_start(m_RadioButton3);

      // Put Close button in Box2:
      m_Box2.pack_start(m_Button_Close);

      // Connect the button signals:
#if 1 // Full C++11: (change this to #if 0 to use the traditional way)
      m_RadioButton1.signal_clicked().connect([&]{on_radio_button_clicked(m_RadioButton1);});
      m_RadioButton2.signal_clicked().connect([&]{on_radio_button_clicked(m_RadioButton2);});
      m_RadioButton3.signal_clicked().connect([&]{on_radio_button_clicked(m_RadioButton3);});

      m_Button_Close.signal_clicked().connect([&]{on_close_button_clicked();});
#else // Traditional:
  m_RadioButton1.signal_clicked() // Full sigc
     .connect(sigc::bind(sigc::mem_fun(*this, &ButtonWindow::on_radio_button_clicked),
                         sigc::ref(m_RadioButton1)));

  m_RadioButton2.signal_clicked() // sigc && C++98
     .connect(std::bind(sigc::mem_fun(*this, &ButtonWindow::on_radio_button_clicked),
                        std::ref(m_RadioButton2)));

  m_RadioButton3.signal_clicked() // Full C++98
     .connect(std::bind(&ButtonWindow::on_radio_button_clicked, this,
                        std::ref(m_RadioButton3))); 

      m_Button_Close.signal_clicked()
         .connect(sigc::mem_fun(*this, &ButtonWindow::on_close_button_clicked));
#endif

      // Set the second button active:
      m_RadioButton2.set_active();

      // Make the close button the default widget:
      m_Button_Close.set_can_default();
      m_Button_Close.grab_default();

      // Show all children of the window:
      show_all_children();
   }
  
protected:
   //Signal handlers:
   void on_radio_button_clicked(Gtk::RadioButton& button)
   {
      if(m_SelectedButton != &button && button.get_active())
      {
         m_SelectedButton = &button;
         std::cout << "Radio "<< m_SelectedButton->get_label() << " selected.\n";
      }
   }

   void on_close_button_clicked()
   {
      hide(); // Close the application
   }
};

int main(int argc, char *argv[])
{
   auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

   ButtonWindow button;

   //Shows the window and returns when it is closed.
   return app->run(button);
}
#包括
#包括
#包括
#包括
#包括
#包括
类按钮窗口:公共Gtk::窗口
{
私人:
//子窗口小部件:
Gtk::盒子m_盒子顶部,m_盒子1,m_盒子2;
Gtk::RadioButton m_RadioButton1、m_RadioButton2、m_RadioButton3;
Gtk::分离器m_分离器;
Gtk::按钮m_按钮关闭;
Gtk::RadioButton*m_SelectedButton{nullptr};
公众:
按钮窗口()
:m_箱_顶部(Gtk::方向_垂直),
m_-Box1(Gtk::垂直方向,15),
m_-Box2(Gtk::方向垂直,0),
m_RadioButton1(“按钮1”),
m_RadioButton2(“按钮2”),
m_RadioButton3(“按钮3”),
m_按钮_关闭(“关闭”)
{
//设置窗口的标题和边框
设置标题(“单选按钮”);
设置边框宽度(0);
//将单选按钮2和3与1放在同一组中:
m_RadioButton2.加入m_组(m_RadioButton1);
m_RadioButton3.加入m_组(m_RadioButton1);
//将外部框添加到窗口(因为窗口
//只能包含一个小部件)
添加(m_盒顶部);
//将内盒和分离器放入外盒:
m_盒顶部。打包启动(m_盒1);
m_盒顶部。包装启动(m_分离器);
m_盒顶部。打包启动(m_盒2);
//设置内部框的边框
m_Box1.设置边框宽度(20);
m_Box2.设置边框宽度(10);
//将单选按钮放入框1:
m_盒1.打包启动(m_RadioButton1);
m_盒1.打包启动(m_RadioButton2);
m_盒1.打包启动(m_RadioButton3);
//将关闭按钮置于框2中:
m_盒2.打包启动(m_按钮关闭);
//连接按钮信号:
#if 1//Full C++11:(将其更改为#if 0以使用传统方式)
m_RadioButton1.signal_clicked().connect([&]{on_RadioButton_clicked(m_RadioButton1);});
m_RadioButton2.signal_clicked().connect([&]{on_RadioButton_clicked(m_RadioButton2);});
m_RadioButton3.signal_clicked().connect([&]{on_RadioButton_clicked(m_RadioButton3);});
m_按钮_关闭。信号_单击()。连接([&]{on_关闭_按钮_单击();});
#else//传统:
m_RadioButton1.单击信号()//完整信号
.connect(sigc::bind(sigc::mem_-fun(*这个,&ButtonWindow::单击单选按钮),
sigc::ref(m_RadioButton1));
m_RadioButton2.单击信号()//sigc&&C++98
.connect(std::bind(sigc::mem_-fun(*这个,&ButtonWindow::点击单选按钮),
标准::参考(m_RadioButton2));
m_RadioButton3.signal_clicked()//完整的C++98
.connect(std::bind(&ButtonWindow::单击单选按钮时,
标准::参考(m_RadioButton3));
m_按钮_关闭。信号_点击()
.connect(sigc::mem_-fun(*此,&ButtonWindow::on_-close_-button_-clicked));
#恩迪夫
//将第二个按钮设置为激活状态:
m_RadioButton2.设置_活动();
//将关闭按钮设为默认窗口小部件:
m_按钮_关闭。设置_可以_默认值();
m_按钮_关闭。抓取默认值();
//显示窗口的所有子项:
显示所有子项();
}
受保护的:
//信号处理程序:
单击单选按钮时无效(Gtk::单选按钮和按钮)
{
如果(m_SelectedButton!=&button&&button.get_active())
{
m_SelectedButton=&button;

std::您能澄清一下这是如何回答所问问题的吗?是的,先生,我在第一行中添加了示例,说明了我如何找到所选的单选按钮。在回答您时,我现在意识到我使用的是gtkmm,而不是问题所问的gtk+。抱歉,但我认为这段代码无论如何都是有用的。我还想澄清一下,我编写了diff连接信号的不同方式。第一种是我的方式