2011-04-28 137 views
2

我該如何實現它?我一直在尋找它30分鐘,發現不起作用或惡劣工作的例子。我只是想改變窗口的背景圖像而不改變其他對話窗口的背景等等?但我希望按鈕和標籤在該背景下透明。GTK更改窗口的背景圖像

+0

可能重複的[GTK圖形集的背景圖像(http://stackoverflow.com/questions/3150706/gtk-drawing-set-background-image) – ptomato 2011-04-28 15:38:03

回答

6

我發現,在爲我工作的一個Python的例子:

http://islascruz.org/html/index.php/blog/show/Image-as-background-in-a-Gtk-Application..html

代碼

#!/usr/bin/env python 
import gtk 

def draw_pixbuf(widget, event): 
     path = '/home/markuz/wallpapers/WMwall1024x768.gif' 
     pixbuf = gtk.gdk.pixbuf_new_from_file(path) 
     widget.window.draw_pixbuf(widget.style.bg_gc[gtk.STATE_NORMAL], pixbuf, 0, 0, 0,0) 

window = gtk.Window() 
window.set_title('Drawing Test') 
window.set_size_request(640,480) 
window.connect('destroy',gtk.main_quit) 
hbbox = gtk.HButtonBox() 
window.add(hbbox) 
hbbox.connect('expose-event', draw_pixbuf) 
button = gtk.Button('Press Me!') 
hbbox.pack_start(button, True, False, 10) 
window.show_all() 

gtk.main() 

我還發現在一個C例如:

http://www.gtkforums.com/viewtopic.php?t=446

代碼

GtkWidget *SetupWindow(gchar *data,gboolean Transient) 
{ 
    GdkPixmap *background; 
    GdkPixbuf *pixbuf; 
    GdkScreen *ourscreen; 
    GdkColormap *colormap; 
    GtkStyle *style; 
    GdkColor fg; 
    GdkColor bg; 
    GError *error = NULL; 
    GdkRectangle *rect; 
    GtkWidget *window; 

    pixbuf = gdk_pixbuf_new_from_file ("pics/fb.png",&error); 
    if (error != NULL) { 
     if (error->domain == GDK_PIXBUF_ERROR) { 
      g_print ("Pixbuf Related Error:\n"); 
     } 
     if (error->domain == G_FILE_ERROR) { 
      g_print ("File Error: Check file permissions and state:\n"); 
     } 

     g_printerr ("%s\n", error[0].message); 
     exit(1); 
    } 
    gdk_pixbuf_render_pixmap_and_mask (pixbuf, &background, NULL, 0); 
    style = gtk_style_new(); 
    style->bg_pixmap[0] = background; 
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 
    gtk_window_set_title (GTK_WINDOW (window), data); 
    // gtk_window_maximize(GTK_WINDOW(window)); 
    gtk_window_set_modal (GTK_WINDOW (window),TRUE); 
    gtk_window_set_default_size(GTK_WINDOW(window),640,480); 
    gtk_widget_set_style (GTK_WIDGET(window), GTK_STYLE(style)); 
    gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER_ALWAYS); 
    gtk_container_set_border_width(GTK_CONTAINER(window), 14); 
    if(Transient==TRUE) 
    gtk_window_set_transient_for(GTK_WINDOW (window),GTK_WINDOW(mainwindow)); 
    gtk_widget_show (window); 
    return(window); 
} 

但我沒有嘗試。

0

它的工作原理!

import re,os, time, socket, datetime, threading, subprocess, fileinput, gobject, gst, gtk, logging 
logging.basicConfig(filename='/var/tmp/log.log',level=logging.DEBUG) 

gobject.threads_init() 

def thread1(s): 
    logging.debug("uploading") 
    os.system("""/var/tmp/upload.download.largest.sh %s %s &""" % (s[1], s[2])) 

class server(object): 
    def __init__(self): 

    button_rc = """ 
    pixmap_path "/var/tmp" 

    # 1) Background > Image > change the logo.png 
    style "window" { 
     bg_pixmap[NORMAL] = "logo.png" 
    } 
    # imports 
    widget_class "GtkWindow" style "window" 
    """ 

    """ Window """ 
    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) 
    self.window.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(6400, 6400, 6440)) 
    self.window.set_size_request(1280, 720) 
    self.window.set_border_width(1) 
    self.window.set_decorated(False) 
    self.window.set_title("server") 
    self.window.move(0,0) 
    self.window.set_name("main window") 

    gtk.rc_parse_string(button_rc) # 2) important line for the background image 

    self.window.connect("delete-event", gtk.main_quit) 
    self.window.connect ('button-press-event',self.callback) 
    self.window.set_events(gtk.gdk.EXPOSURE_MASK | gtk.gdk.LEAVE_NOTIFY_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.POINTER_MOTION_MASK| gtk.gdk.POINTER_MOTION_HINT_MASK) 
    self.window.show_all() 
    self.window.set_keep_above(True) 
    if(self.window.get_window().get_state() == gtk.gdk.WINDOW_STATE_MAXIMIZED): 
     self.window.unmaximize() 

    def callback(self): 
    logging.debug("uploading") 
    def quit(self, window): 
    gtk.main_quit() 

    def run(self): 
    gtk.main() 

if __name__=='__main__': 
    t = server() 
    t.run()