#!/usr/bin/env python from Tkinter import * __doc__ = """ A very simple clipboard program. It floats above your screen and you can paste text into it. I use it to transport snippets of desks across desktops. Use control-n or the right mouse button to spawn a new one. Use control-c or double click to close a window. If you use Fvwm2, like me, you'll probably want to add: Style "ClipTk*" NoTitle, NoHandles, BorderWidth 0, Sticky, StaysOnTop Bugs: - closing things causes a selection to form on the other windows """ __author__ = 'David McClosky (dmcc@bigasterisk.com)' __version__ = '1.0' class ClipTk(Text): instancesopen = 0 def __init__(self, master, *args, **kwargs): kwargs.update({'height' : 2, 'width' : 3, 'bd' : 2, 'relief' : 'raised', 'exportselection' : 0}) Text.__init__(self, master, *args, **kwargs) self.master = master self.clip_args = args self.clip_kwargs = kwargs self.bind("<>", self.expand, '+') self.bind("<>", self.expand, '+') self.bind("", self.spawn, '+') self.bind("<3>", self.spawn, '+') self.bind("", self.close, '+') self.bind("", self.close) ClipTk.instancesopen += 1 def expand(self, evt): self.delete("0.0", END) self.after(100, self.do_expand) def do_expand(self, *args): text = self.get("0.0", END) lines = text.split('\n') lines = stripblankatbeginning(lines) lines.reverse() lines = stripblankatbeginning(lines) lines.reverse() width = 0 for l in lines: width = max(width, len(l)) self['height'] = len(lines) self['width'] = width def spawn(self, *args): t = Toplevel() t.title("ClipTk") newclip = ClipTk(t, *self.clip_args, **self.clip_kwargs) newclip.pack(fill=BOTH, expand=1) newclip.focus() def close(self, *args): ClipTk.instancesopen -= 1 self.master.destroy() if ClipTk.instancesopen <= 0: import sys sys.exit(0) def blank(s): return not s or s.isspace() def stripblankatbeginning(lines): while len(lines) and blank(lines[0]): del lines[0] return lines if __name__ == "__main__": root = Tk() root.title("ClipTk dummy") Label(root).pack() root.iconify() tl = Toplevel() tl.title("ClipTk") clip = ClipTk(tl) clip.pack(fill=BOTH, expand=1) clip.focus() mainloop()