Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Pick color and store #HEX in system clipboard
#!/usr/bin/env python
import gtk
import gobject
import pynotify
import sys
# Clipboard object
clipboard = None
# Initialize system notifications
pynotify.init("Color picker")
# Delay ID to implement "pause" for color changing events
delay = None
def main(argv):
global clipboard
# Create and run color selection dialog
dialog = gtk.ColorSelectionDialog("Color picker")
dialog.colorsel.connect("color-changed", copyColorDelayed) # Signal connection
response =
if response == gtk.RESPONSE_OK:
# Need to store clipboard to prevent reset when exiting
if clipboard != None:
# Close dialog: this ends GTK main thread
def copyColor(color):
global clipboard
# Parse color to HEX string
rgb = ( / 257, / 257, / 257)
hexcolor = ('#%02x%02x%02x' % rgb).upper()
print '{} <- {}'.format(hexcolor, str(color))
# Copy HEX to clipboard
if clipboard == None: clipboard = gtk.Clipboard()
# Show notification
notification = pynotify.Notification(hexcolor, "Color copied to clipboard")
return False
def copyColorDelayed(colorSelection):
global delay
# Sets color in clipboard only when color selection is idle for a certain amount of time
if delay != None: gobject.source_remove(delay)
delay = gobject.timeout_add(250, copyColor, colorSelection.get_current_color())
if __name__ == '__main__': sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.