1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
# -*- coding:utf-8 -*-
import pygtk
pygtk.require('2.0')
import gtk
#------------------------------------------------------------------------------
class Palette(gtk.ScrolledWindow):
"""Palette toolbar with automatic lines/columns adaptation."""
def __init__(self):
gtk.ScrolledWindow.__init__(self)
self.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
self.set_shadow_type(gtk.SHADOW_NONE)
self.table = gtk.Table()
self.table.set_homogeneous(False)
self.add_with_viewport(self.table)
self.connect("size-allocate", self.update_organization)
self.tools = []
self.show_all()
def add_tool(self, tool):
"""Add a tool to the palette."""
self.tools.append(tool)
tool.show_all()
def remove_tool(self, tool):
"""Remove the specified tool."""
tool.destroy()
self.tools.remove(tool)
def update_organization(self, widget=None, allocation=None):
"""Callback to execute when the size of the widget is changed."""
width = 1
# Get the widthest widget and remove all widgets from the table
for t in self.tools:
if t.allocation.width > width:
width = t.allocation.width
self.table.remove(t)
#*Get the number of columns and rows
cols = int(allocation.width / width)
if cols < 1:
cols = 1
rows = int(len(self.tools) / cols) + 1
if rows*cols < len(self.tools):
rows += 1
#*Re-organize the table
self.table.resize(rows, cols)
i = 0
for r in range(rows):
for c in range(cols):
if i <= len(self.tools) - 1:
print c, c+1, r, r+1
self.table.attach(self.tools[i], c, c+1, r, r+1, xoptions=gtk.FILL, yoptions=gtk.FILL, xpadding=5, ypadding=5)
i += 1
self.show_all()
print "Done"
#------------------------------------------------------------------------------
if __name__ == "__main__":
win = gtk.Window()
win.connect('destroy', lambda w: gtk.main_quit())
win.set_size_request(150, 150)
p = Palette()
for i in range(25):
b = gtk.Button(stock=gtk.STOCK_SAVE)
b.show()
p.add_tool(b)
win.add(p)
win.show_all()
gtk.main() |
Partager