/ lib / python / bwidget.py
bwidget.py
  1  #    Copyright 2004, 2005, 2006 Jeff Epler <jepler@unpythonic.net>
  2  #
  3  #    This program is free software; you can redistribute it and/or modify
  4  #    it under the terms of the GNU General Public License as published by
  5  #    the Free Software Foundation; either version 2 of the License, or
  6  #    (at your option) any later version.
  7  #
  8  #    This program is distributed in the hope that it will be useful,
  9  #    but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 11  #    GNU General Public License for more details.
 12  #
 13  #    You should have received a copy of the GNU General Public License
 14  #    along with this program; if not, write to the Free Software
 15  #    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 16  
 17  """Wrapper for BWidget family of widgets"""
 18  
 19  __author__ = "Jeff Epler <jepler AT unpy DOT net>"
 20  
 21  __all__ = """
 22      Entry Label Button ArrowButton ProgressBar ScrollView Separator
 23  
 24      MainFrame LabelFrame TitleFrame PanelFrame ScrolledWindow ScrollableFrame
 25      PanedWindow ButtonBox PagesManager NoteBook Dialog StatusBar
 26  
 27      LabelEntry ComboBox SpinBox Tree ListBox MessageDialog ProgressDialog
 28      PasswordDialog SelectFont SelectColor SelectColorMenu
 29  
 30      CASCADE CHECKBUTTON COMMAND RADIOBUTTON SEPARATOR STATUS PROGRESSION
 31  """.split()
 32  
 33  import Tkinter, types
 34  
 35  def returnswidget(f):
 36      def w(self, *args, **kw):
 37          r = f(self, *args, **kw)
 38          return self.nametowidget(str(r))
 39      w.__doc__ = f.__doc__
 40      return w
 41  
 42  def makeswidget(f, t):
 43      def w(self, *args, **kw):
 44          r = str(f(self, *args, **kw))
 45          try:
 46              return self.nametowidget(r)
 47          except KeyError:
 48              return makewidget(self, t, str(r))
 49      w.__doc__ = f.__doc__
 50      return w
 51  
 52  def nametowidget(self, name):
 53      """Return the Tkinter instance of a widget identified by
 54      its Tcl name NAME."""
 55      w = self
 56      if name[0] == '.':
 57          w = w._root()
 58          name = name[1:]
 59      while name:
 60          i = name.find('.')
 61          if i >= 0:
 62              name, tail = name[:i], name[i+1:]
 63          else:
 64              tail = ''
 65          while tail:
 66              try:
 67                  w.children[name]
 68              except KeyError:
 69                  j = tail.find('.')
 70                  if j >= 0:
 71                      name, tail = name + "." + tail[:j], tail[j+1:]
 72                  else:
 73                      name, tail = name + "." + tail, ''
 74              else:
 75                  break
 76          w = w.children[name]
 77          name = tail
 78      return w
 79  
 80  Tkinter.Misc.nametowidget = nametowidget
 81      
 82  def makewidget(master, klass, path):
 83      path = str(path)
 84      self = types.InstanceType(klass)
 85      self._name = path[len(master._w)+1:]
 86      self._w = path
 87      self.children = {}
 88      master.children[self._name] = self
 89      self.master = master
 90      self.tk = master.tk
 91      return self
 92  
 93  class BWidget:
 94      def _require(self, master):
 95          master.tk.call("package", "require", "BWidget")
 96      def __init__(self, master, cnf={}, **kw):
 97          self._require(master)
 98          Tkinter.Widget.__init__(self, master, self.__class__.__name__, cnf, kw)
 99  
100  # Simple Widgets
101  class Entry(BWidget, Tkinter.Entry):
102      def invoke(self):
103          return self.tk.call(self._w, "invoke")
104  
105  class Label(BWidget, Tkinter.Label):
106      def setfocus(self):
107          return self.tk.call(self._w, "setfocus")
108  
109  class Button(BWidget, Tkinter.Button): pass
110  
111  class ArrowButton(BWidget, Tkinter.Button): pass
112  
113  class ProgressBar(BWidget, Tkinter.Widget): pass
114  
115  class ScrollView(BWidget, Tkinter.Widget): pass
116  
117  class Separator(BWidget, Tkinter.Widget): pass
118      
119  # Manager Widgets
120  
121  class _Frame:
122      def getframe(self):
123          return self.tk.call(self._w, "getframe")
124      getframe = makeswidget(getframe, Tkinter.Frame)
125  
126  class _Items:
127      def itemcget(self, index, option):
128          return self.tk.call(self._w, "itemcget", index, '-' + option)
129  
130      def itemconfigure(self, index, cnf=None, **kw):
131          return self._configure(('itemconfigure', index), cnf, kw) 
132  
133  CASCADE="cascade"
134  CHECKBUTTON="checkbutton"
135  COMMAND="command"
136  RADIOBUTTON="radiobutton"
137  SEPARATOR="separator"
138  STATUS = "status"
139  PROGRESSION = "progression"
140  
141  class MainFrame(BWidget, _Frame, Tkinter.Widget):
142      def addindicator(self, **kw):
143          return self.tk.call(self._w, "addindicator", *self._options(kw))
144      addindicator = makeswidget(addindicator, Label)
145  
146      def getindicator(self, i):
147          return self.tk.call(self._w, "getindicator", i)
148      getindicator = returnswidget(getindicator)
149  
150      def getmenu(self):
151          return self.tk.call(self._w, "getmenu")
152      getmenu = returnswidget(getmenu)
153  
154      def setmenustate(self, tag, state):
155          return self.tk.call(self._w, "setmenustate", tag, state)
156  
157      def showstatusbar(self, name):
158          return self.tk.call(self._w, "showstatusbar", name)
159  
160      def showtoolbar(self, index, bool_):
161          return self.tk.call(self._w, "showtoolbar", index, bool_)
162  
163  class LabelFrame(BWidget, _Frame, Tkinter.Widget):
164      def align(self, others):
165          return self.tk.call("LabelFrame::align", self, *others)
166  
167  class TitleFrame(BWidget, _Frame, Tkinter.Frame): pass
168  
169  class PanelFrame(BWidget, Tkinter.Frame): pass
170  
171  class ScrolledWindow(BWidget, _Frame, Tkinter.Frame):
172      def setwidget(self, child):
173          return self.tk.call(self._w, "setwidget", child)
174  
175  class ScrollableFrame(BWidget, _Frame, Tkinter.Frame):
176      def see(self, w, vert=None, horiz=None):
177          if vert is None and horiz is None:
178              return self.tk.call(self._w, "see", w)
179          return self.tk.call(self._w, "see", w, vert, horiz)
180  
181      def xview(self, *args):
182          return self.tk.call(self._w, "xview", *args)
183  
184      def yview(self, *args):
185          return self.tk.call(self._w, "yview", *args)
186  
187  class PanedWindow(BWidget, Tkinter.Frame):
188      def add(self, **kw):
189          return self.tk.call(self._w, "add", *self._options(kw))
190      add = makeswidget(add, Tkinter.Frame)
191  
192      def getframe(self, index):
193          return self.tk.call(self._w, "getframe", index)
194      getframe = makeswidget(getframe, Tkinter.Frame)
195  
196  class ButtonBox(BWidget, _Items, Tkinter.Frame):
197      def add(self, **kw):
198          return self.tk.call(self._w, "add", *self._options(kw))
199      add = makeswidget(add, Button)
200  
201      def delete(self, index):
202          self.tk.call(self._w, "delete", index)
203  
204      def index(self, item):
205          self.tk.call(self._w, "index", item)
206  
207      def insert(self, index, *kw):
208          return self.tk.call(self._w, "insert", index, *self._options(kw))
209      insert = makeswidget(insert, Button)
210  
211      def invoke(self, index):
212          return self.tk.call(self._w, "invoke", index)
213  
214      def setfocus(self, index):
215          return self.tk.call(self._w, "setfocus", index)
216  
217  class PagesManager(BWidget, Tkinter.Frame):
218      def add(self, page):
219          return self.tk.call(self._w, "add", page)
220      add = makeswidget(add, Tkinter.Frame)
221  
222      def compute_size(self):
223          return self.tk.call(self._w, "compute_size")
224  
225      def delete(self, page):
226          return self.tk.call(self._w, "delete", page)
227  
228      def getframe(self, page):
229          return self.tk.call(self._w, "getframe", page)
230      getframe = makeswidget(getframe, Tkinter.Frame)
231  
232      def pages(self, *args):
233          return self.tk.call(self._w, "pages", *args)
234  
235      def raise_page(self, page=None):
236          if page is None:
237              return self.tk.call(self._w, "raise")
238          return self.tk.call(self._w, "raise", page)
239  
240  class NoteBook(PagesManager, _Items):
241      def bindtabs(self, event, script):
242          return self.tk.call(self._w, "bindtabs", event, script)
243  
244      def delete(self, page, destroyframe=True):
245          return self.tk.call(self._w, "delete", page, destroyframe)
246  
247      def insert(self, index, page, **kw):
248          return self.tk.call(self._w, "insert", index, page, *self._options(kw))
249      insert = makeswidget(insert, Tkinter.Frame)
250  
251      def move(self, page, index):
252          return self.tk.call(self._w, "move", page, index)
253  
254      def see(self, page):
255          return self.tk.call(self._w, "see", page)
256  
257      def raise_page(self, page=None):
258          if page is None:
259              return self.tk.call(self._w, "raise")
260          else:
261              return self.tk.call(self._w, "raise", page)
262  
263  class Dialog(ButtonBox, Tkinter.BaseWidget, _Frame):
264      def draw(self, focus=None):
265          if focus is None:
266              return self.tk.call(self, "draw")
267          return self.tk.call(self._w, "draw", focus)
268  
269      def enddialog(self):
270          return self.tk.call(self._w, "enddialog")
271  
272      def withdraw(self):
273          return self.tk.call(self._w, "withdraw")
274  
275  class StatusBar(BWidget): pass
276  
277  class LabelEntry(Entry): pass
278      
279  class ComboBox(Entry):
280      def bind_entry(self, *args):
281          return self.tk.call(self._w, "bind", *args)
282  
283      def getlistbox(self):
284          r = str(self.tk.call(self._w, "getlistbox"))
285          try:
286              return self.nametowidget(r)
287          except KeyError:
288              c = self.tk.call("winfo", "class", r)
289              if c == "ListBox":
290                  return makewidget(self, ListBox, r)
291              else:
292                  return makewidget(self, Tkinter.Listbox, r)
293  
294      def getvalue(self):
295          return self.tk.call(self._w, "getvalue")
296  
297      def post(self):
298          return self.tk.call(self._w, "post")
299  
300      def setvalue(self, index):
301          return self.tk.call(self._w, "setvalue", index)
302  
303      def unpost(self):
304          return self.tk.call(self._w, "unpost")
305  
306  class SpinBox(Entry):
307      def bind_entry(self, *args):
308          return self.tk.call(self._w, "bind", *args)
309  
310      def setvalue(self, index):
311          return self.tk.call(self._w, "setvalue", index)
312  
313      def getvalue(self):
314          return self.tk.call(self._w, "getvalue")
315  
316  class Tree(BWidget, Tkinter.Widget, _Items):
317      def bind_image(self, event, script):
318          return self.tk.call(self._w, "bindImage", event, script)
319  
320      def bind_text(self, event, script):
321          return self.tk.call(self._w, "bindText", event, script)
322  
323      def closetree(self, node):
324          return self.tk.call(self._w, "closetree", node)
325  
326      def delete(self, arg, *args):
327          return self.tk.call(self._w, "delete", arg, *args)
328  
329      def edit(self, node, text, *args):
330          "edit(self, node, text, verifycmd=None, clickres=None, select=None)"
331          return self.tk.call(self._w, "edit", node, text, *args)
332  
333      def exists(self, node):
334          return self.tk.call(self._w, "exists", node)
335  
336      def index(self, node):
337          return self.tk.call(self._w, "index", node)
338  
339      def insert(self, index, parent, node="#auto", **kw):
340          return self.tk.call(self._w, "insert", index, parent, node,
341                  *self._options(kw))
342  
343      def move(self, parent, node, index):
344          return self.tk.call(self._w, "move", parent, node, index)
345  
346      def nodes(self, node, *args):
347          return self.tk.call(self._w, "nodes", *args)
348  
349      def opentree(self, node, recurse=True):
350          return self.tk.call(self._w, "opentree", node, recurse)
351  
352      def parent(self, node):
353          return self.tk.call(self._w, "parent", node)
354  
355      def reorder(self, node, neworder):
356          return self.tk.call(self._w, "reorder", node, neworder)
357  
358      def see(self, node):
359          return self.tk.call(self._w, "see", node)
360  
361      def selection_add(self, *args):
362          return self.tk.call(self._w, "selection", "add", *args)
363  
364      def selection_clear(self):
365          return self.tl.call(self._w, "selection", "clear")
366  
367      def selection_get(self):
368          return self.tl.call(self._w, "selection", "get")
369  
370      def selection_includes(self, node):
371          return self.tl.call(self._w, "selection", "includes")
372  
373      def selection_remove(self, *args):
374          return self.tk.call(self._w, "selection", "remove", *args)
375  
376      def selection_set(self, *args):
377          return self.tk.call(self._w, "selection", "set", *args)
378  
379      def selection_toggle(self, *args):
380          return self.tk.call(self._w, "selection", "toggle", *args)
381  
382      def toggle(self, node):
383          return self.tk.call(self._w, "toggle", node)
384      
385      def visible(self, node):
386          return self.tk.call(self._w, "visible", node)
387  
388      def xview(self, *args):
389          return self.tk.call(self.xview, *args)
390  
391      def yview(self, *args):
392          return self.tk.call(self.yview, *args)
393  
394  class ListBox(BWidget, Tkinter.Widget, _Items):
395      def bind_image(self, event, script):
396          return self.tk.call(self._w, "bindImage", event, script)
397  
398      def bind_text(self, event, script):
399          return self.tk.call(self._w, "bindText", event, script)
400  
401      def delete(self, arg, *args):
402          return self.tk.call(self._w, "delete", arg, *args)
403  
404      def edit(self, item, text, *args):
405          "edit(self, item, text, verifycmd=None, clickres=None, select=None)"
406          return self.tk.call(self._w, "edit", item, text, *args)
407  
408      def exists(self, item):
409          return self.tk.call(self._w, "exists", item)
410  
411      def index(self, item):
412          return self.tk.call(self._w, "index", item)
413  
414      def insert(self, index, item="#auto", **kw):
415          return self.tk.call(self._w, "insert", parent, item,
416                  *self._options(kw))
417  
418      def items(self, item, *args):
419          return self.tk.call(self._w, "items", *args)
420  
421      def move(self, parent, item, index):
422          return self.tk.call(self._w, "move", parent, item, index)
423  
424      def reorder(self, node, neworder):
425          return self.tk.call(self._w, "reorder", node, neworder)
426  
427      def see(self, node):
428          return self.tk.call(self._w, "see", node)
429  
430      def selection_add(self, *args):
431          return self.tk.call(self._w, "selection", "add", *args)
432  
433      def selection_clear(self):
434          return self.tl.call(self._w, "selection", "clear")
435  
436      def selection_get(self):
437          return self.tl.call(self._w, "selection", "get")
438  
439      def selection_includes(self, node):
440          return self.tl.call(self._w, "selection", "includes")
441  
442      def selection_remove(self, *args):
443          return self.tk.call(self._w, "selection", "remove", *args)
444  
445      def selection_set(self, *args):
446          return self.tk.call(self._w, "selection", "set", *args)
447  
448      def selection_toggle(self, *args):
449          return self.tk.call(self._w, "selection", "toggle", *args)
450  
451      def xview(self, *args):
452          return self.tk.call(self.xview, *args)
453  
454      def yview(self, *args):
455          return self.tk.call(self.yview, *args)
456  
457  class MessageDialog(Dialog):
458      def __init__(self, master, cnf={}, **kw):
459          self._require(master)
460          Tkinter.Widget.__init__(self, master, "MessageDlg", cnf, kw)
461      
462  class ProgressDialog(Dialog):
463      def __init__(self, master, cnf={}, **kw):
464          self._require(master)
465          Tkinter.Widget.__init__(self, master, "ProgressDlg", cnf, kw)
466  
467  class PasswordDialog(Dialog):
468      def __init__(self, master, cnf={}, **kw):
469          self._require(master)
470          Tkinter.Widget.__init__(self, master, "PasswdDlg", cnf, kw)
471  
472  class SelectFont(Dialog): pass
473  class SelectColor(Dialog):
474      def setcolor(self, index, color):
475          self.tk.call("SelectColor::setcolor", index, color)
476  
477  def SelectColorMenu(master, *args, **kw):
478      i = id([])
479      if master._w=='.':
480          w = '.' + name
481      else:
482          w = master._w + '.' + name
483      return master.tk.call("SelectColor::menu", args, *master._options(kw))
484  
485  # vim:ts=8:sts=4:et: