我正在用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)
再次感謝。