Debian Codegraph @

docs/tutorials/gtk4/examples/layout_flowbox.py

raw
1 import gi
2
3 gi.require_version("Gtk", "4.0")
4 from gi.repository import Gtk, Gdk
5
6
7 class FlowBoxWindow(Gtk.ApplicationWindow):
8 def __init__(self, **kargs):
9 super().__init__(**kargs, title="FlowBox Demo")
10
11 self.set_default_size(300, 250)
12
13 header = Gtk.HeaderBar()
14 self.set_titlebar(header)
15
16 scrolled = Gtk.ScrolledWindow()
17 scrolled.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
18 self.set_child(scrolled)
19
20 flowbox = Gtk.FlowBox()
21 flowbox.props.valign = Gtk.Align.START
22 flowbox.props.max_children_per_line = 30
23 flowbox.props.selection_mode = Gtk.SelectionMode.NONE
24 scrolled.set_child(flowbox)
25
26 self.create_flowbox(flowbox)
27
28 def draw_button_color(self, area, cr, width, height, rgba):
29 context = area.get_style_context()
30
31 Gtk.render_background(context, cr, 0, 0, width, height)
32
33 cr.set_source_rgba(rgba.red, rgba.green, rgba.blue, rgba.alpha)
34 cr.rectangle(0, 0, width, height)
35 cr.fill()
36
37 def color_swatch_new(self, str_color):
38 rgba = Gdk.RGBA()
39 rgba.parse(str_color)
40
41 button = Gtk.Button()
42
43 area = Gtk.DrawingArea()
44 area.set_size_request(24, 24)
45 area.set_draw_func(self.draw_button_color, rgba)
46
47 button.set_child(area)
48
49 return button
50
51 def create_flowbox(self, flowbox):
52 colors = [
53 "AliceBlue",
54 "AntiqueWhite",
55 "AntiqueWhite1",
56 "AntiqueWhite2",
57 "AntiqueWhite3",
58 "AntiqueWhite4",
59 "aqua",
60 "aquamarine",
61 "aquamarine1",
62 "aquamarine2",
63 "aquamarine3",
64 "aquamarine4",
65 "azure",
66 "azure1",
67 "azure2",
68 "azure3",
69 "azure4",
70 "beige",
71 "bisque",
72 "bisque1",
73 "bisque2",
74 "bisque3",
75 "bisque4",
76 "black",
77 "BlanchedAlmond",
78 "blue",
79 "blue1",
80 "blue2",
81 "blue3",
82 "blue4",
83 "BlueViolet",
84 "brown",
85 "brown1",
86 "brown2",
87 "brown3",
88 "brown4",
89 "burlywood",
90 "burlywood1",
91 "burlywood2",
92 "burlywood3",
93 "burlywood4",
94 "CadetBlue",
95 "CadetBlue1",
96 "CadetBlue2",
97 "CadetBlue3",
98 "CadetBlue4",
99 "chartreuse",
100 "chartreuse1",
101 "chartreuse2",
102 "chartreuse3",
103 "chartreuse4",
104 "chocolate",
105 "chocolate1",
106 "chocolate2",
107 "chocolate3",
108 "chocolate4",
109 "coral",
110 "coral1",
111 "coral2",
112 "coral3",
113 "coral4",
114 ]
115
116 for color in colors:
117 button = self.color_swatch_new(color)
118 button.props.tooltip_text = color
119 flowbox.append(button)
120
121
122 def on_activate(app):
123 # Create window
124 win = FlowBoxWindow(application=app)
125 win.present()
126
127
128 app = Gtk.Application(application_id="com.example.App")
129 app.connect("activate", on_activate)
130
131 app.run(None)