util.py 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572
  1. # Natural Language Toolkit: Drawing utilities
  2. #
  3. # Copyright (C) 2001-2020 NLTK Project
  4. # Author: Edward Loper <edloper@gmail.com>
  5. # URL: <http://nltk.org/>
  6. # For license information, see LICENSE.TXT
  7. """
  8. Tools for graphically displaying and interacting with the objects and
  9. processing classes defined by the Toolkit. These tools are primarily
  10. intended to help students visualize the objects that they create.
  11. The graphical tools are typically built using "canvas widgets", each
  12. of which encapsulates the graphical elements and bindings used to
  13. display a complex object on a Tkinter ``Canvas``. For example, NLTK
  14. defines canvas widgets for displaying trees and directed graphs, as
  15. well as a number of simpler widgets. These canvas widgets make it
  16. easier to build new graphical tools and demos. See the class
  17. documentation for ``CanvasWidget`` for more information.
  18. The ``nltk.draw`` module defines the abstract ``CanvasWidget`` base
  19. class, and a number of simple canvas widgets. The remaining canvas
  20. widgets are defined by submodules, such as ``nltk.draw.tree``.
  21. The ``nltk.draw`` module also defines ``CanvasFrame``, which
  22. encapsulates a ``Canvas`` and its scrollbars. It uses a
  23. ``ScrollWatcherWidget`` to ensure that all canvas widgets contained on
  24. its canvas are within the scroll region.
  25. Acknowledgements: Many of the ideas behind the canvas widget system
  26. are derived from ``CLIG``, a Tk-based grapher for linguistic data
  27. structures. For more information, see the CLIG
  28. homepage (http://www.ags.uni-sb.de/~konrad/clig.html).
  29. """
  30. from abc import ABCMeta, abstractmethod
  31. from tkinter import (
  32. Button,
  33. Canvas,
  34. Entry,
  35. Frame,
  36. Label,
  37. Menu,
  38. Menubutton,
  39. Scrollbar,
  40. StringVar,
  41. Text,
  42. Tk,
  43. Toplevel,
  44. Widget,
  45. RAISED,
  46. )
  47. from tkinter.filedialog import asksaveasfilename
  48. from nltk.util import in_idle
  49. ##//////////////////////////////////////////////////////
  50. ## CanvasWidget
  51. ##//////////////////////////////////////////////////////
  52. class CanvasWidget(metaclass=ABCMeta):
  53. """
  54. A collection of graphical elements and bindings used to display a
  55. complex object on a Tkinter ``Canvas``. A canvas widget is
  56. responsible for managing the ``Canvas`` tags and callback bindings
  57. necessary to display and interact with the object. Canvas widgets
  58. are often organized into hierarchies, where parent canvas widgets
  59. control aspects of their child widgets.
  60. Each canvas widget is bound to a single ``Canvas``. This ``Canvas``
  61. is specified as the first argument to the ``CanvasWidget``'s
  62. constructor.
  63. Attributes. Each canvas widget can support a variety of
  64. "attributes", which control how the canvas widget is displayed.
  65. Some typical examples attributes are ``color``, ``font``, and
  66. ``radius``. Each attribute has a default value. This default
  67. value can be overridden in the constructor, using keyword
  68. arguments of the form ``attribute=value``:
  69. >>> from nltk.draw.util import TextWidget
  70. >>> cn = TextWidget(c, 'test', color='red')
  71. Attribute values can also be changed after a canvas widget has
  72. been constructed, using the ``__setitem__`` operator:
  73. >>> cn['font'] = 'times'
  74. The current value of an attribute value can be queried using the
  75. ``__getitem__`` operator:
  76. >>> cn['color']
  77. red
  78. For a list of the attributes supported by a type of canvas widget,
  79. see its class documentation.
  80. Interaction. The attribute ``'draggable'`` controls whether the
  81. user can drag a canvas widget around the canvas. By default,
  82. canvas widgets are not draggable.
  83. ``CanvasWidget`` provides callback support for two types of user
  84. interaction: clicking and dragging. The method ``bind_click``
  85. registers a callback function that is called whenever the canvas
  86. widget is clicked. The method ``bind_drag`` registers a callback
  87. function that is called after the canvas widget is dragged. If
  88. the user clicks or drags a canvas widget with no registered
  89. callback function, then the interaction event will propagate to
  90. its parent. For each canvas widget, only one callback function
  91. may be registered for an interaction event. Callback functions
  92. can be deregistered with the ``unbind_click`` and ``unbind_drag``
  93. methods.
  94. Subclassing. ``CanvasWidget`` is an abstract class. Subclasses
  95. are required to implement the following methods:
  96. - ``__init__``: Builds a new canvas widget. It must perform the
  97. following three tasks (in order):
  98. - Create any new graphical elements.
  99. - Call ``_add_child_widget`` on each child widget.
  100. - Call the ``CanvasWidget`` constructor.
  101. - ``_tags``: Returns a list of the canvas tags for all graphical
  102. elements managed by this canvas widget, not including
  103. graphical elements managed by its child widgets.
  104. - ``_manage``: Arranges the child widgets of this canvas widget.
  105. This is typically only called when the canvas widget is
  106. created.
  107. - ``_update``: Update this canvas widget in response to a
  108. change in a single child.
  109. For a ``CanvasWidget`` with no child widgets, the default
  110. definitions for ``_manage`` and ``_update`` may be used.
  111. If a subclass defines any attributes, then it should implement
  112. ``__getitem__`` and ``__setitem__``. If either of these methods is
  113. called with an unknown attribute, then they should propagate the
  114. request to ``CanvasWidget``.
  115. Most subclasses implement a number of additional methods that
  116. modify the ``CanvasWidget`` in some way. These methods must call
  117. ``parent.update(self)`` after making any changes to the canvas
  118. widget's graphical elements. The canvas widget must also call
  119. ``parent.update(self)`` after changing any attribute value that
  120. affects the shape or position of the canvas widget's graphical
  121. elements.
  122. :type __canvas: Tkinter.Canvas
  123. :ivar __canvas: This ``CanvasWidget``'s canvas.
  124. :type __parent: CanvasWidget or None
  125. :ivar __parent: This ``CanvasWidget``'s hierarchical parent widget.
  126. :type __children: list(CanvasWidget)
  127. :ivar __children: This ``CanvasWidget``'s hierarchical child widgets.
  128. :type __updating: bool
  129. :ivar __updating: Is this canvas widget currently performing an
  130. update? If it is, then it will ignore any new update requests
  131. from child widgets.
  132. :type __draggable: bool
  133. :ivar __draggable: Is this canvas widget draggable?
  134. :type __press: event
  135. :ivar __press: The ButtonPress event that we're currently handling.
  136. :type __drag_x: int
  137. :ivar __drag_x: Where it's been moved to (to find dx)
  138. :type __drag_y: int
  139. :ivar __drag_y: Where it's been moved to (to find dy)
  140. :type __callbacks: dictionary
  141. :ivar __callbacks: Registered callbacks. Currently, four keys are
  142. used: ``1``, ``2``, ``3``, and ``'drag'``. The values are
  143. callback functions. Each callback function takes a single
  144. argument, which is the ``CanvasWidget`` that triggered the
  145. callback.
  146. """
  147. def __init__(self, canvas, parent=None, **attribs):
  148. """
  149. Create a new canvas widget. This constructor should only be
  150. called by subclass constructors; and it should be called only
  151. "after" the subclass has constructed all graphical canvas
  152. objects and registered all child widgets.
  153. :param canvas: This canvas widget's canvas.
  154. :type canvas: Tkinter.Canvas
  155. :param parent: This canvas widget's hierarchical parent.
  156. :type parent: CanvasWidget
  157. :param attribs: The new canvas widget's attributes.
  158. """
  159. if self.__class__ == CanvasWidget:
  160. raise TypeError("CanvasWidget is an abstract base class")
  161. if not isinstance(canvas, Canvas):
  162. raise TypeError("Expected a canvas!")
  163. self.__canvas = canvas
  164. self.__parent = parent
  165. # If the subclass constructor called _add_child_widget, then
  166. # self.__children will already exist.
  167. if not hasattr(self, "_CanvasWidget__children"):
  168. self.__children = []
  169. # Is this widget hidden?
  170. self.__hidden = 0
  171. # Update control (prevents infinite loops)
  172. self.__updating = 0
  173. # Button-press and drag callback handling.
  174. self.__press = None
  175. self.__drag_x = self.__drag_y = 0
  176. self.__callbacks = {}
  177. self.__draggable = 0
  178. # Set up attributes.
  179. for (attr, value) in list(attribs.items()):
  180. self[attr] = value
  181. # Manage this canvas widget
  182. self._manage()
  183. # Register any new bindings
  184. for tag in self._tags():
  185. self.__canvas.tag_bind(tag, "<ButtonPress-1>", self.__press_cb)
  186. self.__canvas.tag_bind(tag, "<ButtonPress-2>", self.__press_cb)
  187. self.__canvas.tag_bind(tag, "<ButtonPress-3>", self.__press_cb)
  188. ##//////////////////////////////////////////////////////
  189. ## Inherited methods.
  190. ##//////////////////////////////////////////////////////
  191. def bbox(self):
  192. """
  193. :return: A bounding box for this ``CanvasWidget``. The bounding
  194. box is a tuple of four coordinates, *(xmin, ymin, xmax, ymax)*,
  195. for a rectangle which encloses all of the canvas
  196. widget's graphical elements. Bounding box coordinates are
  197. specified with respect to the coordinate space of the ``Canvas``.
  198. :rtype: tuple(int, int, int, int)
  199. """
  200. if self.__hidden:
  201. return (0, 0, 0, 0)
  202. if len(self.tags()) == 0:
  203. raise ValueError("No tags")
  204. return self.__canvas.bbox(*self.tags())
  205. def width(self):
  206. """
  207. :return: The width of this canvas widget's bounding box, in
  208. its ``Canvas``'s coordinate space.
  209. :rtype: int
  210. """
  211. if len(self.tags()) == 0:
  212. raise ValueError("No tags")
  213. bbox = self.__canvas.bbox(*self.tags())
  214. return bbox[2] - bbox[0]
  215. def height(self):
  216. """
  217. :return: The height of this canvas widget's bounding box, in
  218. its ``Canvas``'s coordinate space.
  219. :rtype: int
  220. """
  221. if len(self.tags()) == 0:
  222. raise ValueError("No tags")
  223. bbox = self.__canvas.bbox(*self.tags())
  224. return bbox[3] - bbox[1]
  225. def parent(self):
  226. """
  227. :return: The hierarchical parent of this canvas widget.
  228. ``self`` is considered a subpart of its parent for
  229. purposes of user interaction.
  230. :rtype: CanvasWidget or None
  231. """
  232. return self.__parent
  233. def child_widgets(self):
  234. """
  235. :return: A list of the hierarchical children of this canvas
  236. widget. These children are considered part of ``self``
  237. for purposes of user interaction.
  238. :rtype: list of CanvasWidget
  239. """
  240. return self.__children
  241. def canvas(self):
  242. """
  243. :return: The canvas that this canvas widget is bound to.
  244. :rtype: Tkinter.Canvas
  245. """
  246. return self.__canvas
  247. def move(self, dx, dy):
  248. """
  249. Move this canvas widget by a given distance. In particular,
  250. shift the canvas widget right by ``dx`` pixels, and down by
  251. ``dy`` pixels. Both ``dx`` and ``dy`` may be negative, resulting
  252. in leftward or upward movement.
  253. :type dx: int
  254. :param dx: The number of pixels to move this canvas widget
  255. rightwards.
  256. :type dy: int
  257. :param dy: The number of pixels to move this canvas widget
  258. downwards.
  259. :rtype: None
  260. """
  261. if dx == dy == 0:
  262. return
  263. for tag in self.tags():
  264. self.__canvas.move(tag, dx, dy)
  265. if self.__parent:
  266. self.__parent.update(self)
  267. def moveto(self, x, y, anchor="NW"):
  268. """
  269. Move this canvas widget to the given location. In particular,
  270. shift the canvas widget such that the corner or side of the
  271. bounding box specified by ``anchor`` is at location (``x``,
  272. ``y``).
  273. :param x,y: The location that the canvas widget should be moved
  274. to.
  275. :param anchor: The corner or side of the canvas widget that
  276. should be moved to the specified location. ``'N'``
  277. specifies the top center; ``'NE'`` specifies the top right
  278. corner; etc.
  279. """
  280. x1, y1, x2, y2 = self.bbox()
  281. if anchor == "NW":
  282. self.move(x - x1, y - y1)
  283. if anchor == "N":
  284. self.move(x - x1 / 2 - x2 / 2, y - y1)
  285. if anchor == "NE":
  286. self.move(x - x2, y - y1)
  287. if anchor == "E":
  288. self.move(x - x2, y - y1 / 2 - y2 / 2)
  289. if anchor == "SE":
  290. self.move(x - x2, y - y2)
  291. if anchor == "S":
  292. self.move(x - x1 / 2 - x2 / 2, y - y2)
  293. if anchor == "SW":
  294. self.move(x - x1, y - y2)
  295. if anchor == "W":
  296. self.move(x - x1, y - y1 / 2 - y2 / 2)
  297. def destroy(self):
  298. """
  299. Remove this ``CanvasWidget`` from its ``Canvas``. After a
  300. ``CanvasWidget`` has been destroyed, it should not be accessed.
  301. Note that you only need to destroy a top-level
  302. ``CanvasWidget``; its child widgets will be destroyed
  303. automatically. If you destroy a non-top-level
  304. ``CanvasWidget``, then the entire top-level widget will be
  305. destroyed.
  306. :raise ValueError: if this ``CanvasWidget`` has a parent.
  307. :rtype: None
  308. """
  309. if self.__parent is not None:
  310. self.__parent.destroy()
  311. return
  312. for tag in self.tags():
  313. self.__canvas.tag_unbind(tag, "<ButtonPress-1>")
  314. self.__canvas.tag_unbind(tag, "<ButtonPress-2>")
  315. self.__canvas.tag_unbind(tag, "<ButtonPress-3>")
  316. self.__canvas.delete(*self.tags())
  317. self.__canvas = None
  318. def update(self, child):
  319. """
  320. Update the graphical display of this canvas widget, and all of
  321. its ancestors, in response to a change in one of this canvas
  322. widget's children.
  323. :param child: The child widget that changed.
  324. :type child: CanvasWidget
  325. """
  326. if self.__hidden or child.__hidden:
  327. return
  328. # If we're already updating, then do nothing. This prevents
  329. # infinite loops when _update modifies its children.
  330. if self.__updating:
  331. return
  332. self.__updating = 1
  333. # Update this CanvasWidget.
  334. self._update(child)
  335. # Propagate update request to the parent.
  336. if self.__parent:
  337. self.__parent.update(self)
  338. # We're done updating.
  339. self.__updating = 0
  340. def manage(self):
  341. """
  342. Arrange this canvas widget and all of its descendants.
  343. :rtype: None
  344. """
  345. if self.__hidden:
  346. return
  347. for child in self.__children:
  348. child.manage()
  349. self._manage()
  350. def tags(self):
  351. """
  352. :return: a list of the canvas tags for all graphical
  353. elements managed by this canvas widget, including
  354. graphical elements managed by its child widgets.
  355. :rtype: list of int
  356. """
  357. if self.__canvas is None:
  358. raise ValueError("Attempt to access a destroyed canvas widget")
  359. tags = []
  360. tags += self._tags()
  361. for child in self.__children:
  362. tags += child.tags()
  363. return tags
  364. def __setitem__(self, attr, value):
  365. """
  366. Set the value of the attribute ``attr`` to ``value``. See the
  367. class documentation for a list of attributes supported by this
  368. canvas widget.
  369. :rtype: None
  370. """
  371. if attr == "draggable":
  372. self.__draggable = value
  373. else:
  374. raise ValueError("Unknown attribute %r" % attr)
  375. def __getitem__(self, attr):
  376. """
  377. :return: the value of the attribute ``attr``. See the class
  378. documentation for a list of attributes supported by this
  379. canvas widget.
  380. :rtype: (any)
  381. """
  382. if attr == "draggable":
  383. return self.__draggable
  384. else:
  385. raise ValueError("Unknown attribute %r" % attr)
  386. def __repr__(self):
  387. """
  388. :return: a string representation of this canvas widget.
  389. :rtype: str
  390. """
  391. return "<%s>" % self.__class__.__name__
  392. def hide(self):
  393. """
  394. Temporarily hide this canvas widget.
  395. :rtype: None
  396. """
  397. self.__hidden = 1
  398. for tag in self.tags():
  399. self.__canvas.itemconfig(tag, state="hidden")
  400. def show(self):
  401. """
  402. Show a hidden canvas widget.
  403. :rtype: None
  404. """
  405. self.__hidden = 0
  406. for tag in self.tags():
  407. self.__canvas.itemconfig(tag, state="normal")
  408. def hidden(self):
  409. """
  410. :return: True if this canvas widget is hidden.
  411. :rtype: bool
  412. """
  413. return self.__hidden
  414. ##//////////////////////////////////////////////////////
  415. ## Callback interface
  416. ##//////////////////////////////////////////////////////
  417. def bind_click(self, callback, button=1):
  418. """
  419. Register a new callback that will be called whenever this
  420. ``CanvasWidget`` is clicked on.
  421. :type callback: function
  422. :param callback: The callback function that will be called
  423. whenever this ``CanvasWidget`` is clicked. This function
  424. will be called with this ``CanvasWidget`` as its argument.
  425. :type button: int
  426. :param button: Which button the user should use to click on
  427. this ``CanvasWidget``. Typically, this should be 1 (left
  428. button), 3 (right button), or 2 (middle button).
  429. """
  430. self.__callbacks[button] = callback
  431. def bind_drag(self, callback):
  432. """
  433. Register a new callback that will be called after this
  434. ``CanvasWidget`` is dragged. This implicitly makes this
  435. ``CanvasWidget`` draggable.
  436. :type callback: function
  437. :param callback: The callback function that will be called
  438. whenever this ``CanvasWidget`` is clicked. This function
  439. will be called with this ``CanvasWidget`` as its argument.
  440. """
  441. self.__draggable = 1
  442. self.__callbacks["drag"] = callback
  443. def unbind_click(self, button=1):
  444. """
  445. Remove a callback that was registered with ``bind_click``.
  446. :type button: int
  447. :param button: Which button the user should use to click on
  448. this ``CanvasWidget``. Typically, this should be 1 (left
  449. button), 3 (right button), or 2 (middle button).
  450. """
  451. try:
  452. del self.__callbacks[button]
  453. except:
  454. pass
  455. def unbind_drag(self):
  456. """
  457. Remove a callback that was registered with ``bind_drag``.
  458. """
  459. try:
  460. del self.__callbacks["drag"]
  461. except:
  462. pass
  463. ##//////////////////////////////////////////////////////
  464. ## Callback internals
  465. ##//////////////////////////////////////////////////////
  466. def __press_cb(self, event):
  467. """
  468. Handle a button-press event:
  469. - record the button press event in ``self.__press``
  470. - register a button-release callback.
  471. - if this CanvasWidget or any of its ancestors are
  472. draggable, then register the appropriate motion callback.
  473. """
  474. # If we're already waiting for a button release, then ignore
  475. # this new button press.
  476. if (
  477. self.__canvas.bind("<ButtonRelease-1>")
  478. or self.__canvas.bind("<ButtonRelease-2>")
  479. or self.__canvas.bind("<ButtonRelease-3>")
  480. ):
  481. return
  482. # Unbind motion (just in case; this shouldn't be necessary)
  483. self.__canvas.unbind("<Motion>")
  484. # Record the button press event.
  485. self.__press = event
  486. # If any ancestor is draggable, set up a motion callback.
  487. # (Only if they pressed button number 1)
  488. if event.num == 1:
  489. widget = self
  490. while widget is not None:
  491. if widget["draggable"]:
  492. widget.__start_drag(event)
  493. break
  494. widget = widget.parent()
  495. # Set up the button release callback.
  496. self.__canvas.bind("<ButtonRelease-%d>" % event.num, self.__release_cb)
  497. def __start_drag(self, event):
  498. """
  499. Begin dragging this object:
  500. - register a motion callback
  501. - record the drag coordinates
  502. """
  503. self.__canvas.bind("<Motion>", self.__motion_cb)
  504. self.__drag_x = event.x
  505. self.__drag_y = event.y
  506. def __motion_cb(self, event):
  507. """
  508. Handle a motion event:
  509. - move this object to the new location
  510. - record the new drag coordinates
  511. """
  512. self.move(event.x - self.__drag_x, event.y - self.__drag_y)
  513. self.__drag_x = event.x
  514. self.__drag_y = event.y
  515. def __release_cb(self, event):
  516. """
  517. Handle a release callback:
  518. - unregister motion & button release callbacks.
  519. - decide whether they clicked, dragged, or cancelled
  520. - call the appropriate handler.
  521. """
  522. # Unbind the button release & motion callbacks.
  523. self.__canvas.unbind("<ButtonRelease-%d>" % event.num)
  524. self.__canvas.unbind("<Motion>")
  525. # Is it a click or a drag?
  526. if (
  527. event.time - self.__press.time < 100
  528. and abs(event.x - self.__press.x) + abs(event.y - self.__press.y) < 5
  529. ):
  530. # Move it back, if we were dragging.
  531. if self.__draggable and event.num == 1:
  532. self.move(
  533. self.__press.x - self.__drag_x, self.__press.y - self.__drag_y
  534. )
  535. self.__click(event.num)
  536. elif event.num == 1:
  537. self.__drag()
  538. self.__press = None
  539. def __drag(self):
  540. """
  541. If this ``CanvasWidget`` has a drag callback, then call it;
  542. otherwise, find the closest ancestor with a drag callback, and
  543. call it. If no ancestors have a drag callback, do nothing.
  544. """
  545. if self.__draggable:
  546. if "drag" in self.__callbacks:
  547. cb = self.__callbacks["drag"]
  548. try:
  549. cb(self)
  550. except:
  551. print("Error in drag callback for %r" % self)
  552. elif self.__parent is not None:
  553. self.__parent.__drag()
  554. def __click(self, button):
  555. """
  556. If this ``CanvasWidget`` has a drag callback, then call it;
  557. otherwise, find the closest ancestor with a click callback, and
  558. call it. If no ancestors have a click callback, do nothing.
  559. """
  560. if button in self.__callbacks:
  561. cb = self.__callbacks[button]
  562. # try:
  563. cb(self)
  564. # except:
  565. # print('Error in click callback for %r' % self)
  566. # raise
  567. elif self.__parent is not None:
  568. self.__parent.__click(button)
  569. ##//////////////////////////////////////////////////////
  570. ## Child/parent Handling
  571. ##//////////////////////////////////////////////////////
  572. def _add_child_widget(self, child):
  573. """
  574. Register a hierarchical child widget. The child will be
  575. considered part of this canvas widget for purposes of user
  576. interaction. ``_add_child_widget`` has two direct effects:
  577. - It sets ``child``'s parent to this canvas widget.
  578. - It adds ``child`` to the list of canvas widgets returned by
  579. the ``child_widgets`` member function.
  580. :param child: The new child widget. ``child`` must not already
  581. have a parent.
  582. :type child: CanvasWidget
  583. """
  584. if not hasattr(self, "_CanvasWidget__children"):
  585. self.__children = []
  586. if child.__parent is not None:
  587. raise ValueError("{} already has a parent".format(child))
  588. child.__parent = self
  589. self.__children.append(child)
  590. def _remove_child_widget(self, child):
  591. """
  592. Remove a hierarchical child widget. This child will no longer
  593. be considered part of this canvas widget for purposes of user
  594. interaction. ``_add_child_widget`` has two direct effects:
  595. - It sets ``child``'s parent to None.
  596. - It removes ``child`` from the list of canvas widgets
  597. returned by the ``child_widgets`` member function.
  598. :param child: The child widget to remove. ``child`` must be a
  599. child of this canvas widget.
  600. :type child: CanvasWidget
  601. """
  602. self.__children.remove(child)
  603. child.__parent = None
  604. ##//////////////////////////////////////////////////////
  605. ## Defined by subclass
  606. ##//////////////////////////////////////////////////////
  607. @abstractmethod
  608. def _tags(self):
  609. """
  610. :return: a list of canvas tags for all graphical elements
  611. managed by this canvas widget, not including graphical
  612. elements managed by its child widgets.
  613. :rtype: list of int
  614. """
  615. def _manage(self):
  616. """
  617. Arrange the child widgets of this canvas widget. This method
  618. is called when the canvas widget is initially created. It is
  619. also called if the user calls the ``manage`` method on this
  620. canvas widget or any of its ancestors.
  621. :rtype: None
  622. """
  623. def _update(self, child):
  624. """
  625. Update this canvas widget in response to a change in one of
  626. its children.
  627. :param child: The child that changed.
  628. :type child: CanvasWidget
  629. :rtype: None
  630. """
  631. ##//////////////////////////////////////////////////////
  632. ## Basic widgets.
  633. ##//////////////////////////////////////////////////////
  634. class TextWidget(CanvasWidget):
  635. """
  636. A canvas widget that displays a single string of text.
  637. Attributes:
  638. - ``color``: the color of the text.
  639. - ``font``: the font used to display the text.
  640. - ``justify``: justification for multi-line texts. Valid values
  641. are ``left``, ``center``, and ``right``.
  642. - ``width``: the width of the text. If the text is wider than
  643. this width, it will be line-wrapped at whitespace.
  644. - ``draggable``: whether the text can be dragged by the user.
  645. """
  646. def __init__(self, canvas, text, **attribs):
  647. """
  648. Create a new text widget.
  649. :type canvas: Tkinter.Canvas
  650. :param canvas: This canvas widget's canvas.
  651. :type text: str
  652. :param text: The string of text to display.
  653. :param attribs: The new canvas widget's attributes.
  654. """
  655. self._text = text
  656. self._tag = canvas.create_text(1, 1, text=text)
  657. CanvasWidget.__init__(self, canvas, **attribs)
  658. def __setitem__(self, attr, value):
  659. if attr in ("color", "font", "justify", "width"):
  660. if attr == "color":
  661. attr = "fill"
  662. self.canvas().itemconfig(self._tag, {attr: value})
  663. else:
  664. CanvasWidget.__setitem__(self, attr, value)
  665. def __getitem__(self, attr):
  666. if attr == "width":
  667. return int(self.canvas().itemcget(self._tag, attr))
  668. elif attr in ("color", "font", "justify"):
  669. if attr == "color":
  670. attr = "fill"
  671. return self.canvas().itemcget(self._tag, attr)
  672. else:
  673. return CanvasWidget.__getitem__(self, attr)
  674. def _tags(self):
  675. return [self._tag]
  676. def text(self):
  677. """
  678. :return: The text displayed by this text widget.
  679. :rtype: str
  680. """
  681. return self.canvas().itemcget(self._tag, "TEXT")
  682. def set_text(self, text):
  683. """
  684. Change the text that is displayed by this text widget.
  685. :type text: str
  686. :param text: The string of text to display.
  687. :rtype: None
  688. """
  689. self.canvas().itemconfig(self._tag, text=text)
  690. if self.parent() is not None:
  691. self.parent().update(self)
  692. def __repr__(self):
  693. return "[Text: %r]" % self._text
  694. class SymbolWidget(TextWidget):
  695. """
  696. A canvas widget that displays special symbols, such as the
  697. negation sign and the exists operator. Symbols are specified by
  698. name. Currently, the following symbol names are defined: ``neg``,
  699. ``disj``, ``conj``, ``lambda``, ``merge``, ``forall``, ``exists``,
  700. ``subseteq``, ``subset``, ``notsubset``, ``emptyset``, ``imp``,
  701. ``rightarrow``, ``equal``, ``notequal``, ``epsilon``.
  702. Attributes:
  703. - ``color``: the color of the text.
  704. - ``draggable``: whether the text can be dragged by the user.
  705. :cvar SYMBOLS: A dictionary mapping from symbols to the character
  706. in the ``symbol`` font used to render them.
  707. """
  708. SYMBOLS = {
  709. "neg": "\330",
  710. "disj": "\332",
  711. "conj": "\331",
  712. "lambda": "\154",
  713. "merge": "\304",
  714. "forall": "\042",
  715. "exists": "\044",
  716. "subseteq": "\315",
  717. "subset": "\314",
  718. "notsubset": "\313",
  719. "emptyset": "\306",
  720. "imp": "\336",
  721. "rightarrow": chr(222), #'\256',
  722. "equal": "\75",
  723. "notequal": "\271",
  724. "intersection": "\307",
  725. "union": "\310",
  726. "epsilon": "e",
  727. }
  728. def __init__(self, canvas, symbol, **attribs):
  729. """
  730. Create a new symbol widget.
  731. :type canvas: Tkinter.Canvas
  732. :param canvas: This canvas widget's canvas.
  733. :type symbol: str
  734. :param symbol: The name of the symbol to display.
  735. :param attribs: The new canvas widget's attributes.
  736. """
  737. attribs["font"] = "symbol"
  738. TextWidget.__init__(self, canvas, "", **attribs)
  739. self.set_symbol(symbol)
  740. def symbol(self):
  741. """
  742. :return: the name of the symbol that is displayed by this
  743. symbol widget.
  744. :rtype: str
  745. """
  746. return self._symbol
  747. def set_symbol(self, symbol):
  748. """
  749. Change the symbol that is displayed by this symbol widget.
  750. :type symbol: str
  751. :param symbol: The name of the symbol to display.
  752. """
  753. if symbol not in SymbolWidget.SYMBOLS:
  754. raise ValueError("Unknown symbol: %s" % symbol)
  755. self._symbol = symbol
  756. self.set_text(SymbolWidget.SYMBOLS[symbol])
  757. def __repr__(self):
  758. return "[Symbol: %r]" % self._symbol
  759. @staticmethod
  760. def symbolsheet(size=20):
  761. """
  762. Open a new Tkinter window that displays the entire alphabet
  763. for the symbol font. This is useful for constructing the
  764. ``SymbolWidget.SYMBOLS`` dictionary.
  765. """
  766. top = Tk()
  767. def destroy(e, top=top):
  768. top.destroy()
  769. top.bind("q", destroy)
  770. Button(top, text="Quit", command=top.destroy).pack(side="bottom")
  771. text = Text(top, font=("helvetica", -size), width=20, height=30)
  772. text.pack(side="left")
  773. sb = Scrollbar(top, command=text.yview)
  774. text["yscrollcommand"] = sb.set
  775. sb.pack(side="right", fill="y")
  776. text.tag_config("symbol", font=("symbol", -size))
  777. for i in range(256):
  778. if i in (0, 10):
  779. continue # null and newline
  780. for k, v in list(SymbolWidget.SYMBOLS.items()):
  781. if v == chr(i):
  782. text.insert("end", "%-10s\t" % k)
  783. break
  784. else:
  785. text.insert("end", "%-10d \t" % i)
  786. text.insert("end", "[%s]\n" % chr(i), "symbol")
  787. top.mainloop()
  788. class AbstractContainerWidget(CanvasWidget):
  789. """
  790. An abstract class for canvas widgets that contain a single child,
  791. such as ``BoxWidget`` and ``OvalWidget``. Subclasses must define
  792. a constructor, which should create any new graphical elements and
  793. then call the ``AbstractCanvasContainer`` constructor. Subclasses
  794. must also define the ``_update`` method and the ``_tags`` method;
  795. and any subclasses that define attributes should define
  796. ``__setitem__`` and ``__getitem__``.
  797. """
  798. def __init__(self, canvas, child, **attribs):
  799. """
  800. Create a new container widget. This constructor should only
  801. be called by subclass constructors.
  802. :type canvas: Tkinter.Canvas
  803. :param canvas: This canvas widget's canvas.
  804. :param child: The container's child widget. ``child`` must not
  805. have a parent.
  806. :type child: CanvasWidget
  807. :param attribs: The new canvas widget's attributes.
  808. """
  809. self._child = child
  810. self._add_child_widget(child)
  811. CanvasWidget.__init__(self, canvas, **attribs)
  812. def _manage(self):
  813. self._update(self._child)
  814. def child(self):
  815. """
  816. :return: The child widget contained by this container widget.
  817. :rtype: CanvasWidget
  818. """
  819. return self._child
  820. def set_child(self, child):
  821. """
  822. Change the child widget contained by this container widget.
  823. :param child: The new child widget. ``child`` must not have a
  824. parent.
  825. :type child: CanvasWidget
  826. :rtype: None
  827. """
  828. self._remove_child_widget(self._child)
  829. self._add_child_widget(child)
  830. self._child = child
  831. self.update(child)
  832. def __repr__(self):
  833. name = self.__class__.__name__
  834. if name[-6:] == "Widget":
  835. name = name[:-6]
  836. return "[%s: %r]" % (name, self._child)
  837. class BoxWidget(AbstractContainerWidget):
  838. """
  839. A canvas widget that places a box around a child widget.
  840. Attributes:
  841. - ``fill``: The color used to fill the interior of the box.
  842. - ``outline``: The color used to draw the outline of the box.
  843. - ``width``: The width of the outline of the box.
  844. - ``margin``: The number of pixels space left between the child
  845. and the box.
  846. - ``draggable``: whether the text can be dragged by the user.
  847. """
  848. def __init__(self, canvas, child, **attribs):
  849. """
  850. Create a new box widget.
  851. :type canvas: Tkinter.Canvas
  852. :param canvas: This canvas widget's canvas.
  853. :param child: The child widget. ``child`` must not have a
  854. parent.
  855. :type child: CanvasWidget
  856. :param attribs: The new canvas widget's attributes.
  857. """
  858. self._child = child
  859. self._margin = 1
  860. self._box = canvas.create_rectangle(1, 1, 1, 1)
  861. canvas.tag_lower(self._box)
  862. AbstractContainerWidget.__init__(self, canvas, child, **attribs)
  863. def __setitem__(self, attr, value):
  864. if attr == "margin":
  865. self._margin = value
  866. elif attr in ("outline", "fill", "width"):
  867. self.canvas().itemconfig(self._box, {attr: value})
  868. else:
  869. CanvasWidget.__setitem__(self, attr, value)
  870. def __getitem__(self, attr):
  871. if attr == "margin":
  872. return self._margin
  873. elif attr == "width":
  874. return float(self.canvas().itemcget(self._box, attr))
  875. elif attr in ("outline", "fill", "width"):
  876. return self.canvas().itemcget(self._box, attr)
  877. else:
  878. return CanvasWidget.__getitem__(self, attr)
  879. def _update(self, child):
  880. (x1, y1, x2, y2) = child.bbox()
  881. margin = self._margin + self["width"] / 2
  882. self.canvas().coords(
  883. self._box, x1 - margin, y1 - margin, x2 + margin, y2 + margin
  884. )
  885. def _tags(self):
  886. return [self._box]
  887. class OvalWidget(AbstractContainerWidget):
  888. """
  889. A canvas widget that places a oval around a child widget.
  890. Attributes:
  891. - ``fill``: The color used to fill the interior of the oval.
  892. - ``outline``: The color used to draw the outline of the oval.
  893. - ``width``: The width of the outline of the oval.
  894. - ``margin``: The number of pixels space left between the child
  895. and the oval.
  896. - ``draggable``: whether the text can be dragged by the user.
  897. - ``double``: If true, then a double-oval is drawn.
  898. """
  899. def __init__(self, canvas, child, **attribs):
  900. """
  901. Create a new oval widget.
  902. :type canvas: Tkinter.Canvas
  903. :param canvas: This canvas widget's canvas.
  904. :param child: The child widget. ``child`` must not have a
  905. parent.
  906. :type child: CanvasWidget
  907. :param attribs: The new canvas widget's attributes.
  908. """
  909. self._child = child
  910. self._margin = 1
  911. self._oval = canvas.create_oval(1, 1, 1, 1)
  912. self._circle = attribs.pop("circle", False)
  913. self._double = attribs.pop("double", False)
  914. if self._double:
  915. self._oval2 = canvas.create_oval(1, 1, 1, 1)
  916. else:
  917. self._oval2 = None
  918. canvas.tag_lower(self._oval)
  919. AbstractContainerWidget.__init__(self, canvas, child, **attribs)
  920. def __setitem__(self, attr, value):
  921. c = self.canvas()
  922. if attr == "margin":
  923. self._margin = value
  924. elif attr == "double":
  925. if value == True and self._oval2 is None:
  926. # Copy attributes & position from self._oval.
  927. x1, y1, x2, y2 = c.bbox(self._oval)
  928. w = self["width"] * 2
  929. self._oval2 = c.create_oval(
  930. x1 - w,
  931. y1 - w,
  932. x2 + w,
  933. y2 + w,
  934. outline=c.itemcget(self._oval, "outline"),
  935. width=c.itemcget(self._oval, "width"),
  936. )
  937. c.tag_lower(self._oval2)
  938. if value == False and self._oval2 is not None:
  939. c.delete(self._oval2)
  940. self._oval2 = None
  941. elif attr in ("outline", "fill", "width"):
  942. c.itemconfig(self._oval, {attr: value})
  943. if self._oval2 is not None and attr != "fill":
  944. c.itemconfig(self._oval2, {attr: value})
  945. if self._oval2 is not None and attr != "fill":
  946. self.canvas().itemconfig(self._oval2, {attr: value})
  947. else:
  948. CanvasWidget.__setitem__(self, attr, value)
  949. def __getitem__(self, attr):
  950. if attr == "margin":
  951. return self._margin
  952. elif attr == "double":
  953. return self._double is not None
  954. elif attr == "width":
  955. return float(self.canvas().itemcget(self._oval, attr))
  956. elif attr in ("outline", "fill", "width"):
  957. return self.canvas().itemcget(self._oval, attr)
  958. else:
  959. return CanvasWidget.__getitem__(self, attr)
  960. # The ratio between inscribed & circumscribed ovals
  961. RATIO = 1.4142135623730949
  962. def _update(self, child):
  963. R = OvalWidget.RATIO
  964. (x1, y1, x2, y2) = child.bbox()
  965. margin = self._margin
  966. # If we're a circle, pretend our contents are square.
  967. if self._circle:
  968. dx, dy = abs(x1 - x2), abs(y1 - y2)
  969. if dx > dy:
  970. y = (y1 + y2) / 2
  971. y1, y2 = y - dx / 2, y + dx / 2
  972. elif dy > dx:
  973. x = (x1 + x2) / 2
  974. x1, x2 = x - dy / 2, x + dy / 2
  975. # Find the four corners.
  976. left = int((x1 * (1 + R) + x2 * (1 - R)) / 2)
  977. right = left + int((x2 - x1) * R)
  978. top = int((y1 * (1 + R) + y2 * (1 - R)) / 2)
  979. bot = top + int((y2 - y1) * R)
  980. self.canvas().coords(
  981. self._oval, left - margin, top - margin, right + margin, bot + margin
  982. )
  983. if self._oval2 is not None:
  984. self.canvas().coords(
  985. self._oval2,
  986. left - margin + 2,
  987. top - margin + 2,
  988. right + margin - 2,
  989. bot + margin - 2,
  990. )
  991. def _tags(self):
  992. if self._oval2 is None:
  993. return [self._oval]
  994. else:
  995. return [self._oval, self._oval2]
  996. class ParenWidget(AbstractContainerWidget):
  997. """
  998. A canvas widget that places a pair of parenthases around a child
  999. widget.
  1000. Attributes:
  1001. - ``color``: The color used to draw the parenthases.
  1002. - ``width``: The width of the parenthases.
  1003. - ``draggable``: whether the text can be dragged by the user.
  1004. """
  1005. def __init__(self, canvas, child, **attribs):
  1006. """
  1007. Create a new parenthasis widget.
  1008. :type canvas: Tkinter.Canvas
  1009. :param canvas: This canvas widget's canvas.
  1010. :param child: The child widget. ``child`` must not have a
  1011. parent.
  1012. :type child: CanvasWidget
  1013. :param attribs: The new canvas widget's attributes.
  1014. """
  1015. self._child = child
  1016. self._oparen = canvas.create_arc(1, 1, 1, 1, style="arc", start=90, extent=180)
  1017. self._cparen = canvas.create_arc(1, 1, 1, 1, style="arc", start=-90, extent=180)
  1018. AbstractContainerWidget.__init__(self, canvas, child, **attribs)
  1019. def __setitem__(self, attr, value):
  1020. if attr == "color":
  1021. self.canvas().itemconfig(self._oparen, outline=value)
  1022. self.canvas().itemconfig(self._cparen, outline=value)
  1023. elif attr == "width":
  1024. self.canvas().itemconfig(self._oparen, width=value)
  1025. self.canvas().itemconfig(self._cparen, width=value)
  1026. else:
  1027. CanvasWidget.__setitem__(self, attr, value)
  1028. def __getitem__(self, attr):
  1029. if attr == "color":
  1030. return self.canvas().itemcget(self._oparen, "outline")
  1031. elif attr == "width":
  1032. return self.canvas().itemcget(self._oparen, "width")
  1033. else:
  1034. return CanvasWidget.__getitem__(self, attr)
  1035. def _update(self, child):
  1036. (x1, y1, x2, y2) = child.bbox()
  1037. width = max((y2 - y1) / 6, 4)
  1038. self.canvas().coords(self._oparen, x1 - width, y1, x1 + width, y2)
  1039. self.canvas().coords(self._cparen, x2 - width, y1, x2 + width, y2)
  1040. def _tags(self):
  1041. return [self._oparen, self._cparen]
  1042. class BracketWidget(AbstractContainerWidget):
  1043. """
  1044. A canvas widget that places a pair of brackets around a child
  1045. widget.
  1046. Attributes:
  1047. - ``color``: The color used to draw the brackets.
  1048. - ``width``: The width of the brackets.
  1049. - ``draggable``: whether the text can be dragged by the user.
  1050. """
  1051. def __init__(self, canvas, child, **attribs):
  1052. """
  1053. Create a new bracket widget.
  1054. :type canvas: Tkinter.Canvas
  1055. :param canvas: This canvas widget's canvas.
  1056. :param child: The child widget. ``child`` must not have a
  1057. parent.
  1058. :type child: CanvasWidget
  1059. :param attribs: The new canvas widget's attributes.
  1060. """
  1061. self._child = child
  1062. self._obrack = canvas.create_line(1, 1, 1, 1, 1, 1, 1, 1)
  1063. self._cbrack = canvas.create_line(1, 1, 1, 1, 1, 1, 1, 1)
  1064. AbstractContainerWidget.__init__(self, canvas, child, **attribs)
  1065. def __setitem__(self, attr, value):
  1066. if attr == "color":
  1067. self.canvas().itemconfig(self._obrack, fill=value)
  1068. self.canvas().itemconfig(self._cbrack, fill=value)
  1069. elif attr == "width":
  1070. self.canvas().itemconfig(self._obrack, width=value)
  1071. self.canvas().itemconfig(self._cbrack, width=value)
  1072. else:
  1073. CanvasWidget.__setitem__(self, attr, value)
  1074. def __getitem__(self, attr):
  1075. if attr == "color":
  1076. return self.canvas().itemcget(self._obrack, "outline")
  1077. elif attr == "width":
  1078. return self.canvas().itemcget(self._obrack, "width")
  1079. else:
  1080. return CanvasWidget.__getitem__(self, attr)
  1081. def _update(self, child):
  1082. (x1, y1, x2, y2) = child.bbox()
  1083. width = max((y2 - y1) / 8, 2)
  1084. self.canvas().coords(
  1085. self._obrack, x1, y1, x1 - width, y1, x1 - width, y2, x1, y2
  1086. )
  1087. self.canvas().coords(
  1088. self._cbrack, x2, y1, x2 + width, y1, x2 + width, y2, x2, y2
  1089. )
  1090. def _tags(self):
  1091. return [self._obrack, self._cbrack]
  1092. class SequenceWidget(CanvasWidget):
  1093. """
  1094. A canvas widget that keeps a list of canvas widgets in a
  1095. horizontal line.
  1096. Attributes:
  1097. - ``align``: The vertical alignment of the children. Possible
  1098. values are ``'top'``, ``'center'``, and ``'bottom'``. By
  1099. default, children are center-aligned.
  1100. - ``space``: The amount of horizontal space to place between
  1101. children. By default, one pixel of space is used.
  1102. - ``ordered``: If true, then keep the children in their
  1103. original order.
  1104. """
  1105. def __init__(self, canvas, *children, **attribs):
  1106. """
  1107. Create a new sequence widget.
  1108. :type canvas: Tkinter.Canvas
  1109. :param canvas: This canvas widget's canvas.
  1110. :param children: The widgets that should be aligned
  1111. horizontally. Each child must not have a parent.
  1112. :type children: list(CanvasWidget)
  1113. :param attribs: The new canvas widget's attributes.
  1114. """
  1115. self._align = "center"
  1116. self._space = 1
  1117. self._ordered = False
  1118. self._children = list(children)
  1119. for child in children:
  1120. self._add_child_widget(child)
  1121. CanvasWidget.__init__(self, canvas, **attribs)
  1122. def __setitem__(self, attr, value):
  1123. if attr == "align":
  1124. if value not in ("top", "bottom", "center"):
  1125. raise ValueError("Bad alignment: %r" % value)
  1126. self._align = value
  1127. elif attr == "space":
  1128. self._space = value
  1129. elif attr == "ordered":
  1130. self._ordered = value
  1131. else:
  1132. CanvasWidget.__setitem__(self, attr, value)
  1133. def __getitem__(self, attr):
  1134. if attr == "align":
  1135. return self._align
  1136. elif attr == "space":
  1137. return self._space
  1138. elif attr == "ordered":
  1139. return self._ordered
  1140. else:
  1141. return CanvasWidget.__getitem__(self, attr)
  1142. def _tags(self):
  1143. return []
  1144. def _yalign(self, top, bot):
  1145. if self._align == "top":
  1146. return top
  1147. if self._align == "bottom":
  1148. return bot
  1149. if self._align == "center":
  1150. return (top + bot) / 2
  1151. def _update(self, child):
  1152. # Align all children with child.
  1153. (left, top, right, bot) = child.bbox()
  1154. y = self._yalign(top, bot)
  1155. for c in self._children:
  1156. (x1, y1, x2, y2) = c.bbox()
  1157. c.move(0, y - self._yalign(y1, y2))
  1158. if self._ordered and len(self._children) > 1:
  1159. index = self._children.index(child)
  1160. x = right + self._space
  1161. for i in range(index + 1, len(self._children)):
  1162. (x1, y1, x2, y2) = self._children[i].bbox()
  1163. if x > x1:
  1164. self._children[i].move(x - x1, 0)
  1165. x += x2 - x1 + self._space
  1166. x = left - self._space
  1167. for i in range(index - 1, -1, -1):
  1168. (x1, y1, x2, y2) = self._children[i].bbox()
  1169. if x < x2:
  1170. self._children[i].move(x - x2, 0)
  1171. x -= x2 - x1 + self._space
  1172. def _manage(self):
  1173. if len(self._children) == 0:
  1174. return
  1175. child = self._children[0]
  1176. # Align all children with child.
  1177. (left, top, right, bot) = child.bbox()
  1178. y = self._yalign(top, bot)
  1179. index = self._children.index(child)
  1180. # Line up children to the right of child.
  1181. x = right + self._space
  1182. for i in range(index + 1, len(self._children)):
  1183. (x1, y1, x2, y2) = self._children[i].bbox()
  1184. self._children[i].move(x - x1, y - self._yalign(y1, y2))
  1185. x += x2 - x1 + self._space
  1186. # Line up children to the left of child.
  1187. x = left - self._space
  1188. for i in range(index - 1, -1, -1):
  1189. (x1, y1, x2, y2) = self._children[i].bbox()
  1190. self._children[i].move(x - x2, y - self._yalign(y1, y2))
  1191. x -= x2 - x1 + self._space
  1192. def __repr__(self):
  1193. return "[Sequence: " + repr(self._children)[1:-1] + "]"
  1194. # Provide an alias for the child_widgets() member.
  1195. children = CanvasWidget.child_widgets
  1196. def replace_child(self, oldchild, newchild):
  1197. """
  1198. Replace the child canvas widget ``oldchild`` with ``newchild``.
  1199. ``newchild`` must not have a parent. ``oldchild``'s parent will
  1200. be set to None.
  1201. :type oldchild: CanvasWidget
  1202. :param oldchild: The child canvas widget to remove.
  1203. :type newchild: CanvasWidget
  1204. :param newchild: The canvas widget that should replace
  1205. ``oldchild``.
  1206. """
  1207. index = self._children.index(oldchild)
  1208. self._children[index] = newchild
  1209. self._remove_child_widget(oldchild)
  1210. self._add_child_widget(newchild)
  1211. self.update(newchild)
  1212. def remove_child(self, child):
  1213. """
  1214. Remove the given child canvas widget. ``child``'s parent will
  1215. be set ot None.
  1216. :type child: CanvasWidget
  1217. :param child: The child canvas widget to remove.
  1218. """
  1219. index = self._children.index(child)
  1220. del self._children[index]
  1221. self._remove_child_widget(child)
  1222. if len(self._children) > 0:
  1223. self.update(self._children[0])
  1224. def insert_child(self, index, child):
  1225. """
  1226. Insert a child canvas widget before a given index.
  1227. :type child: CanvasWidget
  1228. :param child: The canvas widget that should be inserted.
  1229. :type index: int
  1230. :param index: The index where the child widget should be
  1231. inserted. In particular, the index of ``child`` will be
  1232. ``index``; and the index of any children whose indices were
  1233. greater than equal to ``index`` before ``child`` was
  1234. inserted will be incremented by one.
  1235. """
  1236. self._children.insert(index, child)
  1237. self._add_child_widget(child)
  1238. class StackWidget(CanvasWidget):
  1239. """
  1240. A canvas widget that keeps a list of canvas widgets in a vertical
  1241. line.
  1242. Attributes:
  1243. - ``align``: The horizontal alignment of the children. Possible
  1244. values are ``'left'``, ``'center'``, and ``'right'``. By
  1245. default, children are center-aligned.
  1246. - ``space``: The amount of vertical space to place between
  1247. children. By default, one pixel of space is used.
  1248. - ``ordered``: If true, then keep the children in their
  1249. original order.
  1250. """
  1251. def __init__(self, canvas, *children, **attribs):
  1252. """
  1253. Create a new stack widget.
  1254. :type canvas: Tkinter.Canvas
  1255. :param canvas: This canvas widget's canvas.
  1256. :param children: The widgets that should be aligned
  1257. vertically. Each child must not have a parent.
  1258. :type children: list(CanvasWidget)
  1259. :param attribs: The new canvas widget's attributes.
  1260. """
  1261. self._align = "center"
  1262. self._space = 1
  1263. self._ordered = False
  1264. self._children = list(children)
  1265. for child in children:
  1266. self._add_child_widget(child)
  1267. CanvasWidget.__init__(self, canvas, **attribs)
  1268. def __setitem__(self, attr, value):
  1269. if attr == "align":
  1270. if value not in ("left", "right", "center"):
  1271. raise ValueError("Bad alignment: %r" % value)
  1272. self._align = value
  1273. elif attr == "space":
  1274. self._space = value
  1275. elif attr == "ordered":
  1276. self._ordered = value
  1277. else:
  1278. CanvasWidget.__setitem__(self, attr, value)
  1279. def __getitem__(self, attr):
  1280. if attr == "align":
  1281. return self._align
  1282. elif attr == "space":
  1283. return self._space
  1284. elif attr == "ordered":
  1285. return self._ordered
  1286. else:
  1287. return CanvasWidget.__getitem__(self, attr)
  1288. def _tags(self):
  1289. return []
  1290. def _xalign(self, left, right):
  1291. if self._align == "left":
  1292. return left
  1293. if self._align == "right":
  1294. return right
  1295. if self._align == "center":
  1296. return (left + right) / 2
  1297. def _update(self, child):
  1298. # Align all children with child.
  1299. (left, top, right, bot) = child.bbox()
  1300. x = self._xalign(left, right)
  1301. for c in self._children:
  1302. (x1, y1, x2, y2) = c.bbox()
  1303. c.move(x - self._xalign(x1, x2), 0)
  1304. if self._ordered and len(self._children) > 1:
  1305. index = self._children.index(child)
  1306. y = bot + self._space
  1307. for i in range(index + 1, len(self._children)):
  1308. (x1, y1, x2, y2) = self._children[i].bbox()
  1309. if y > y1:
  1310. self._children[i].move(0, y - y1)
  1311. y += y2 - y1 + self._space
  1312. y = top - self._space
  1313. for i in range(index - 1, -1, -1):
  1314. (x1, y1, x2, y2) = self._children[i].bbox()
  1315. if y < y2:
  1316. self._children[i].move(0, y - y2)
  1317. y -= y2 - y1 + self._space
  1318. def _manage(self):
  1319. if len(self._children) == 0:
  1320. return
  1321. child = self._children[0]
  1322. # Align all children with child.
  1323. (left, top, right, bot) = child.bbox()
  1324. x = self._xalign(left, right)
  1325. index = self._children.index(child)
  1326. # Line up children below the child.
  1327. y = bot + self._space
  1328. for i in range(index + 1, len(self._children)):
  1329. (x1, y1, x2, y2) = self._children[i].bbox()
  1330. self._children[i].move(x - self._xalign(x1, x2), y - y1)
  1331. y += y2 - y1 + self._space
  1332. # Line up children above the child.
  1333. y = top - self._space
  1334. for i in range(index - 1, -1, -1):
  1335. (x1, y1, x2, y2) = self._children[i].bbox()
  1336. self._children[i].move(x - self._xalign(x1, x2), y - y2)
  1337. y -= y2 - y1 + self._space
  1338. def __repr__(self):
  1339. return "[Stack: " + repr(self._children)[1:-1] + "]"
  1340. # Provide an alias for the child_widgets() member.
  1341. children = CanvasWidget.child_widgets
  1342. def replace_child(self, oldchild, newchild):
  1343. """
  1344. Replace the child canvas widget ``oldchild`` with ``newchild``.
  1345. ``newchild`` must not have a parent. ``oldchild``'s parent will
  1346. be set to None.
  1347. :type oldchild: CanvasWidget
  1348. :param oldchild: The child canvas widget to remove.
  1349. :type newchild: CanvasWidget
  1350. :param newchild: The canvas widget that should replace
  1351. ``oldchild``.
  1352. """
  1353. index = self._children.index(oldchild)
  1354. self._children[index] = newchild
  1355. self._remove_child_widget(oldchild)
  1356. self._add_child_widget(newchild)
  1357. self.update(newchild)
  1358. def remove_child(self, child):
  1359. """
  1360. Remove the given child canvas widget. ``child``'s parent will
  1361. be set ot None.
  1362. :type child: CanvasWidget
  1363. :param child: The child canvas widget to remove.
  1364. """
  1365. index = self._children.index(child)
  1366. del self._children[index]
  1367. self._remove_child_widget(child)
  1368. if len(self._children) > 0:
  1369. self.update(self._children[0])
  1370. def insert_child(self, index, child):
  1371. """
  1372. Insert a child canvas widget before a given index.
  1373. :type child: CanvasWidget
  1374. :param child: The canvas widget that should be inserted.
  1375. :type index: int
  1376. :param index: The index where the child widget should be
  1377. inserted. In particular, the index of ``child`` will be
  1378. ``index``; and the index of any children whose indices were
  1379. greater than equal to ``index`` before ``child`` was
  1380. inserted will be incremented by one.
  1381. """
  1382. self._children.insert(index, child)
  1383. self._add_child_widget(child)
  1384. class SpaceWidget(CanvasWidget):
  1385. """
  1386. A canvas widget that takes up space but does not display
  1387. anything. A ``SpaceWidget`` can be used to add space between
  1388. elements. Each space widget is characterized by a width and a
  1389. height. If you wish to only create horizontal space, then use a
  1390. height of zero; and if you wish to only create vertical space, use
  1391. a width of zero.
  1392. """
  1393. def __init__(self, canvas, width, height, **attribs):
  1394. """
  1395. Create a new space widget.
  1396. :type canvas: Tkinter.Canvas
  1397. :param canvas: This canvas widget's canvas.
  1398. :type width: int
  1399. :param width: The width of the new space widget.
  1400. :type height: int
  1401. :param height: The height of the new space widget.
  1402. :param attribs: The new canvas widget's attributes.
  1403. """
  1404. # For some reason,
  1405. if width > 4:
  1406. width -= 4
  1407. if height > 4:
  1408. height -= 4
  1409. self._tag = canvas.create_line(1, 1, width, height, fill="")
  1410. CanvasWidget.__init__(self, canvas, **attribs)
  1411. # note: width() and height() are already defined by CanvasWidget.
  1412. def set_width(self, width):
  1413. """
  1414. Change the width of this space widget.
  1415. :param width: The new width.
  1416. :type width: int
  1417. :rtype: None
  1418. """
  1419. [x1, y1, x2, y2] = self.bbox()
  1420. self.canvas().coords(self._tag, x1, y1, x1 + width, y2)
  1421. def set_height(self, height):
  1422. """
  1423. Change the height of this space widget.
  1424. :param height: The new height.
  1425. :type height: int
  1426. :rtype: None
  1427. """
  1428. [x1, y1, x2, y2] = self.bbox()
  1429. self.canvas().coords(self._tag, x1, y1, x2, y1 + height)
  1430. def _tags(self):
  1431. return [self._tag]
  1432. def __repr__(self):
  1433. return "[Space]"
  1434. class ScrollWatcherWidget(CanvasWidget):
  1435. """
  1436. A special canvas widget that adjusts its ``Canvas``'s scrollregion
  1437. to always include the bounding boxes of all of its children. The
  1438. scroll-watcher widget will only increase the size of the
  1439. ``Canvas``'s scrollregion; it will never decrease it.
  1440. """
  1441. def __init__(self, canvas, *children, **attribs):
  1442. """
  1443. Create a new scroll-watcher widget.
  1444. :type canvas: Tkinter.Canvas
  1445. :param canvas: This canvas widget's canvas.
  1446. :type children: list(CanvasWidget)
  1447. :param children: The canvas widgets watched by the
  1448. scroll-watcher. The scroll-watcher will ensure that these
  1449. canvas widgets are always contained in their canvas's
  1450. scrollregion.
  1451. :param attribs: The new canvas widget's attributes.
  1452. """
  1453. for child in children:
  1454. self._add_child_widget(child)
  1455. CanvasWidget.__init__(self, canvas, **attribs)
  1456. def add_child(self, canvaswidget):
  1457. """
  1458. Add a new canvas widget to the scroll-watcher. The
  1459. scroll-watcher will ensure that the new canvas widget is
  1460. always contained in its canvas's scrollregion.
  1461. :param canvaswidget: The new canvas widget.
  1462. :type canvaswidget: CanvasWidget
  1463. :rtype: None
  1464. """
  1465. self._add_child_widget(canvaswidget)
  1466. self.update(canvaswidget)
  1467. def remove_child(self, canvaswidget):
  1468. """
  1469. Remove a canvas widget from the scroll-watcher. The
  1470. scroll-watcher will no longer ensure that the new canvas
  1471. widget is always contained in its canvas's scrollregion.
  1472. :param canvaswidget: The canvas widget to remove.
  1473. :type canvaswidget: CanvasWidget
  1474. :rtype: None
  1475. """
  1476. self._remove_child_widget(canvaswidget)
  1477. def _tags(self):
  1478. return []
  1479. def _update(self, child):
  1480. self._adjust_scrollregion()
  1481. def _adjust_scrollregion(self):
  1482. """
  1483. Adjust the scrollregion of this scroll-watcher's ``Canvas`` to
  1484. include the bounding boxes of all of its children.
  1485. """
  1486. bbox = self.bbox()
  1487. canvas = self.canvas()
  1488. scrollregion = [int(n) for n in canvas["scrollregion"].split()]
  1489. if len(scrollregion) != 4:
  1490. return
  1491. if (
  1492. bbox[0] < scrollregion[0]
  1493. or bbox[1] < scrollregion[1]
  1494. or bbox[2] > scrollregion[2]
  1495. or bbox[3] > scrollregion[3]
  1496. ):
  1497. scrollregion = "%d %d %d %d" % (
  1498. min(bbox[0], scrollregion[0]),
  1499. min(bbox[1], scrollregion[1]),
  1500. max(bbox[2], scrollregion[2]),
  1501. max(bbox[3], scrollregion[3]),
  1502. )
  1503. canvas["scrollregion"] = scrollregion
  1504. ##//////////////////////////////////////////////////////
  1505. ## Canvas Frame
  1506. ##//////////////////////////////////////////////////////
  1507. class CanvasFrame(object):
  1508. """
  1509. A ``Tkinter`` frame containing a canvas and scrollbars.
  1510. ``CanvasFrame`` uses a ``ScrollWatcherWidget`` to ensure that all of
  1511. the canvas widgets contained on its canvas are within its
  1512. scrollregion. In order for ``CanvasFrame`` to make these checks,
  1513. all canvas widgets must be registered with ``add_widget`` when they
  1514. are added to the canvas; and destroyed with ``destroy_widget`` when
  1515. they are no longer needed.
  1516. If a ``CanvasFrame`` is created with no parent, then it will create
  1517. its own main window, including a "Done" button and a "Print"
  1518. button.
  1519. """
  1520. def __init__(self, parent=None, **kw):
  1521. """
  1522. Create a new ``CanvasFrame``.
  1523. :type parent: Tkinter.BaseWidget or Tkinter.Tk
  1524. :param parent: The parent ``Tkinter`` widget. If no parent is
  1525. specified, then ``CanvasFrame`` will create a new main
  1526. window.
  1527. :param kw: Keyword arguments for the new ``Canvas``. See the
  1528. documentation for ``Tkinter.Canvas`` for more information.
  1529. """
  1530. # If no parent was given, set up a top-level window.
  1531. if parent is None:
  1532. self._parent = Tk()
  1533. self._parent.title("NLTK")
  1534. self._parent.bind("<Control-p>", lambda e: self.print_to_file())
  1535. self._parent.bind("<Control-x>", self.destroy)
  1536. self._parent.bind("<Control-q>", self.destroy)
  1537. else:
  1538. self._parent = parent
  1539. # Create a frame for the canvas & scrollbars
  1540. self._frame = frame = Frame(self._parent)
  1541. self._canvas = canvas = Canvas(frame, **kw)
  1542. xscrollbar = Scrollbar(self._frame, orient="horizontal")
  1543. yscrollbar = Scrollbar(self._frame, orient="vertical")
  1544. xscrollbar["command"] = canvas.xview
  1545. yscrollbar["command"] = canvas.yview
  1546. canvas["xscrollcommand"] = xscrollbar.set
  1547. canvas["yscrollcommand"] = yscrollbar.set
  1548. yscrollbar.pack(fill="y", side="right")
  1549. xscrollbar.pack(fill="x", side="bottom")
  1550. canvas.pack(expand=1, fill="both", side="left")
  1551. # Set initial scroll region.
  1552. scrollregion = "0 0 %s %s" % (canvas["width"], canvas["height"])
  1553. canvas["scrollregion"] = scrollregion
  1554. self._scrollwatcher = ScrollWatcherWidget(canvas)
  1555. # If no parent was given, pack the frame, and add a menu.
  1556. if parent is None:
  1557. self.pack(expand=1, fill="both")
  1558. self._init_menubar()
  1559. def _init_menubar(self):
  1560. menubar = Menu(self._parent)
  1561. filemenu = Menu(menubar, tearoff=0)
  1562. filemenu.add_command(
  1563. label="Print to Postscript",
  1564. underline=0,
  1565. command=self.print_to_file,
  1566. accelerator="Ctrl-p",
  1567. )
  1568. filemenu.add_command(
  1569. label="Exit", underline=1, command=self.destroy, accelerator="Ctrl-x"
  1570. )
  1571. menubar.add_cascade(label="File", underline=0, menu=filemenu)
  1572. self._parent.config(menu=menubar)
  1573. def print_to_file(self, filename=None):
  1574. """
  1575. Print the contents of this ``CanvasFrame`` to a postscript
  1576. file. If no filename is given, then prompt the user for one.
  1577. :param filename: The name of the file to print the tree to.
  1578. :type filename: str
  1579. :rtype: None
  1580. """
  1581. if filename is None:
  1582. ftypes = [("Postscript files", ".ps"), ("All files", "*")]
  1583. filename = asksaveasfilename(filetypes=ftypes, defaultextension=".ps")
  1584. if not filename:
  1585. return
  1586. (x0, y0, w, h) = self.scrollregion()
  1587. postscript = self._canvas.postscript(
  1588. x=x0,
  1589. y=y0,
  1590. width=w + 2,
  1591. height=h + 2,
  1592. pagewidth=w + 2, # points = 1/72 inch
  1593. pageheight=h + 2, # points = 1/72 inch
  1594. pagex=0,
  1595. pagey=0,
  1596. )
  1597. # workaround for bug in Tk font handling
  1598. postscript = postscript.replace(" 0 scalefont ", " 9 scalefont ")
  1599. with open(filename, "wb") as f:
  1600. f.write(postscript.encode("utf8"))
  1601. def scrollregion(self):
  1602. """
  1603. :return: The current scroll region for the canvas managed by
  1604. this ``CanvasFrame``.
  1605. :rtype: 4-tuple of int
  1606. """
  1607. (x1, y1, x2, y2) = self._canvas["scrollregion"].split()
  1608. return (int(x1), int(y1), int(x2), int(y2))
  1609. def canvas(self):
  1610. """
  1611. :return: The canvas managed by this ``CanvasFrame``.
  1612. :rtype: Tkinter.Canvas
  1613. """
  1614. return self._canvas
  1615. def add_widget(self, canvaswidget, x=None, y=None):
  1616. """
  1617. Register a canvas widget with this ``CanvasFrame``. The
  1618. ``CanvasFrame`` will ensure that this canvas widget is always
  1619. within the ``Canvas``'s scrollregion. If no coordinates are
  1620. given for the canvas widget, then the ``CanvasFrame`` will
  1621. attempt to find a clear area of the canvas for it.
  1622. :type canvaswidget: CanvasWidget
  1623. :param canvaswidget: The new canvas widget. ``canvaswidget``
  1624. must have been created on this ``CanvasFrame``'s canvas.
  1625. :type x: int
  1626. :param x: The initial x coordinate for the upper left hand
  1627. corner of ``canvaswidget``, in the canvas's coordinate
  1628. space.
  1629. :type y: int
  1630. :param y: The initial y coordinate for the upper left hand
  1631. corner of ``canvaswidget``, in the canvas's coordinate
  1632. space.
  1633. """
  1634. if x is None or y is None:
  1635. (x, y) = self._find_room(canvaswidget, x, y)
  1636. # Move to (x,y)
  1637. (x1, y1, x2, y2) = canvaswidget.bbox()
  1638. canvaswidget.move(x - x1, y - y1)
  1639. # Register with scrollwatcher.
  1640. self._scrollwatcher.add_child(canvaswidget)
  1641. def _find_room(self, widget, desired_x, desired_y):
  1642. """
  1643. Try to find a space for a given widget.
  1644. """
  1645. (left, top, right, bot) = self.scrollregion()
  1646. w = widget.width()
  1647. h = widget.height()
  1648. if w >= (right - left):
  1649. return (0, 0)
  1650. if h >= (bot - top):
  1651. return (0, 0)
  1652. # Move the widget out of the way, for now.
  1653. (x1, y1, x2, y2) = widget.bbox()
  1654. widget.move(left - x2 - 50, top - y2 - 50)
  1655. if desired_x is not None:
  1656. x = desired_x
  1657. for y in range(top, bot - h, int((bot - top - h) / 10)):
  1658. if not self._canvas.find_overlapping(
  1659. x - 5, y - 5, x + w + 5, y + h + 5
  1660. ):
  1661. return (x, y)
  1662. if desired_y is not None:
  1663. y = desired_y
  1664. for x in range(left, right - w, int((right - left - w) / 10)):
  1665. if not self._canvas.find_overlapping(
  1666. x - 5, y - 5, x + w + 5, y + h + 5
  1667. ):
  1668. return (x, y)
  1669. for y in range(top, bot - h, int((bot - top - h) / 10)):
  1670. for x in range(left, right - w, int((right - left - w) / 10)):
  1671. if not self._canvas.find_overlapping(
  1672. x - 5, y - 5, x + w + 5, y + h + 5
  1673. ):
  1674. return (x, y)
  1675. return (0, 0)
  1676. def destroy_widget(self, canvaswidget):
  1677. """
  1678. Remove a canvas widget from this ``CanvasFrame``. This
  1679. deregisters the canvas widget, and destroys it.
  1680. """
  1681. self.remove_widget(canvaswidget)
  1682. canvaswidget.destroy()
  1683. def remove_widget(self, canvaswidget):
  1684. # Deregister with scrollwatcher.
  1685. self._scrollwatcher.remove_child(canvaswidget)
  1686. def pack(self, cnf={}, **kw):
  1687. """
  1688. Pack this ``CanvasFrame``. See the documentation for
  1689. ``Tkinter.Pack`` for more information.
  1690. """
  1691. self._frame.pack(cnf, **kw)
  1692. # Adjust to be big enough for kids?
  1693. def destroy(self, *e):
  1694. """
  1695. Destroy this ``CanvasFrame``. If this ``CanvasFrame`` created a
  1696. top-level window, then this will close that window.
  1697. """
  1698. if self._parent is None:
  1699. return
  1700. self._parent.destroy()
  1701. self._parent = None
  1702. def mainloop(self, *args, **kwargs):
  1703. """
  1704. Enter the Tkinter mainloop. This function must be called if
  1705. this frame is created from a non-interactive program (e.g.
  1706. from a secript); otherwise, the frame will close as soon as
  1707. the script completes.
  1708. """
  1709. if in_idle():
  1710. return
  1711. self._parent.mainloop(*args, **kwargs)
  1712. ##//////////////////////////////////////////////////////
  1713. ## Text display
  1714. ##//////////////////////////////////////////////////////
  1715. class ShowText(object):
  1716. """
  1717. A ``Tkinter`` window used to display a text. ``ShowText`` is
  1718. typically used by graphical tools to display help text, or similar
  1719. information.
  1720. """
  1721. def __init__(self, root, title, text, width=None, height=None, **textbox_options):
  1722. if width is None or height is None:
  1723. (width, height) = self.find_dimentions(text, width, height)
  1724. # Create the main window.
  1725. if root is None:
  1726. self._top = top = Tk()
  1727. else:
  1728. self._top = top = Toplevel(root)
  1729. top.title(title)
  1730. b = Button(top, text="Ok", command=self.destroy)
  1731. b.pack(side="bottom")
  1732. tbf = Frame(top)
  1733. tbf.pack(expand=1, fill="both")
  1734. scrollbar = Scrollbar(tbf, orient="vertical")
  1735. scrollbar.pack(side="right", fill="y")
  1736. textbox = Text(tbf, wrap="word", width=width, height=height, **textbox_options)
  1737. textbox.insert("end", text)
  1738. textbox["state"] = "disabled"
  1739. textbox.pack(side="left", expand=1, fill="both")
  1740. scrollbar["command"] = textbox.yview
  1741. textbox["yscrollcommand"] = scrollbar.set
  1742. # Make it easy to close the window.
  1743. top.bind("q", self.destroy)
  1744. top.bind("x", self.destroy)
  1745. top.bind("c", self.destroy)
  1746. top.bind("<Return>", self.destroy)
  1747. top.bind("<Escape>", self.destroy)
  1748. # Focus the scrollbar, so they can use up/down, etc.
  1749. scrollbar.focus()
  1750. def find_dimentions(self, text, width, height):
  1751. lines = text.split("\n")
  1752. if width is None:
  1753. maxwidth = max(len(line) for line in lines)
  1754. width = min(maxwidth, 80)
  1755. # Now, find height.
  1756. height = 0
  1757. for line in lines:
  1758. while len(line) > width:
  1759. brk = line[:width].rfind(" ")
  1760. line = line[brk:]
  1761. height += 1
  1762. height += 1
  1763. height = min(height, 25)
  1764. return (width, height)
  1765. def destroy(self, *e):
  1766. if self._top is None:
  1767. return
  1768. self._top.destroy()
  1769. self._top = None
  1770. def mainloop(self, *args, **kwargs):
  1771. """
  1772. Enter the Tkinter mainloop. This function must be called if
  1773. this window is created from a non-interactive program (e.g.
  1774. from a secript); otherwise, the window will close as soon as
  1775. the script completes.
  1776. """
  1777. if in_idle():
  1778. return
  1779. self._top.mainloop(*args, **kwargs)
  1780. ##//////////////////////////////////////////////////////
  1781. ## Entry dialog
  1782. ##//////////////////////////////////////////////////////
  1783. class EntryDialog(object):
  1784. """
  1785. A dialog box for entering
  1786. """
  1787. def __init__(
  1788. self, parent, original_text="", instructions="", set_callback=None, title=None
  1789. ):
  1790. self._parent = parent
  1791. self._original_text = original_text
  1792. self._set_callback = set_callback
  1793. width = int(max(30, len(original_text) * 3 / 2))
  1794. self._top = Toplevel(parent)
  1795. if title:
  1796. self._top.title(title)
  1797. # The text entry box.
  1798. entryframe = Frame(self._top)
  1799. entryframe.pack(expand=1, fill="both", padx=5, pady=5, ipady=10)
  1800. if instructions:
  1801. l = Label(entryframe, text=instructions)
  1802. l.pack(side="top", anchor="w", padx=30)
  1803. self._entry = Entry(entryframe, width=width)
  1804. self._entry.pack(expand=1, fill="x", padx=30)
  1805. self._entry.insert(0, original_text)
  1806. # A divider
  1807. divider = Frame(self._top, borderwidth=1, relief="sunken")
  1808. divider.pack(fill="x", ipady=1, padx=10)
  1809. # The buttons.
  1810. buttons = Frame(self._top)
  1811. buttons.pack(expand=0, fill="x", padx=5, pady=5)
  1812. b = Button(buttons, text="Cancel", command=self._cancel, width=8)
  1813. b.pack(side="right", padx=5)
  1814. b = Button(buttons, text="Ok", command=self._ok, width=8, default="active")
  1815. b.pack(side="left", padx=5)
  1816. b = Button(buttons, text="Apply", command=self._apply, width=8)
  1817. b.pack(side="left")
  1818. self._top.bind("<Return>", self._ok)
  1819. self._top.bind("<Control-q>", self._cancel)
  1820. self._top.bind("<Escape>", self._cancel)
  1821. self._entry.focus()
  1822. def _reset(self, *e):
  1823. self._entry.delete(0, "end")
  1824. self._entry.insert(0, self._original_text)
  1825. if self._set_callback:
  1826. self._set_callback(self._original_text)
  1827. def _cancel(self, *e):
  1828. try:
  1829. self._reset()
  1830. except:
  1831. pass
  1832. self._destroy()
  1833. def _ok(self, *e):
  1834. self._apply()
  1835. self._destroy()
  1836. def _apply(self, *e):
  1837. if self._set_callback:
  1838. self._set_callback(self._entry.get())
  1839. def _destroy(self, *e):
  1840. if self._top is None:
  1841. return
  1842. self._top.destroy()
  1843. self._top = None
  1844. ##//////////////////////////////////////////////////////
  1845. ## Colorized List
  1846. ##//////////////////////////////////////////////////////
  1847. class ColorizedList(object):
  1848. """
  1849. An abstract base class for displaying a colorized list of items.
  1850. Subclasses should define:
  1851. - ``_init_colortags``, which sets up Text color tags that
  1852. will be used by the list.
  1853. - ``_item_repr``, which returns a list of (text,colortag)
  1854. tuples that make up the colorized representation of the
  1855. item.
  1856. :note: Typically, you will want to register a callback for
  1857. ``'select'`` that calls ``mark`` on the given item.
  1858. """
  1859. def __init__(self, parent, items=[], **options):
  1860. """
  1861. Construct a new list.
  1862. :param parent: The Tk widget that contains the colorized list
  1863. :param items: The initial contents of the colorized list.
  1864. :param options:
  1865. """
  1866. self._parent = parent
  1867. self._callbacks = {}
  1868. # Which items are marked?
  1869. self._marks = {}
  1870. # Initialize the Tkinter frames.
  1871. self._init_itemframe(options.copy())
  1872. # Set up key & mouse bindings.
  1873. self._textwidget.bind("<KeyPress>", self._keypress)
  1874. self._textwidget.bind("<ButtonPress>", self._buttonpress)
  1875. # Fill in the given CFG's items.
  1876. self._items = None
  1877. self.set(items)
  1878. # ////////////////////////////////////////////////////////////
  1879. # Abstract methods
  1880. # ////////////////////////////////////////////////////////////
  1881. @abstractmethod
  1882. def _init_colortags(self, textwidget, options):
  1883. """
  1884. Set up any colortags that will be used by this colorized list.
  1885. E.g.:
  1886. >>> textwidget.tag_config('terminal', foreground='black')
  1887. """
  1888. @abstractmethod
  1889. def _item_repr(self, item):
  1890. """
  1891. Return a list of (text, colortag) tuples that make up the
  1892. colorized representation of the item. Colorized
  1893. representations may not span multiple lines. I.e., the text
  1894. strings returned may not contain newline characters.
  1895. """
  1896. # ////////////////////////////////////////////////////////////
  1897. # Item Access
  1898. # ////////////////////////////////////////////////////////////
  1899. def get(self, index=None):
  1900. """
  1901. :return: A list of the items contained by this list.
  1902. """
  1903. if index is None:
  1904. return self._items[:]
  1905. else:
  1906. return self._items[index]
  1907. def set(self, items):
  1908. """
  1909. Modify the list of items contained by this list.
  1910. """
  1911. items = list(items)
  1912. if self._items == items:
  1913. return
  1914. self._items = list(items)
  1915. self._textwidget["state"] = "normal"
  1916. self._textwidget.delete("1.0", "end")
  1917. for item in items:
  1918. for (text, colortag) in self._item_repr(item):
  1919. assert "\n" not in text, "item repr may not contain newline"
  1920. self._textwidget.insert("end", text, colortag)
  1921. self._textwidget.insert("end", "\n")
  1922. # Remove the final newline
  1923. self._textwidget.delete("end-1char", "end")
  1924. self._textwidget.mark_set("insert", "1.0")
  1925. self._textwidget["state"] = "disabled"
  1926. # Clear all marks
  1927. self._marks.clear()
  1928. def unmark(self, item=None):
  1929. """
  1930. Remove highlighting from the given item; or from every item,
  1931. if no item is given.
  1932. :raise ValueError: If ``item`` is not contained in the list.
  1933. :raise KeyError: If ``item`` is not marked.
  1934. """
  1935. if item is None:
  1936. self._marks.clear()
  1937. self._textwidget.tag_remove("highlight", "1.0", "end+1char")
  1938. else:
  1939. index = self._items.index(item)
  1940. del self._marks[item]
  1941. (start, end) = ("%d.0" % (index + 1), "%d.0" % (index + 2))
  1942. self._textwidget.tag_remove("highlight", start, end)
  1943. def mark(self, item):
  1944. """
  1945. Highlight the given item.
  1946. :raise ValueError: If ``item`` is not contained in the list.
  1947. """
  1948. self._marks[item] = 1
  1949. index = self._items.index(item)
  1950. (start, end) = ("%d.0" % (index + 1), "%d.0" % (index + 2))
  1951. self._textwidget.tag_add("highlight", start, end)
  1952. def markonly(self, item):
  1953. """
  1954. Remove any current highlighting, and mark the given item.
  1955. :raise ValueError: If ``item`` is not contained in the list.
  1956. """
  1957. self.unmark()
  1958. self.mark(item)
  1959. def view(self, item):
  1960. """
  1961. Adjust the view such that the given item is visible. If
  1962. the item is already visible, then do nothing.
  1963. """
  1964. index = self._items.index(item)
  1965. self._textwidget.see("%d.0" % (index + 1))
  1966. # ////////////////////////////////////////////////////////////
  1967. # Callbacks
  1968. # ////////////////////////////////////////////////////////////
  1969. def add_callback(self, event, func):
  1970. """
  1971. Register a callback function with the list. This function
  1972. will be called whenever the given event occurs.
  1973. :param event: The event that will trigger the callback
  1974. function. Valid events are: click1, click2, click3,
  1975. space, return, select, up, down, next, prior, move
  1976. :param func: The function that should be called when
  1977. the event occurs. ``func`` will be called with a
  1978. single item as its argument. (The item selected
  1979. or the item moved to).
  1980. """
  1981. if event == "select":
  1982. events = ["click1", "space", "return"]
  1983. elif event == "move":
  1984. events = ["up", "down", "next", "prior"]
  1985. else:
  1986. events = [event]
  1987. for e in events:
  1988. self._callbacks.setdefault(e, {})[func] = 1
  1989. def remove_callback(self, event, func=None):
  1990. """
  1991. Deregister a callback function. If ``func`` is none, then
  1992. all callbacks are removed for the given event.
  1993. """
  1994. if event is None:
  1995. events = list(self._callbacks.keys())
  1996. elif event == "select":
  1997. events = ["click1", "space", "return"]
  1998. elif event == "move":
  1999. events = ["up", "down", "next", "prior"]
  2000. else:
  2001. events = [event]
  2002. for e in events:
  2003. if func is None:
  2004. del self._callbacks[e]
  2005. else:
  2006. try:
  2007. del self._callbacks[e][func]
  2008. except:
  2009. pass
  2010. # ////////////////////////////////////////////////////////////
  2011. # Tkinter Methods
  2012. # ////////////////////////////////////////////////////////////
  2013. def pack(self, cnf={}, **kw):
  2014. # "@include: Tkinter.Pack.pack"
  2015. self._itemframe.pack(cnf, **kw)
  2016. def grid(self, cnf={}, **kw):
  2017. # "@include: Tkinter.Grid.grid"
  2018. self._itemframe.grid(cnf, *kw)
  2019. def focus(self):
  2020. # "@include: Tkinter.Widget.focus"
  2021. self._textwidget.focus()
  2022. # ////////////////////////////////////////////////////////////
  2023. # Internal Methods
  2024. # ////////////////////////////////////////////////////////////
  2025. def _init_itemframe(self, options):
  2026. self._itemframe = Frame(self._parent)
  2027. # Create the basic Text widget & scrollbar.
  2028. options.setdefault("background", "#e0e0e0")
  2029. self._textwidget = Text(self._itemframe, **options)
  2030. self._textscroll = Scrollbar(self._itemframe, takefocus=0, orient="vertical")
  2031. self._textwidget.config(yscrollcommand=self._textscroll.set)
  2032. self._textscroll.config(command=self._textwidget.yview)
  2033. self._textscroll.pack(side="right", fill="y")
  2034. self._textwidget.pack(expand=1, fill="both", side="left")
  2035. # Initialize the colorization tags
  2036. self._textwidget.tag_config(
  2037. "highlight", background="#e0ffff", border="1", relief="raised"
  2038. )
  2039. self._init_colortags(self._textwidget, options)
  2040. # How do I want to mark keyboard selection?
  2041. self._textwidget.tag_config("sel", foreground="")
  2042. self._textwidget.tag_config(
  2043. "sel", foreground="", background="", border="", underline=1
  2044. )
  2045. self._textwidget.tag_lower("highlight", "sel")
  2046. def _fire_callback(self, event, itemnum):
  2047. if event not in self._callbacks:
  2048. return
  2049. if 0 <= itemnum < len(self._items):
  2050. item = self._items[itemnum]
  2051. else:
  2052. item = None
  2053. for cb_func in list(self._callbacks[event].keys()):
  2054. cb_func(item)
  2055. def _buttonpress(self, event):
  2056. clickloc = "@%d,%d" % (event.x, event.y)
  2057. insert_point = self._textwidget.index(clickloc)
  2058. itemnum = int(insert_point.split(".")[0]) - 1
  2059. self._fire_callback("click%d" % event.num, itemnum)
  2060. def _keypress(self, event):
  2061. if event.keysym == "Return" or event.keysym == "space":
  2062. insert_point = self._textwidget.index("insert")
  2063. itemnum = int(insert_point.split(".")[0]) - 1
  2064. self._fire_callback(event.keysym.lower(), itemnum)
  2065. return
  2066. elif event.keysym == "Down":
  2067. delta = "+1line"
  2068. elif event.keysym == "Up":
  2069. delta = "-1line"
  2070. elif event.keysym == "Next":
  2071. delta = "+10lines"
  2072. elif event.keysym == "Prior":
  2073. delta = "-10lines"
  2074. else:
  2075. return "continue"
  2076. self._textwidget.mark_set("insert", "insert" + delta)
  2077. self._textwidget.see("insert")
  2078. self._textwidget.tag_remove("sel", "1.0", "end+1char")
  2079. self._textwidget.tag_add("sel", "insert linestart", "insert lineend")
  2080. insert_point = self._textwidget.index("insert")
  2081. itemnum = int(insert_point.split(".")[0]) - 1
  2082. self._fire_callback(event.keysym.lower(), itemnum)
  2083. return "break"
  2084. ##//////////////////////////////////////////////////////
  2085. ## Improved OptionMenu
  2086. ##//////////////////////////////////////////////////////
  2087. class MutableOptionMenu(Menubutton):
  2088. def __init__(self, master, values, **options):
  2089. self._callback = options.get("command")
  2090. if "command" in options:
  2091. del options["command"]
  2092. # Create a variable
  2093. self._variable = variable = StringVar()
  2094. if len(values) > 0:
  2095. variable.set(values[0])
  2096. kw = {
  2097. "borderwidth": 2,
  2098. "textvariable": variable,
  2099. "indicatoron": 1,
  2100. "relief": RAISED,
  2101. "anchor": "c",
  2102. "highlightthickness": 2,
  2103. }
  2104. kw.update(options)
  2105. Widget.__init__(self, master, "menubutton", kw)
  2106. self.widgetName = "tk_optionMenu"
  2107. self._menu = Menu(self, name="menu", tearoff=0)
  2108. self.menuname = self._menu._w
  2109. self._values = []
  2110. for value in values:
  2111. self.add(value)
  2112. self["menu"] = self._menu
  2113. def add(self, value):
  2114. if value in self._values:
  2115. return
  2116. def set(value=value):
  2117. self.set(value)
  2118. self._menu.add_command(label=value, command=set)
  2119. self._values.append(value)
  2120. def set(self, value):
  2121. self._variable.set(value)
  2122. if self._callback:
  2123. self._callback(value)
  2124. def remove(self, value):
  2125. # Might raise indexerror: pass to parent.
  2126. i = self._values.index(value)
  2127. del self._values[i]
  2128. self._menu.delete(i, i)
  2129. def __getitem__(self, name):
  2130. if name == "menu":
  2131. return self.__menu
  2132. return Widget.__getitem__(self, name)
  2133. def destroy(self):
  2134. """Destroy this widget and the associated menu."""
  2135. Menubutton.destroy(self)
  2136. self._menu = None
  2137. ##//////////////////////////////////////////////////////
  2138. ## Test code.
  2139. ##//////////////////////////////////////////////////////
  2140. def demo():
  2141. """
  2142. A simple demonstration showing how to use canvas widgets.
  2143. """
  2144. def fill(cw):
  2145. from random import randint
  2146. cw["fill"] = "#00%04d" % randint(0, 9999)
  2147. def color(cw):
  2148. from random import randint
  2149. cw["color"] = "#ff%04d" % randint(0, 9999)
  2150. cf = CanvasFrame(closeenough=10, width=300, height=300)
  2151. c = cf.canvas()
  2152. ct3 = TextWidget(c, "hiya there", draggable=1)
  2153. ct2 = TextWidget(c, "o o\n||\n___\n U", draggable=1, justify="center")
  2154. co = OvalWidget(c, ct2, outline="red")
  2155. ct = TextWidget(c, "o o\n||\n\\___/", draggable=1, justify="center")
  2156. cp = ParenWidget(c, ct, color="red")
  2157. cb = BoxWidget(c, cp, fill="cyan", draggable=1, width=3, margin=10)
  2158. equation = SequenceWidget(
  2159. c,
  2160. SymbolWidget(c, "forall"),
  2161. TextWidget(c, "x"),
  2162. SymbolWidget(c, "exists"),
  2163. TextWidget(c, "y: "),
  2164. TextWidget(c, "x"),
  2165. SymbolWidget(c, "notequal"),
  2166. TextWidget(c, "y"),
  2167. )
  2168. space = SpaceWidget(c, 0, 30)
  2169. cstack = StackWidget(c, cb, ct3, space, co, equation, align="center")
  2170. prompt_msg = TextWidget(
  2171. c, "try clicking\nand dragging", draggable=1, justify="center"
  2172. )
  2173. cs = SequenceWidget(c, cstack, prompt_msg)
  2174. zz = BracketWidget(c, cs, color="green4", width=3)
  2175. cf.add_widget(zz, 60, 30)
  2176. cb.bind_click(fill)
  2177. ct.bind_click(color)
  2178. co.bind_click(fill)
  2179. ct2.bind_click(color)
  2180. ct3.bind_click(color)
  2181. cf.mainloop()
  2182. # ShowText(None, 'title', ((('this is text'*150)+'\n')*5))
  2183. if __name__ == "__main__":
  2184. demo()