Gtk图像不会显示在C中

时间:2014-10-19 20:56:12

标签: c gtk

你好我有一些关于GTK的麻烦,我使用file_chooser_dialog来选择一个图像并在我点击“确定”之后打开它但是这不起作用而且根本没有显示任何内容。有人可以向我解释一下吗? 这里的代码和感谢回答。

#include <stdlib.h>
#include <gtk/gtk.h>



static void open_file(GtkWidget* pOpen, gpointer pWindow, GtkWidget* pTable,)
{        
GtkWidget *image = NULL;
GtkWidget *open;
open = gtk_file_chooser_dialog_new("Select a file", GTK_WINDOW(pWindow),
GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_OK, GTK_RESPONSE_OK, 
    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
    gtk_widget_show_all(open);
    gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(open),
    g_get_home_dir());
gint resp = gtk_dialog_run(GTK_DIALOG(open));
if(resp == GTK_RESPONSE_OK)
    {
            GtkFileChooser *chooser = GTK_FILE_CHOOSER(open);
            image = gtk_image_new_from_file(gtk_file_chooser_get_filename(chooser));
            gtk_table_attach_defaults(GTK_TABLE(pTable),image,
            8,12,10,20);
    }
else
    g_print("You pressed Cancel\n");
gtk_widget_destroy(open);
} 

int main(int argc,char *argv[])
{
//Variable
GtkWidget *pWindow = NULL;
GtkWidget *pLabel = NULL;
GtkWidget *pTable = NULL;
GtkWidget *pButton = NULL;
GtkWidget *pOpen = NULL;  



//Iniatialisation && attributs
gtk_init(&argc, &argv);
pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(pWindow), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(pWindow), 720, 480);
pTable = gtk_table_new(40,20, TRUE);
pButton = gtk_button_new_with_label("Open Image");




//Position of the box
gtk_table_attach_defaults(GTK_TABLE(pTable), pButton,
3,7,5,7);
gtk_table_attach_defaults(GTK_TABLE(pTable), pLabel,
3,7,8,9);
gtk_table_attach_defaults(GTK_TABLE(pTable), image,
3,7,1,3);

//add the table to the main window
gtk_container_add(GTK_CONTAINER(pWindow), GTK_WIDGET(pTable));

//Signal
g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(pButton), "clicked", G_CALLBACK(open_file), NULL);
gtk_widget_show_all(pWindow);
gtk_main();

}

1 个答案:

答案 0 :(得分:1)

在启动mainloop之前,你读取了全局(未初始化的!)变量path,然后再也不会这样做了。 path变量首先被填充为gtk_main()行内的回调,该回调处理UI,信号和其他一些东西。