当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 2 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : GTK+编程求助!
帖子发表于 : 2009-06-24 19:58 
头像

注册: 2007-06-11 21:08
帖子: 70
地址: 江南水乡
送出感谢: 0 次
接收感谢: 0 次
刚接触GTK+不久,自己尝试着用glade写了个程序,结果有问题了,请各位大侠帮忙指点一下!

是一个简单的加法器程序!

main.c
代码:
/*
 * Initial main.c file generated by Glade. Edit as required.
 * Glade will not overwrite this file.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>

#include "interface.h"
#include "support.h"

int
main (int argc, char *argv[])
{
  GtkWidget *window1;

#ifdef ENABLE_NLS
  bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);
#endif

  gtk_set_locale ();
  gtk_init (&argc, &argv);

  add_pixmap_directory (PACKAGE_DATA_DIR "/" PACKAGE "/pixmaps");

  /*
   * The following code was added by Glade to create one of each component
   * (except popup menus), just so that you see something after building
   * the project. Delete any components that you don't want shown initially.
   */
  window1 = create_window1 ();
  gtk_widget_show (window1);

  gtk_main ();
  return 0;
}




interface.c
代码:
/*
 * DO NOT EDIT THIS FILE - it is generated by Glade.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"

#define GLADE_HOOKUP_OBJECT(component,widget,name) \
  g_object_set_data_full (G_OBJECT (component), name, \
    gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)

#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
  g_object_set_data (G_OBJECT (component), name, widget)

GtkWidget*
create_window1 (void)
{
  GtkWidget *window1;
  GtkWidget *table1;
  GtkWidget *text1;
  GtkWidget *text2;
  GtkWidget *button;
  GtkWidget *label;

  window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window1), _("window1"));

  table1 = gtk_table_new (4, 1, FALSE);
  gtk_widget_show (table1);
  gtk_container_add (GTK_CONTAINER (window1), table1);

  text1 = gtk_entry_new ();
  gtk_widget_show (text1);
  gtk_table_attach (GTK_TABLE (table1), text1, 0, 1, 0, 1,
                    (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                    (GtkAttachOptions) (0), 20, 20);
  gtk_entry_set_max_length (GTK_ENTRY (text1), 10);
  gtk_entry_set_invisible_char (GTK_ENTRY (text1), 8226);

  text2 = gtk_entry_new ();
  gtk_widget_show (text2);
  gtk_table_attach (GTK_TABLE (table1), text2, 0, 1, 1, 2,
                    (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                    (GtkAttachOptions) (0), 20, 20);
  gtk_entry_set_invisible_char (GTK_ENTRY (text2), 8226);

  button = gtk_button_new_with_mnemonic (_("ok"));
  gtk_widget_show (button);
  gtk_table_attach (GTK_TABLE (table1), button, 0, 1, 2, 3,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 50, 0);

  label = gtk_label_new (_("result"));
  gtk_widget_show (label);
  gtk_table_attach (GTK_TABLE (table1), label, 0, 1, 3, 4,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 70, 30);
  gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_CENTER);
  gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
  gtk_misc_set_padding (GTK_MISC (label), 100, 10);

  g_signal_connect ((gpointer) button, "clicked",
                    G_CALLBACK (on_button_clicked),
                    NULL);

  /* Store pointers to all widgets, for use by lookup_widget(). */
  GLADE_HOOKUP_OBJECT_NO_REF (window1, window1, "window1");
  GLADE_HOOKUP_OBJECT (window1, table1, "table1");
  GLADE_HOOKUP_OBJECT (window1, text1, "text1");
  GLADE_HOOKUP_OBJECT (window1, text2, "text2");
  GLADE_HOOKUP_OBJECT (window1, button, "button");
  GLADE_HOOKUP_OBJECT (window1, label, "label");

  return window1;
}



callbacks.c
代码:
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"
void
on_button_clicked                      (GtkButton       *button,
                                        gpointer         user_data)
{

     GtkWidget *table1;
     GtkWidget *text1;
     GtkWidget *text2;
     GtkWidget *label;
   char a1[10];
   char a2[10];
   char a3[10];
   float a,b,c;
   strcpy(a1,gtk_entry_get_text(GTK_ENTRY(text1)));
   strcpy(a2,gtk_entry_get_text(GTK_ENTRY(text2)));
   a=atof(a1);
   b=atof(a2);
   c=a+b;
   gcvt(c,7,a3);
   gtk_label_set_text(GTK_LABEL(label),a3);
   }


make能通过,提示
引用:
make all-recursive
make[1]: 正在进入目录 `/home/chaoses/桌面/adder/adder'
Making all in src
make[2]: 正在进入目录 `/home/chaoses/桌面/adder/adder/src'
gcc -DHAVE_CONFIG_H -I. -I.. -DPACKAGE_DATA_DIR=\""/usr/local/share"\" -DPACKAGE_LOCALE_DIR=\""/usr/local/share/locale"\" -D_REENTRANT -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/directfb -I/usr/include/libpng12 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
gcc -DHAVE_CONFIG_H -I. -I.. -DPACKAGE_DATA_DIR=\""/usr/local/share"\" -DPACKAGE_LOCALE_DIR=\""/usr/local/share/locale"\" -D_REENTRANT -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/directfb -I/usr/include/libpng12 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -g -O2 -MT support.o -MD -MP -MF .deps/support.Tpo -c -o support.o support.c
mv -f .deps/support.Tpo .deps/support.Po
gcc -DHAVE_CONFIG_H -I. -I.. -DPACKAGE_DATA_DIR=\""/usr/local/share"\" -DPACKAGE_LOCALE_DIR=\""/usr/local/share/locale"\" -D_REENTRANT -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/directfb -I/usr/include/libpng12 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -g -O2 -MT interface.o -MD -MP -MF .deps/interface.Tpo -c -o interface.o interface.c
mv -f .deps/interface.Tpo .deps/interface.Po
gcc -DHAVE_CONFIG_H -I. -I.. -DPACKAGE_DATA_DIR=\""/usr/local/share"\" -DPACKAGE_LOCALE_DIR=\""/usr/local/share/locale"\" -D_REENTRANT -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/directfb -I/usr/include/libpng12 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -g -O2 -MT callbacks.o -MD -MP -MF .deps/callbacks.Tpo -c -o callbacks.o callbacks.c
callbacks.c: 在函数‘on_button_clicked’中:
callbacks.c:25: 警告: 隐式声明与内建函数‘strcpy’不兼容
mv -f .deps/callbacks.Tpo .deps/callbacks.Po
gcc -g -O2 -o adder main.o support.o interface.o callbacks.o -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lpangoft2-1.0 -lgdk_pixbuf-2.0 -lm -lpangocairo-1.0 -lgio-2.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lglib-2.0
make[2]:正在离开目录 `/home/chaoses/桌面/adder/adder/src'
Making all in po
make[2]: 正在进入目录 `/home/chaoses/桌面/adder/adder/po'
make[2]: 没有什么可以做的为 `all'。
make[2]:正在离开目录 `/home/chaoses/桌面/adder/adder/po'
make[2]: 正在进入目录 `/home/chaoses/桌面/adder/adder'
make[2]:正在离开目录 `/home/chaoses/桌面/adder/adder'
make[1]:正在离开目录 `/home/chaoses/桌面/adder/adder'


运行src/adder能出来图形界面,可是有错误提示
引用:
(adder:1920): Gtk-CRITICAL **: gtk_entry_get_text: assertion `GTK_IS_ENTRY (entry)' failed
段错误

然后点击OK按钮后图形界面直接关闭。
我估计是callbacks.c这个文件里面有什么问题,可手头也没有相关的参考书
大家帮忙看看出什么问题了,谢谢


页首
 用户资料  
 
2 楼 
 文章标题 : Re: GTK+编程求助!
帖子发表于 : 2009-06-24 21:18 
头像

注册: 2007-09-26 17:34
帖子: 618
送出感谢: 0 次
接收感谢: 2
代码:
void
on_button_clicked                      (GtkButton       *button,
                                        gpointer         user_data)
{

     GtkWidget *table1;
     GtkWidget *text1;
     GtkWidget *text2;
     GtkWidget *label;
   char a1[10];
   char a2[10];
   char a3[10];
   float a,b,c;
   strcpy(a1,gtk_entry_get_text(GTK_ENTRY(text1)));
   strcpy(a2,gtk_entry_get_text(GTK_ENTRY(text2)));
   a=atof(a1);
   b=atof(a2);
   c=a+b;
   gcvt(c,7,a3);
   gtk_label_set_text(GTK_LABEL(label),a3);
   }

这里声明两个 局部变量,text1和text2,没有初始化,后面调用gtk_entry_get_text当然会出错了。
如果你想调用在interface.c里创建的GtkEntry的话,就要把那个指针声明为全局变量,然后把on_button_clicked里的text1,text2声明去掉。


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 2 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 2 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译