GTK Glade3显示文本

发布于 2021-11-12 08:10:20 字数 12237 浏览 810 评论 5

#include <gtk-2.0/gtk/gtk.h>
#include <stdlib.h>
gint delete_event( GtkWidget *widget,GdkEvent  *event,gpointer data )
{
	gtk_main_quit ();
	return FALSE;
}
void openfile(GtkWidget *widget, gpointer *data)  
{  
	//	   GtkWidget *FileSelection;  
	//	   FileSelection = gtk_file_selection_new ("选择文件");    //创建文件选择构件 
	//	   gtk_file_selection_set_filename (GTK_FILE_SELECTION (FileSelection),"*.txt *.doc");  
	//	   gtk_widget_show (FileSelection);  
	GtkWidget *dialog;
//	FILE *file;
	dialog = gtk_file_chooser_dialog_new("Open File",NULL,GTK_FILE_CHOOSER_ACTION_OPEN,GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL,GTK_STOCK_OPEN,GTK_RESPONSE_ACCEPT,NULL);
	if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
	{
		gchar *filename;
		gchar *content;
		gsize bytes;
		GError *error = NULL;
		GtkTextBuffer *buffer;
		filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
		//		open_file(filename);
	//	file=fopen(filename,"rb");
		g_file_get_contents(filename, &content, &bytes, &error);
		if (error != NULL) {
			g_printf(error->message);
			g_clear_error(&error);
		}

		buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (text));
		gtk_text_buffer_set_text (buffer, content, -1);

		g_free(filename);
	}
	gtk_widget_destroy(dialog);
}

int main(int argc,char* argv[])
{
	/*创建指向GtkWidget类型的指针*/
	GtkWidget *window;
	GtkBuilder *builder = NULL;
	/*初始化*/
	gtk_init(&argc,&argv);

	builder=gtk_builder_new();
	gtk_builder_add_from_file(builder,"text.glade",NULL);
	gtk_builder_connect_signals(builder, NULL);

	// 根据 ID 获取子构件 
	window=GTK_WIDGET(gtk_builder_get_object(builder,"window")); 
	//	g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(PressClear),NULL);
	g_signal_connect (G_OBJECT (window), "destroy",G_CALLBACK (gtk_main_quit), NULL);

	//	g_signal_connect (G_OBJECT(menubar), "activate",G_CALLBACK (openfile), NULL);

	g_object_unref(G_OBJECT(builder));

	gtk_widget_show_all(window);
	gtk_main();
	return 0;
}

下面是glade3的xml

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="window">
    <child>
      <object class="GtkVBox" id="vbox">
        <property name="visible">True</property>
        <child>
          <object class="GtkMenuBar" id="menubar">
            <property name="visible">True</property>
            <property name="ubuntu_local">True</property>
            <child>
              <object class="GtkMenuItem" id="file">
                <property name="visible">True</property>
                <property name="label" translatable="yes">文件(_F)</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu" id="menu1">
                    <property name="visible">True</property>
                    <property name="ubuntu_local">True</property>
                    <child>
                      <object class="GtkImageMenuItem" id="new">
                        <property name="label">gtk-new</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="open">
                        <property name="label">gtk-open</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                        <signal name="activate" handler="openfile" object="text"/>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="save">
                        <property name="label">gtk-save</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="save-as">
                        <property name="label">gtk-save-as</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkSeparatorMenuItem" id="separatormenuitem1">
                        <property name="visible">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="quit">
                        <property name="label">gtk-quit</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem" id="edit">
                <property name="visible">True</property>
                <property name="label" translatable="yes">编辑(_E)</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu" id="menu2">
                    <property name="visible">True</property>
                    <property name="ubuntu_local">True</property>
                    <child>
                      <object class="GtkImageMenuItem" id="cut">
                        <property name="label">gtk-cut</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="copy">
                        <property name="label">gtk-copy</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="paste">
                        <property name="label">gtk-paste</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="delete">
                        <property name="label">gtk-delete</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem" id="view">
                <property name="visible">True</property>
                <property name="label" translatable="yes">视图(_V)</property>
                <property name="use_underline">True</property>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem" id="help">
                <property name="visible">True</property>
                <property name="label" translatable="yes">帮助(_H)</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu" id="menu3">
                    <property name="visible">True</property>
                    <property name="ubuntu_local">True</property>
                    <child>
                      <object class="GtkImageMenuItem" id="about">
                        <property name="label">gtk-about</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkTextView" id="text">
            <property name="width_request">700</property>
            <property name="height_request">500</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="pixels_above_lines">1</property>
            <property name="pixels_below_lines">1</property>
            <property name="wrap_mode">word-char</property>
            <property name="left_margin">1</property>
            <property name="right_margin">1</property>
          </object>
          <packing>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkStatusbar" id="statusbar">
            <property name="visible">True</property>
            <property name="tooltip_text" translatable="yes">状态栏</property>
            <property name="spacing">2</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

我想做的是打开一个文件并显示在文本中(text构件)、

菜鸟求解释

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清晨说ぺ晚安 2021-11-18 10:21:59

能帮忙解释下么,小弟最近有兴趣就看了下GTK ,想求一个完整到记事本源码参考。

不乱于心 2021-11-18 10:19:12

如果在XML里设置了user data,GtkBuilder就会交换参数的顺序。

输什么也不输骨气 2021-11-18 06:49:50

将C代码中第33行改为

buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (widget));

即可。

月牙弯弯 2021-11-17 22:44:03

我记得gtk+开发环境有一个doc和demo的程序。。你可以看看

柠檬 2021-11-14 13:14:51

坐等、

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文