我正在用C++和GTK 3创建一个小程序(刚刚学习它),并且我遇到了这个问题。所以,我孤立了有问题的部分。gtk_entry_get_text:assertion'GTK_IS_ENTRY(entry)'failed
它被用来获取条目并在按钮被点击时打印它。
这是代码:
#include <iostream>
#include <gtk/gtk.h>
using namespace std;
GtkWidget *wventana;
GtkWidget *wgrid;
//FUNCS
void ventana(string titulo, int margen)
{
const char * tituloc = titulo.c_str();
wventana = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_position (GTK_WINDOW (wventana), GTK_WIN_POS_CENTER);
gtk_window_set_title (GTK_WINDOW (wventana), tituloc);
gtk_container_set_border_width(GTK_CONTAINER(wventana), margen);
}
void grid()
{
wgrid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(wventana), wgrid);
gtk_grid_set_row_spacing (GTK_GRID (wgrid), 10);
gtk_grid_set_column_spacing (GTK_GRID (wgrid), 25);
}
void boton_clic (GtkEntry *wentrada, gpointer user_data)
{
const char *nombre;
nombre = gtk_entry_get_text(wentrada);
cout << nombre << endl;
}
void entrada(int x, int y, int lx, int ly)
{
GtkWidget *wentrada;
wentrada = gtk_entry_new();
gtk_grid_attach (GTK_GRID (wgrid), wentrada, x, y, lx, ly);
}
void boton(string texto, int x, int y, int lx, int ly)
{
const char * wtexto = texto.c_str();
GtkWidget *wboton;
wboton = gtk_button_new_with_label (wtexto);
gtk_grid_attach (GTK_GRID (wgrid), wboton, x, y, lx, ly);
g_signal_connect (GTK_BUTTON (wboton), "clicked", G_CALLBACK (boton_clic), G_OBJECT (wventana));
}
//MAIN
int main(int argc, char *argv[])
{
gtk_init (&argc, &argv);
ventana("ventana",10);
grid();
entrada(1, 1, 1, 1);
boton("Aprietame", 1, 2, 1, 1);
gtk_widget_show_all (wventana);
gtk_main();
return 0;
}
它没有得到的条目,并显示以下错误消息:
(boton:6669): Gtk-CRITICAL **: gtk_entry_get_text: assertion 'GTK_IS_ENTRY (entry)' failed
任何人都可以告诉的问题是什么?而且,我怎样才能使它工作? 或者无论如何,这样做有什么好的选择? (它必须与分开的功能)
在此先感谢。
谢谢您的回答@ptomato,我想与大家提出的修改替换,得到了新的代码:
#include <iostream>
#include <gtk/gtk.h>
using namespace std;
GtkWidget *wventana;
GtkWidget *wgrid;
//FUNCS
void ventana(string titulo, int margen)
{
const char * tituloc = titulo.c_str();
wventana = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_position (GTK_WINDOW (wventana), GTK_WIN_POS_CENTER);
gtk_window_set_title (GTK_WINDOW (wventana), tituloc);
gtk_container_set_border_width(GTK_CONTAINER(wventana), margen);
}
void grid()
{
wgrid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(wventana), wgrid);
gtk_grid_set_row_spacing (GTK_GRID (wgrid), 10);
gtk_grid_set_column_spacing (GTK_GRID (wgrid), 25);
}
void boton_clic (GtkButton *wboton, GtkEntry *wentrada)
{
const char *nombre;
nombre = gtk_entry_get_text (wentrada);
cout << nombre << endl;
}
void entrada(int x, int y, int lx, int ly)
{
GtkWidget *wentrada;
wentrada = gtk_entry_new();
gtk_grid_attach (GTK_GRID (wgrid), wentrada, x, y, lx, ly);
}
void boton(string texto, int x, int y, int lx, int ly)
{
const char * wtexto = texto.c_str();
GtkWidget *wboton;
wboton = gtk_button_new_with_label (wtexto);
gtk_grid_attach (GTK_GRID (wgrid), wboton, x, y, lx, ly);
g_signal_connect (wboton, "clicked", G_CALLBACK (boton_clic), wentrada);
}
//MAIN
int main(int argc, char *argv[])
{
gtk_init (&argc, &argv);
ventana("ventana",10);
grid();
entrada(1, 1, 1, 1);
boton("Aprietame", 1, 2, 1, 1);
gtk_widget_show_all (wventana);
gtk_main();
return 0;
}
但是当我尝试编译它,它表明:
In file included from /usr/include/glib-2.0/gobject/gobject.h:28:0,
from /usr/include/glib-2.0/gobject/gbinding.h:29,
from /usr/include/glib-2.0/glib-object.h:23,
from /usr/include/glib-2.0/gio/gioenums.h:28,
from /usr/include/glib-2.0/gio/giotypes.h:28,
from /usr/include/glib-2.0/gio/gio.h:26,
from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:28,
from /usr/include/gtk-3.0/gdk/gdk.h:32,
from /usr/include/gtk-3.0/gtk/gtk.h:30,
from boton.cpp:2:
boton.cpp: In function ‘void boton(std::string, int, int, int, int)’:
boton.cpp:51:64: error: ‘wentrada’ was not declared in this scope
g_signal_connect (wboton, "clicked", G_CALLBACK (boton_clic), wentrada);
^
/usr/include/glib-2.0/gobject/gsignal.h:472:73: note: in definition of macro ‘g_signal_connect’
g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NULL, (GConnectFlags) 0)
^
Compilation failed.
有什么不对?我该怎么办???
请您再解释一下第二个参数吗? (英语不是我的母语,这部分对我来说有点令人困惑:P)
再次感谢。