#!/usr/bin/env python # example colorsel.py import gtk import GDK class ColorSelectionExample: # Color changed handler def color_changed_cb(self, widget): # Get drawingarea colormap colormap = self.drawingarea.get_colormap() # Get current color colordata = self.colorseldlg.colorsel.get_color() # Fit to a unsigned 16 bit integer (0..65535) and # insert into the GdkColor structure # And allocate color gdk_color = apply(colormap.alloc, map(lambda color: int(color*65535.0), colordata)) # Set window background color style = self.drawingarea.get_style().copy() style.bg[gtk.STATE_NORMAL] = gdk_color self.drawingarea.set_style(style) # Clear window self.drawingarea.draw_rectangle(style.bg_gc[gtk.STATE_NORMAL], gtk.TRUE, 0, 0, -1, -1) # Drawingarea event handler def area_event(self, widget, event): handled = gtk.FALSE # Check if we've received a button pressed event if event.type == GDK.BUTTON_PRESS: if self.colorseldlg == None: # Yes, we have an event and there's no colorseldlg yet! handled = gtk.TRUE # Create color selection dialog self.colorseldlg = gtk.GtkColorSelectionDialog( "Select background color") # Get the ColorSelection widget colorsel = self.colorseldlg.colorsel # Connect to the "color_changed" signal # to the colorsel widget colorsel.connect("color_changed", self.color_changed_cb) self.colorseldlg.connect( "delete_event", self.dialog_delete_cb) self.colorseldlg.ok_button.connect_object( "clicked", self.colorseldlg.hide, self.colorseldlg) self.colorseldlg.cancel_button.connect_object( "clicked", self.colorseldlg.hide, self.colorseldlg) # Show the dialog self.colorseldlg.show() return handled def dialog_delete_cb(self, widget, event): widget.hide() return gtk.TRUE # Close down and exit handler def destroy_window(self, widget, event): gtk.mainquit() return gtk.TRUE def __init__(self): self.colorseldlg = None # Create toplevel window, set title and policies window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL) window.set_title("Color selection test") window.set_policy(gtk.TRUE, gtk.TRUE, gtk.TRUE) # Attach to the "delete" and "destroy" events so we can exit window.connect("delete_event", self.destroy_window) # Create drawingarea, set size and catch button events self.drawingarea = gtk.GtkDrawingArea() self.drawingarea.size(200, 200) self.drawingarea.set_events(GDK.BUTTON_PRESS_MASK) self.drawingarea.connect("event", self.area_event) # Add drawingarea to window, then show them both window.add(self.drawingarea) self.drawingarea.show() window.show() def main(): gtk.mainloop() return 0 if __name__ == "__main__": ColorSelectionExample() main()