所以,我是 gtk 的新手,我正在尝试制作一个登录表单,当按下“提交”按钮时,我需要发送用户名和密码。我怎样才能做到这一点? 我的代码:
const void button_clicked (GtkWidget *widget,gpointer data)
{
const gchar* text;
text = gtk_entry_get_text(GTK_ENTRY(data));
write(lala,text,100);
}
GtkWidget*window,*label_username,*label_password,*layout,*entry_username,*entry_password,*submit_button;
entry_username = gtk_entry_new();
entry_password = gtk_entry_new();
submit_button = gtk_button_new_with_label("Sumbit");
g_signal_connect(submit_button,"clicked",G_CALLBACK(button_clicked),entry_username;
如何使submit_button函数接收来自entry_username和entry_password的文本?谢谢!
最佳答案
在 gtk 中,对于这种情况,最好将包含小部件的结构作为回调函数的成员传递...
typedef struct MainWindow
{
GtkWidget *entry_username, *entry_password;
}MAINWINDOW;
int main(
MAINWINDOW *mainwindow = malloc(sizeof(MAINWINDOW));
mainwindow->entry_username = gtk_entry_new();
mainwindow->entry_password = gtk_entry_new();
g_signal_connect(submit_button,"clicked",G_CALLBACK(button_clicked),mainwindow);
}
const void button_clicked (GtkWidget *widget,gpointer data)
{
MAINWINDOW *mainwindow = data;
//now you have both the widget's
// modify em as per your wish :)
}
这是一个非常粗略的例子。但这样的东西应该适合你的情况。
关于c - Gtk2 使用同一按钮从 2 个条目获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53833027/