概述
#include <gtk/gtk.h>
#include <stdio.h>
static void on_button_read()
{
FILE *f;
/*不同的电脑,文件路径可能要改动*/
if((f = fopen("/home/manyikaimen/leo/liyang.txt","w")) == NULL)
{
g_printf("open file failed!n");
}
else
{
fclose(f);
g_printf("button_read was pressed,and we have open the filen");
}
}
static void on_button_write()
{
FILE *f;
char *p = "hello this world";
/*不同的电脑,文件路径可能要改动*/
if((f=fopen("/home/manyikaimen/leo/liyang.txt","w")) == NULL)
{
g_printf("sorry,this file does not exist!");
}
else
{
fwrite(p, strlen(p), 1, f);
fclose(f);
g_printf("button_write was pressed,and we have write to the filen");
}
}
static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
gtk_main_quit ();
return FALSE;
}
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *button_read;
GtkWidget *button_write;
GtkWidget *box1;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Hello Buttons!");
g_signal_connect (window, "delete-event",G_CALLBACK (delete_event), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
box1 = gtk_hbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), box1);
button_read = gtk_button_new_with_label ("read file");
/*为打开按钮添加相应函数*/
g_signal_connect (button_read, "clicked",G_CALLBACK (on_button_read), NULL);
gtk_box_pack_start (GTK_BOX(box1), button_read, TRUE, TRUE, 0);
gtk_widget_show (button_read);
/*为写按钮添加事件相应函数*/
button_write = gtk_button_new_with_label ("write file");
g_signal_connect (button_write, "clicked",G_CALLBACK (on_button_write), NULL);
gtk_box_pack_start(GTK_BOX (box1), button_write, TRUE, TRUE, 0);
gtk_widget_show (button_write);
gtk_widget_show (box1);
gtk_widget_show (window);
gtk_main ();
return 0;
}
最后
以上就是故意红酒为你收集整理的GTK 环境编写的hello world ,另外 两个按钮分别实现读写文件的全部内容,希望文章能够帮你解决GTK 环境编写的hello world ,另外 两个按钮分别实现读写文件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复