2014-11-09 171 views
0

忍受我,因爲我對GTK和GDK非常陌生。關閉GTK窗口

我想通過幾個圖像循環,對它們進行修改(在各個點繪製一個圓圈),並從標準輸入接受用戶輸入。

我寫了C++類來包裝GTK框架,所以我可以簡化圖像處理。我目前正在打開每個圖像的個別窗口,要求輸入,關閉該窗口,然後打開下一個窗口。

我可以做的一切都很好,除非讓窗口關閉以編程方式,並讓用戶這樣做是不可接受的(即太乏味)。以下是打開和關閉窗口的代碼。

void PixelImage::show() { 
    gtk_widget_show_all(this->window); 
    gtk_main(); 
} 

void PixelImage::close() { 
    gtk_window_close((GtkWindow*)this->window); 
} 

PixelImage::PixelImage(const char *fname) { 
    this->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 
    g_signal_connect(this->window, "destroy", 
        G_CALLBACK (gtk_main_quit), NULL); 

    this->fname = std::string(fname); 
    this->image = gtk_image_new_from_file(fname); 
    this->pix = gtk_image_get_pixbuf((GtkImage*)this->image); 
    this->pixels = gdk_pixbuf_get_pixels(this->pix); 
    this->len = gdk_pixbuf_get_byte_length(this->pix); 

    this->width = gdk_pixbuf_get_width(this->pix); 
    this->height = gdk_pixbuf_get_height(this->pix); 
    this->nchannels = gdk_pixbuf_get_n_channels(this->pix); 
    this->rowstride = gdk_pixbuf_get_rowstride(this->pix); 

    gtk_container_add(GTK_CONTAINER (this->window), this->image); 
} 

當顯示後調用close時,窗口將保留,當我關閉它時會出現以下錯誤。

(IMG:2173):GTK的CRITICAL **:gtk_widget_get_realized:斷言 'GTK_IS_WIDGET(插件)' 失敗

回答

0

所以我會回答我的問題。也許有更好的方法,我很樂意聽到,但這是我解決它的方法。

我用POSIX線程打開一個線程,打開圖像窗口,然後在主線程上做其他事情。然後我簡單地從主線程調用gtk_main_quit()。然後我加入窗戶軸承螺紋。這是代碼。

static void* gtkStarter(void * a) { 
    gtk_main(); 
    return NULL; 
} 

void PixelImage::show() { 
    gtk_widget_show_all(this->window); 
    pthread_create(&this->pp, NULL, gtkStarter, NULL); 
} 

void PixelImage::close() { 
    gtk_main_quit(); 
    pthread_join(this->pp, NULL); 
} 

它似乎工作得很好。