1from gi.repository import Gtk 2from gi.repository import Gdk 3import sys 4 5 6class MyWindow(Gtk.ApplicationWindow): 7 8 def __init__(self, app): 9 Gtk.Window.__init__(self, title="ColorButton", application=app) 10 self.set_default_size(150, 50) 11 self.set_border_width(10) 12 13 # a colorbutton (which opens a dialogue window in 14 # which we choose a color) 15 self.button = Gtk.ColorButton() 16 # with a default color (blue, in this instance) 17 color = Gdk.RGBA() 18 color.red = 0.0 19 color.green = 0.0 20 color.blue = 1.0 21 color.alpha = 0.5 22 self.button.set_rgba(color) 23 24 # choosing a color in the dialogue window emits a signal 25 self.button.connect("color-set", self.on_color_chosen) 26 27 # a label 28 label = Gtk.Label() 29 label.set_text("Click to choose a color") 30 31 # a grid to attach button and label 32 grid = Gtk.Grid() 33 grid.attach(self.button, 0, 0, 2, 1) 34 grid.attach(label, 0, 1, 2, 1) 35 self.add(grid) 36 37 # if a new color is chosen, we print it as rgb(r,g,b) in the terminal 38 def on_color_chosen(self, user_data): 39 print("You chose the color: " + self.button.get_rgba().to_string()) 40 41 42class MyApplication(Gtk.Application): 43 44 def __init__(self): 45 Gtk.Application.__init__(self) 46 47 def do_activate(self): 48 win = MyWindow(self) 49 win.show_all() 50 51 def do_startup(self): 52 Gtk.Application.do_startup(self) 53 54app = MyApplication() 55exit_status = app.run(sys.argv) 56sys.exit(exit_status) 57