I wrote this code, it's supposed to be a clipart browser (locked to "klip" folder under current directory).
When I start add signal at folder button (dbutton) I screw up: I don't know how to refresh the toolitemgroup content and icons keep appended, I can't remove manually since the button generated in loop.
And one more, how can I "auto-fit/trim" the button label? Cause it grows insane if the filename is lengthy.
import gtk
from os.path import join,normpath,splitext, isdir
from os import getcwd, walk, mkdir
klip="klip"
klipdir=join(getcwd(),klip)
#init dir
if not isdir(klipdir):
mkdir(klipdir)
class kaosmu:
def icon_builder_cb(self, widget, data=klip):
if data != klip:
homebutton = gtk.ToolButton(gtk.STOCK_HOME)
browser.add(homebutton)
upbutton = gtk.ToolButton(gtk.STOCK_GO_UP)
browser.add(upbutton)
#folder
idx=dirs.index(data)
print idx
for i in dirs[idx+1]:
dbutton = gtk.ToolButton(gtk.STOCK_OPEN)
dbutton.set_label(i)
db开发者_Python百科utton.connect("clicked", self.icon_builder_cb, join(data,i))
browser.add(dbutton)
#files
for i in dirs[idx+2]:
pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(join(data,i),32,32)
img=gtk.Image()
img.set_from_pixbuf(pixbuf)
svg = gtk.ToolButton(img,splitext(i)[0])
browser.add(svg)
browser.show_all()
def im_browser_cb(self, widget, data=klipdir):
global dirs
dirs = []
for (p, d, f) in walk(data):
key=p[len(data)-len(klip):]
dirs.append(key)
dirs.append(d)
dirs.append(f)
def __init__(self):
self.im_browser_cb(None)
window = gtk.Window()
window.set_default_size(1024, 800)
window.set_border_width(2)
window.connect("destroy", lambda w: gtk.main_quit())
drawingarea = gtk.DrawingArea()
drawingarea.set_size_request(600, 700)
hpane = gtk.HPaned()
menuscroll = gtk.ScrolledWindow()
canvasscroll = gtk.ScrolledWindow()
toolpalette = gtk.ToolPalette()
toolpalette.set_style(gtk.TOOLBAR_BOTH)
toolpalette.set_icon_size(gtk.ICON_SIZE_DND)
hpane.set_position(250)
window.add(hpane)
hpane.add1(menuscroll)
hpane.add2(canvasscroll)
menuscroll.add_with_viewport(toolpalette)
canvasscroll.add_with_viewport(drawingarea)
global browser
browser = gtk.ToolItemGroup(" Browser ")
toolpalette.add(browser)
toolpalette.set_expand(browser, True)
self.icon_builder_cb(None)
window.show_all()
def main(self):
gtk.main()
if __name__ == "__main__":
kaos = kaosmu()
kaos.main()
I solved this with:
while browser.get_nth_item(0):
browser.remove(browser.get_nth_item(0))
精彩评论