GTK changing background image of window
how can I achieve it? I was searching for it for like 30 minutes and found examples which don't work or work in bad way. I simply want to change background image of window without changing bakgrounds of other dialogs windows etc? But I want buttons and labels to be transparent 开发者_如何学Goon that background.
I found a python example that worked for me at:
http://islascruz.org/html/index.php/blog/show/Image-as-background-in-a-Gtk-Application..html
Code
#!/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()
I also found a C example at:
http://www.gtkforums.com/viewtopic.php?t=446
Code
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);
}
But I didn't try it.
It works!
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()
精彩评论