import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk class ColorButtonWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="ColorButton Example") # Create a color button color_button = Gtk.ColorButton(alpha=255) # Set the initial color color_button.set_rgba(Gdk.RGBA(0, 0, 1, 1)) # Connect the signal to handler color_button.connect("color-set", self.on_color_set) # Add the color button to window self.add(color_button) def on_color_set(self, widget): color = widget.get_rgba() print("The color is", color.to_string()) win = ColorButtonWindow() win.connect("destroy", Gtk.main_quit) win.show_all() Gtk.main()
import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk class ColorButtonWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="ColorButton Example") # Create a color button color_button = Gtk.ColorButton(alpha=255) # Set the initial color color_button.set_rgba(Gdk.RGBA(0, 0, 1, 1)) # Connect the signal to handler color_button.connect("color-set", self.on_color_set) # Add the color button to window self.add(color_button) def on_color_set(self, widget): color = widget.get_rgba() # Create a new Gtk.Label with the selected color as foreground label = Gtk.Label("Selected Color") label.override_color(Gtk.StateFlags.NORMAL, color) # Show the label in a new window window = Gtk.Window() window.add(label) window.show_all() win = ColorButtonWindow() win.connect("destroy", Gtk.main_quit) win.show_all() Gtk.main()This example creates a Gtk.Window with a Gtk.ColorButton widget. When the user sets a color by clicking the button and choosing a color from the dialog, the "color-set" signal is emitted, and a new Gtk.Label is created with the selected color as the foreground color. The label is then shown in a new window.