_backend_tk.py 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. import uuid
  2. import weakref
  3. from contextlib import contextmanager
  4. import logging
  5. import math
  6. import os.path
  7. import pathlib
  8. import sys
  9. import tkinter as tk
  10. import tkinter.filedialog
  11. import tkinter.font
  12. import tkinter.messagebox
  13. from tkinter.simpledialog import SimpleDialog
  14. import numpy as np
  15. from PIL import Image, ImageTk
  16. import matplotlib as mpl
  17. from matplotlib import _api, backend_tools, cbook, _c_internal_utils
  18. from matplotlib.backend_bases import (
  19. _Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
  20. TimerBase, ToolContainerBase, cursors, _Mode, MouseButton,
  21. CloseEvent, KeyEvent, LocationEvent, MouseEvent, ResizeEvent)
  22. from matplotlib._pylab_helpers import Gcf
  23. try:
  24. from . import _tkagg
  25. from ._tkagg import TK_PHOTO_COMPOSITE_OVERLAY, TK_PHOTO_COMPOSITE_SET
  26. except ImportError as e:
  27. # catch incompatibility of python-build-standalone with Tk
  28. cause1 = getattr(e, '__cause__', None)
  29. cause2 = getattr(cause1, '__cause__', None)
  30. if (isinstance(cause1, ImportError) and
  31. isinstance(cause2, AttributeError) and
  32. "'_tkinter' has no attribute '__file__'" in str(cause2)):
  33. is_uv_python = "/uv/python" in (os.path.realpath(sys.executable))
  34. if is_uv_python:
  35. raise ImportError(
  36. "Failed to import tkagg backend. You appear to be using an outdated "
  37. "version of uv's managed Python distribution which is not compatible "
  38. "with Tk. Please upgrade to the latest uv version, then update "
  39. "Python with: `uv python upgrade --reinstall`"
  40. ) from e
  41. else:
  42. raise ImportError(
  43. "Failed to import tkagg backend. This is likely caused by using a "
  44. "Python executable based on python-build-standalone, which is not "
  45. "compatible with Tk. Recent versions of python-build-standalone "
  46. "should be compatible with Tk. Please update your python version "
  47. "or select another backend."
  48. ) from e
  49. else:
  50. raise
  51. _log = logging.getLogger(__name__)
  52. cursord = {
  53. cursors.MOVE: "fleur",
  54. cursors.HAND: "hand2",
  55. cursors.POINTER: "arrow",
  56. cursors.SELECT_REGION: "crosshair",
  57. cursors.WAIT: "watch",
  58. cursors.RESIZE_HORIZONTAL: "sb_h_double_arrow",
  59. cursors.RESIZE_VERTICAL: "sb_v_double_arrow",
  60. }
  61. @contextmanager
  62. def _restore_foreground_window_at_end():
  63. foreground = _c_internal_utils.Win32_GetForegroundWindow()
  64. try:
  65. yield
  66. finally:
  67. if foreground and mpl.rcParams['tk.window_focus']:
  68. _c_internal_utils.Win32_SetForegroundWindow(foreground)
  69. _blit_args = {}
  70. # Initialize to a non-empty string that is not a Tcl command
  71. _blit_tcl_name = "mpl_blit_" + uuid.uuid4().hex
  72. def _blit(argsid):
  73. """
  74. Thin wrapper to blit called via tkapp.call.
  75. *argsid* is a unique string identifier to fetch the correct arguments from
  76. the ``_blit_args`` dict, since arguments cannot be passed directly.
  77. """
  78. photoimage, data, offsets, bbox, comp_rule = _blit_args.pop(argsid)
  79. if not photoimage.tk.call("info", "commands", photoimage):
  80. return
  81. _tkagg.blit(photoimage.tk.interpaddr(), str(photoimage), data, comp_rule, offsets,
  82. bbox)
  83. def blit(photoimage, aggimage, offsets, bbox=None):
  84. """
  85. Blit *aggimage* to *photoimage*.
  86. *offsets* is a tuple describing how to fill the ``offset`` field of the
  87. ``Tk_PhotoImageBlock`` struct: it should be (0, 1, 2, 3) for RGBA8888 data,
  88. (2, 1, 0, 3) for little-endian ARBG32 (i.e. GBRA8888) data and (1, 2, 3, 0)
  89. for big-endian ARGB32 (i.e. ARGB8888) data.
  90. If *bbox* is passed, it defines the region that gets blitted. That region
  91. will be composed with the previous data according to the alpha channel.
  92. Blitting will be clipped to pixels inside the canvas, including silently
  93. doing nothing if the *bbox* region is entirely outside the canvas.
  94. Tcl events must be dispatched to trigger a blit from a non-Tcl thread.
  95. """
  96. data = np.asarray(aggimage)
  97. height, width = data.shape[:2]
  98. if bbox is not None:
  99. (x1, y1), (x2, y2) = bbox.__array__()
  100. x1 = max(math.floor(x1), 0)
  101. x2 = min(math.ceil(x2), width)
  102. y1 = max(math.floor(y1), 0)
  103. y2 = min(math.ceil(y2), height)
  104. if (x1 > x2) or (y1 > y2):
  105. return
  106. bboxptr = (x1, x2, y1, y2)
  107. comp_rule = TK_PHOTO_COMPOSITE_OVERLAY
  108. else:
  109. bboxptr = (0, width, 0, height)
  110. comp_rule = TK_PHOTO_COMPOSITE_SET
  111. # NOTE: _tkagg.blit is thread unsafe and will crash the process if called
  112. # from a thread (GH#13293). Instead of blanking and blitting here,
  113. # use tkapp.call to post a cross-thread event if this function is called
  114. # from a non-Tcl thread.
  115. # tkapp.call coerces all arguments to strings, so to avoid string parsing
  116. # within _blit, pack up the arguments into a global data structure.
  117. args = photoimage, data, offsets, bboxptr, comp_rule
  118. # Need a unique key to avoid thread races.
  119. # Again, make the key a string to avoid string parsing in _blit.
  120. argsid = str(id(args))
  121. _blit_args[argsid] = args
  122. try:
  123. photoimage.tk.call(_blit_tcl_name, argsid)
  124. except tk.TclError as e:
  125. if "invalid command name" not in str(e):
  126. raise
  127. photoimage.tk.createcommand(_blit_tcl_name, _blit)
  128. photoimage.tk.call(_blit_tcl_name, argsid)
  129. class TimerTk(TimerBase):
  130. """Subclass of `backend_bases.TimerBase` using Tk timer events."""
  131. def __init__(self, parent, *args, **kwargs):
  132. self._timer = None
  133. super().__init__(*args, **kwargs)
  134. self.parent = parent
  135. def _timer_start(self):
  136. self._timer_stop()
  137. self._timer = self.parent.after(self._interval, self._on_timer)
  138. def _timer_stop(self):
  139. if self._timer is not None:
  140. self.parent.after_cancel(self._timer)
  141. self._timer = None
  142. def _on_timer(self):
  143. super()._on_timer()
  144. # Tk after() is only a single shot, so we need to add code here to
  145. # reset the timer if we're not operating in single shot mode. However,
  146. # if _timer is None, this means that _timer_stop has been called; so
  147. # don't recreate the timer in that case.
  148. if not self._single and self._timer:
  149. if self._interval > 0:
  150. self._timer = self.parent.after(self._interval, self._on_timer)
  151. else:
  152. # Edge case: Tcl after 0 *prepends* events to the queue
  153. # so a 0 interval does not allow any other events to run.
  154. # This incantation is cancellable and runs as fast as possible
  155. # while also allowing events and drawing every frame. GH#18236
  156. self._timer = self.parent.after_idle(
  157. lambda: self.parent.after(self._interval, self._on_timer)
  158. )
  159. else:
  160. self._timer = None
  161. class FigureCanvasTk(FigureCanvasBase):
  162. required_interactive_framework = "tk"
  163. manager_class = _api.classproperty(lambda cls: FigureManagerTk)
  164. def __init__(self, figure=None, master=None):
  165. super().__init__(figure)
  166. self._idle_draw_id = None
  167. self._event_loop_id = None
  168. w, h = self.get_width_height(physical=True)
  169. self._tkcanvas = tk.Canvas(
  170. master=master, background="white",
  171. width=w, height=h, borderwidth=0, highlightthickness=0)
  172. self._tkphoto = tk.PhotoImage(
  173. master=self._tkcanvas, width=w, height=h)
  174. self._tkcanvas_image_region = self._tkcanvas.create_image(
  175. w//2, h//2, image=self._tkphoto)
  176. self._tkcanvas.bind("<Configure>", self.resize)
  177. self._tkcanvas.bind("<Map>", self._update_device_pixel_ratio)
  178. self._tkcanvas.bind("<Key>", self.key_press)
  179. self._tkcanvas.bind("<Motion>", self.motion_notify_event)
  180. self._tkcanvas.bind("<Enter>", self.enter_notify_event)
  181. self._tkcanvas.bind("<Leave>", self.leave_notify_event)
  182. self._tkcanvas.bind("<KeyRelease>", self.key_release)
  183. for name in ["<Button-1>", "<Button-2>", "<Button-3>"]:
  184. self._tkcanvas.bind(name, self.button_press_event)
  185. for name in [
  186. "<Double-Button-1>", "<Double-Button-2>", "<Double-Button-3>"]:
  187. self._tkcanvas.bind(name, self.button_dblclick_event)
  188. for name in [
  189. "<ButtonRelease-1>", "<ButtonRelease-2>", "<ButtonRelease-3>"]:
  190. self._tkcanvas.bind(name, self.button_release_event)
  191. # Mouse wheel on Linux generates button 4/5 events
  192. for name in "<Button-4>", "<Button-5>":
  193. self._tkcanvas.bind(name, self.scroll_event)
  194. # Mouse wheel for windows goes to the window with the focus.
  195. # Since the canvas won't usually have the focus, bind the
  196. # event to the window containing the canvas instead.
  197. # See https://wiki.tcl-lang.org/3893 (mousewheel) for details
  198. root = self._tkcanvas.winfo_toplevel()
  199. # Prevent long-lived references via tkinter callback structure GH-24820
  200. weakself = weakref.ref(self)
  201. weakroot = weakref.ref(root)
  202. def scroll_event_windows(event):
  203. self = weakself()
  204. if self is None:
  205. root = weakroot()
  206. if root is not None:
  207. root.unbind("<MouseWheel>", scroll_event_windows_id)
  208. return
  209. return self.scroll_event_windows(event)
  210. scroll_event_windows_id = root.bind("<MouseWheel>", scroll_event_windows, "+")
  211. # Can't get destroy events by binding to _tkcanvas. Therefore, bind
  212. # to the window and filter.
  213. def filter_destroy(event):
  214. self = weakself()
  215. if self is None:
  216. root = weakroot()
  217. if root is not None:
  218. root.unbind("<Destroy>", filter_destroy_id)
  219. return
  220. if event.widget is self._tkcanvas:
  221. CloseEvent("close_event", self)._process()
  222. filter_destroy_id = root.bind("<Destroy>", filter_destroy, "+")
  223. self._tkcanvas.focus_set()
  224. self._rubberband_rect_black = None
  225. self._rubberband_rect_white = None
  226. def _update_device_pixel_ratio(self, event=None):
  227. ratio = None
  228. if sys.platform == 'win32':
  229. # Tk gives scaling with respect to 72 DPI, but Windows screens are
  230. # scaled vs 96 dpi, and pixel ratio settings are given in whole
  231. # percentages, so round to 2 digits.
  232. ratio = round(self._tkcanvas.tk.call('tk', 'scaling') / (96 / 72), 2)
  233. elif sys.platform == "linux":
  234. ratio = self._tkcanvas.winfo_fpixels('1i') / 96
  235. if ratio is not None and self._set_device_pixel_ratio(ratio):
  236. # The easiest way to resize the canvas is to resize the canvas
  237. # widget itself, since we implement all the logic for resizing the
  238. # canvas backing store on that event.
  239. w, h = self.get_width_height(physical=True)
  240. self._tkcanvas.configure(width=w, height=h)
  241. def resize(self, event):
  242. width, height = event.width, event.height
  243. # compute desired figure size in inches
  244. dpival = self.figure.dpi
  245. winch = width / dpival
  246. hinch = height / dpival
  247. self.figure.set_size_inches(winch, hinch, forward=False)
  248. self._tkcanvas.delete(self._tkcanvas_image_region)
  249. self._tkphoto.configure(width=int(width), height=int(height))
  250. self._tkcanvas_image_region = self._tkcanvas.create_image(
  251. int(width / 2), int(height / 2), image=self._tkphoto)
  252. ResizeEvent("resize_event", self)._process()
  253. self.draw_idle()
  254. def draw_idle(self):
  255. # docstring inherited
  256. if self._idle_draw_id:
  257. return
  258. def idle_draw(*args):
  259. try:
  260. self.draw()
  261. finally:
  262. self._idle_draw_id = None
  263. self._idle_draw_id = self._tkcanvas.after_idle(idle_draw)
  264. def get_tk_widget(self):
  265. """
  266. Return the Tk widget used to implement FigureCanvasTkAgg.
  267. Although the initial implementation uses a Tk canvas, this routine
  268. is intended to hide that fact.
  269. """
  270. return self._tkcanvas
  271. def _event_mpl_coords(self, event):
  272. # calling canvasx/canvasy allows taking scrollbars into account (i.e.
  273. # the top of the widget may have been scrolled out of view).
  274. return (self._tkcanvas.canvasx(event.x),
  275. # flipy so y=0 is bottom of canvas
  276. self.figure.bbox.height - self._tkcanvas.canvasy(event.y))
  277. def motion_notify_event(self, event):
  278. MouseEvent("motion_notify_event", self,
  279. *self._event_mpl_coords(event),
  280. buttons=self._mpl_buttons(event),
  281. modifiers=self._mpl_modifiers(event),
  282. guiEvent=event)._process()
  283. def enter_notify_event(self, event):
  284. LocationEvent("figure_enter_event", self,
  285. *self._event_mpl_coords(event),
  286. modifiers=self._mpl_modifiers(event),
  287. guiEvent=event)._process()
  288. def leave_notify_event(self, event):
  289. LocationEvent("figure_leave_event", self,
  290. *self._event_mpl_coords(event),
  291. modifiers=self._mpl_modifiers(event),
  292. guiEvent=event)._process()
  293. def button_press_event(self, event, dblclick=False):
  294. # set focus to the canvas so that it can receive keyboard events
  295. self._tkcanvas.focus_set()
  296. num = getattr(event, 'num', None)
  297. if sys.platform == 'darwin': # 2 and 3 are reversed.
  298. num = {2: 3, 3: 2}.get(num, num)
  299. MouseEvent("button_press_event", self,
  300. *self._event_mpl_coords(event), num, dblclick=dblclick,
  301. modifiers=self._mpl_modifiers(event),
  302. guiEvent=event)._process()
  303. def button_dblclick_event(self, event):
  304. self.button_press_event(event, dblclick=True)
  305. def button_release_event(self, event):
  306. num = getattr(event, 'num', None)
  307. if sys.platform == 'darwin': # 2 and 3 are reversed.
  308. num = {2: 3, 3: 2}.get(num, num)
  309. MouseEvent("button_release_event", self,
  310. *self._event_mpl_coords(event), num,
  311. modifiers=self._mpl_modifiers(event),
  312. guiEvent=event)._process()
  313. def scroll_event(self, event):
  314. num = getattr(event, 'num', None)
  315. step = 1 if num == 4 else -1 if num == 5 else 0
  316. MouseEvent("scroll_event", self,
  317. *self._event_mpl_coords(event), step=step,
  318. modifiers=self._mpl_modifiers(event),
  319. guiEvent=event)._process()
  320. def scroll_event_windows(self, event):
  321. """MouseWheel event processor"""
  322. # need to find the window that contains the mouse
  323. w = event.widget.winfo_containing(event.x_root, event.y_root)
  324. if w != self._tkcanvas:
  325. return
  326. x = self._tkcanvas.canvasx(event.x_root - w.winfo_rootx())
  327. y = (self.figure.bbox.height
  328. - self._tkcanvas.canvasy(event.y_root - w.winfo_rooty()))
  329. step = event.delta / 120
  330. MouseEvent("scroll_event", self,
  331. x, y, step=step, modifiers=self._mpl_modifiers(event),
  332. guiEvent=event)._process()
  333. @staticmethod
  334. def _mpl_buttons(event): # See _mpl_modifiers.
  335. # NOTE: This fails to report multiclicks on macOS; only one button is
  336. # reported (multiclicks work correctly on Linux & Windows).
  337. modifiers = [
  338. # macOS appears to swap right and middle (look for "Swap buttons
  339. # 2/3" in tk/macosx/tkMacOSXMouseEvent.c).
  340. (MouseButton.LEFT, 1 << 8),
  341. (MouseButton.RIGHT, 1 << 9),
  342. (MouseButton.MIDDLE, 1 << 10),
  343. (MouseButton.BACK, 1 << 11),
  344. (MouseButton.FORWARD, 1 << 12),
  345. ] if sys.platform == "darwin" else [
  346. (MouseButton.LEFT, 1 << 8),
  347. (MouseButton.MIDDLE, 1 << 9),
  348. (MouseButton.RIGHT, 1 << 10),
  349. (MouseButton.BACK, 1 << 11),
  350. (MouseButton.FORWARD, 1 << 12),
  351. ]
  352. # State *before* press/release.
  353. return [name for name, mask in modifiers if event.state & mask]
  354. @staticmethod
  355. def _mpl_modifiers(event, *, exclude=None):
  356. # Add modifier keys to the key string. Bit values are inferred from
  357. # the implementation of tkinter.Event.__repr__ (1, 2, 4, 8, ... =
  358. # Shift, Lock, Control, Mod1, ..., Mod5, Button1, ..., Button5)
  359. # In general, the modifier key is excluded from the modifier flag,
  360. # however this is not the case on "darwin", so double check that
  361. # we aren't adding repeat modifier flags to a modifier key.
  362. modifiers = [
  363. ("ctrl", 1 << 2, "control"),
  364. ("alt", 1 << 17, "alt"),
  365. ("shift", 1 << 0, "shift"),
  366. ] if sys.platform == "win32" else [
  367. ("ctrl", 1 << 2, "control"),
  368. ("alt", 1 << 4, "alt"),
  369. ("shift", 1 << 0, "shift"),
  370. ("cmd", 1 << 3, "cmd"),
  371. ] if sys.platform == "darwin" else [
  372. ("ctrl", 1 << 2, "control"),
  373. ("alt", 1 << 3, "alt"),
  374. ("shift", 1 << 0, "shift"),
  375. ("super", 1 << 6, "super"),
  376. ]
  377. return [name for name, mask, key in modifiers
  378. if event.state & mask and exclude != key]
  379. def _get_key(self, event):
  380. unikey = event.char
  381. key = cbook._unikey_or_keysym_to_mplkey(unikey, event.keysym)
  382. if key is not None:
  383. mods = self._mpl_modifiers(event, exclude=key)
  384. # shift is not added to the keys as this is already accounted for.
  385. if "shift" in mods and unikey:
  386. mods.remove("shift")
  387. return "+".join([*mods, key])
  388. def key_press(self, event):
  389. KeyEvent("key_press_event", self,
  390. self._get_key(event), *self._event_mpl_coords(event),
  391. guiEvent=event)._process()
  392. def key_release(self, event):
  393. KeyEvent("key_release_event", self,
  394. self._get_key(event), *self._event_mpl_coords(event),
  395. guiEvent=event)._process()
  396. def new_timer(self, *args, **kwargs):
  397. # docstring inherited
  398. return TimerTk(self._tkcanvas, *args, **kwargs)
  399. def flush_events(self):
  400. # docstring inherited
  401. self._tkcanvas.update()
  402. def start_event_loop(self, timeout=0):
  403. # docstring inherited
  404. if timeout > 0:
  405. milliseconds = int(1000 * timeout)
  406. if milliseconds > 0:
  407. self._event_loop_id = self._tkcanvas.after(
  408. milliseconds, self.stop_event_loop)
  409. else:
  410. self._event_loop_id = self._tkcanvas.after_idle(
  411. self.stop_event_loop)
  412. self._tkcanvas.mainloop()
  413. def stop_event_loop(self):
  414. # docstring inherited
  415. if self._event_loop_id:
  416. self._tkcanvas.after_cancel(self._event_loop_id)
  417. self._event_loop_id = None
  418. self._tkcanvas.quit()
  419. def set_cursor(self, cursor):
  420. try:
  421. self._tkcanvas.configure(cursor=cursord[cursor])
  422. except tkinter.TclError:
  423. pass
  424. class FigureManagerTk(FigureManagerBase):
  425. """
  426. Attributes
  427. ----------
  428. canvas : `FigureCanvas`
  429. The FigureCanvas instance
  430. num : int or str
  431. The Figure number
  432. toolbar : tk.Toolbar
  433. The tk.Toolbar
  434. window : tk.Window
  435. The tk.Window
  436. """
  437. _owns_mainloop = False
  438. def __init__(self, canvas, num, window):
  439. self.window = window
  440. super().__init__(canvas, num)
  441. self.window.withdraw()
  442. # packing toolbar first, because if space is getting low, last packed
  443. # widget is getting shrunk first (-> the canvas)
  444. self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
  445. # If the window has per-monitor DPI awareness, then setup a Tk variable
  446. # to store the DPI, which will be updated by the C code, and the trace
  447. # will handle it on the Python side.
  448. window_frame = int(window.wm_frame(), 16)
  449. self._window_dpi = tk.IntVar(master=window, value=96,
  450. name=f'window_dpi{window_frame}')
  451. self._window_dpi_cbname = ''
  452. if _tkagg.enable_dpi_awareness(window_frame, window.tk.interpaddr()):
  453. self._window_dpi_cbname = self._window_dpi.trace_add(
  454. 'write', self._update_window_dpi)
  455. self._shown = False
  456. @classmethod
  457. def create_with_canvas(cls, canvas_class, figure, num):
  458. # docstring inherited
  459. with _restore_foreground_window_at_end():
  460. if cbook._get_running_interactive_framework() is None:
  461. cbook._setup_new_guiapp()
  462. _c_internal_utils.Win32_SetProcessDpiAwareness_max()
  463. window = tk.Tk(className="matplotlib")
  464. window.withdraw()
  465. # Put a Matplotlib icon on the window rather than the default tk
  466. # icon. See https://www.tcl.tk/man/tcl/TkCmd/wm.html#M50
  467. #
  468. # `ImageTk` can be replaced with `tk` whenever the minimum
  469. # supported Tk version is increased to 8.6, as Tk 8.6+ natively
  470. # supports PNG images.
  471. icon_fname = str(cbook._get_data_path(
  472. 'images/matplotlib.png'))
  473. icon_img = ImageTk.PhotoImage(file=icon_fname, master=window)
  474. icon_fname_large = str(cbook._get_data_path(
  475. 'images/matplotlib_large.png'))
  476. icon_img_large = ImageTk.PhotoImage(
  477. file=icon_fname_large, master=window)
  478. window.iconphoto(False, icon_img_large, icon_img)
  479. canvas = canvas_class(figure, master=window)
  480. manager = cls(canvas, num, window)
  481. if mpl.is_interactive():
  482. manager.show()
  483. canvas.draw_idle()
  484. return manager
  485. @classmethod
  486. def start_main_loop(cls):
  487. managers = Gcf.get_all_fig_managers()
  488. if managers:
  489. first_manager = managers[0]
  490. manager_class = type(first_manager)
  491. if manager_class._owns_mainloop:
  492. return
  493. manager_class._owns_mainloop = True
  494. try:
  495. first_manager.window.mainloop()
  496. finally:
  497. manager_class._owns_mainloop = False
  498. def _update_window_dpi(self, *args):
  499. newdpi = self._window_dpi.get()
  500. self.window.call('tk', 'scaling', newdpi / 72)
  501. if self.toolbar and hasattr(self.toolbar, '_rescale'):
  502. self.toolbar._rescale()
  503. self.canvas._update_device_pixel_ratio()
  504. def resize(self, width, height):
  505. max_size = 1_400_000 # the measured max on xorg 1.20.8 was 1_409_023
  506. if (width > max_size or height > max_size) and sys.platform == 'linux':
  507. raise ValueError(
  508. 'You have requested to resize the '
  509. f'Tk window to ({width}, {height}), one of which '
  510. f'is bigger than {max_size}. At larger sizes xorg will '
  511. 'either exit with an error on newer versions (~1.20) or '
  512. 'cause corruption on older version (~1.19). We '
  513. 'do not expect a window over a million pixel wide or tall '
  514. 'to be intended behavior.')
  515. self.canvas._tkcanvas.configure(width=width, height=height)
  516. def show(self):
  517. with _restore_foreground_window_at_end():
  518. if not self._shown:
  519. def destroy(*args):
  520. Gcf.destroy(self)
  521. self.window.protocol("WM_DELETE_WINDOW", destroy)
  522. self.window.deiconify()
  523. self.canvas._tkcanvas.focus_set()
  524. else:
  525. self.canvas.draw_idle()
  526. if mpl.rcParams['figure.raise_window']:
  527. self.canvas.manager.window.attributes('-topmost', 1)
  528. self.canvas.manager.window.attributes('-topmost', 0)
  529. self._shown = True
  530. def destroy(self, *args):
  531. if self.canvas._idle_draw_id:
  532. self.canvas._tkcanvas.after_cancel(self.canvas._idle_draw_id)
  533. if self.canvas._event_loop_id:
  534. self.canvas._tkcanvas.after_cancel(self.canvas._event_loop_id)
  535. if self._window_dpi_cbname:
  536. self._window_dpi.trace_remove('write', self._window_dpi_cbname)
  537. # NOTE: events need to be flushed before issuing destroy (GH #9956),
  538. # however, self.window.update() can break user code. An async callback
  539. # is the safest way to achieve a complete draining of the event queue,
  540. # but it leaks if no tk event loop is running. Therefore we explicitly
  541. # check for an event loop and choose our best guess.
  542. def delayed_destroy():
  543. self.window.destroy()
  544. if self._owns_mainloop and not Gcf.get_num_fig_managers():
  545. self.window.quit()
  546. if cbook._get_running_interactive_framework() == "tk":
  547. # "after idle after 0" avoids Tcl error/race (GH #19940)
  548. self.window.after_idle(self.window.after, 0, delayed_destroy)
  549. else:
  550. self.window.update()
  551. delayed_destroy()
  552. def get_window_title(self):
  553. return self.window.wm_title()
  554. def set_window_title(self, title):
  555. self.window.wm_title(title)
  556. def full_screen_toggle(self):
  557. is_fullscreen = bool(self.window.attributes('-fullscreen'))
  558. self.window.attributes('-fullscreen', not is_fullscreen)
  559. class NavigationToolbar2Tk(NavigationToolbar2, tk.Frame):
  560. def __init__(self, canvas, window=None, *, pack_toolbar=True):
  561. """
  562. Parameters
  563. ----------
  564. canvas : `FigureCanvas`
  565. The figure canvas on which to operate.
  566. window : tk.Window
  567. The tk.Window which owns this toolbar.
  568. pack_toolbar : bool, default: True
  569. If True, add the toolbar to the parent's pack manager's packing
  570. list during initialization with ``side="bottom"`` and ``fill="x"``.
  571. If you want to use the toolbar with a different layout manager, use
  572. ``pack_toolbar=False``.
  573. """
  574. if window is None:
  575. window = canvas.get_tk_widget().master
  576. tk.Frame.__init__(self, master=window, borderwidth=2,
  577. width=int(canvas.figure.bbox.width), height=50)
  578. self._buttons = {}
  579. for text, tooltip_text, image_file, callback in self.toolitems:
  580. if text is None:
  581. # Add a spacer; return value is unused.
  582. self._Spacer()
  583. else:
  584. self._buttons[text] = button = self._Button(
  585. text,
  586. str(cbook._get_data_path(f"images/{image_file}.png")),
  587. toggle=callback in ["zoom", "pan"],
  588. command=getattr(self, callback),
  589. )
  590. if tooltip_text is not None:
  591. add_tooltip(button, tooltip_text)
  592. self._label_font = tkinter.font.Font(root=window, size=10)
  593. # This filler item ensures the toolbar is always at least two text
  594. # lines high. Otherwise the canvas gets redrawn as the mouse hovers
  595. # over images because those use two-line messages which resize the
  596. # toolbar.
  597. label = tk.Label(master=self, font=self._label_font,
  598. text='\N{NO-BREAK SPACE}\n\N{NO-BREAK SPACE}')
  599. label.pack(side=tk.RIGHT)
  600. self.message = tk.StringVar(master=self)
  601. self._message_label = tk.Label(master=self, font=self._label_font,
  602. textvariable=self.message,
  603. justify=tk.RIGHT)
  604. self._message_label.pack(side=tk.RIGHT)
  605. NavigationToolbar2.__init__(self, canvas)
  606. if pack_toolbar:
  607. self.pack(side=tk.BOTTOM, fill=tk.X)
  608. def _rescale(self):
  609. """
  610. Scale all children of the toolbar to current DPI setting.
  611. Before this is called, the Tk scaling setting will have been updated to
  612. match the new DPI. Tk widgets do not update for changes to scaling, but
  613. all measurements made after the change will match the new scaling. Thus
  614. this function re-applies all the same sizes in points, which Tk will
  615. scale correctly to pixels.
  616. """
  617. for widget in self.winfo_children():
  618. if isinstance(widget, (tk.Button, tk.Checkbutton)):
  619. if hasattr(widget, '_image_file'):
  620. # Explicit class because ToolbarTk calls _rescale.
  621. NavigationToolbar2Tk._set_image_for_button(self, widget)
  622. else:
  623. # Text-only button is handled by the font setting instead.
  624. pass
  625. elif isinstance(widget, tk.Frame):
  626. widget.configure(height='18p')
  627. widget.pack_configure(padx='3p')
  628. elif isinstance(widget, tk.Label):
  629. pass # Text is handled by the font setting instead.
  630. else:
  631. _log.warning('Unknown child class %s', widget.winfo_class)
  632. self._label_font.configure(size=10)
  633. def _update_buttons_checked(self):
  634. # sync button checkstates to match active mode
  635. for text, mode in [('Zoom', _Mode.ZOOM), ('Pan', _Mode.PAN)]:
  636. if text in self._buttons:
  637. if self.mode == mode:
  638. self._buttons[text].select() # NOT .invoke()
  639. else:
  640. self._buttons[text].deselect()
  641. def pan(self, *args):
  642. super().pan(*args)
  643. self._update_buttons_checked()
  644. def zoom(self, *args):
  645. super().zoom(*args)
  646. self._update_buttons_checked()
  647. def set_message(self, s):
  648. self.message.set(s)
  649. def draw_rubberband(self, event, x0, y0, x1, y1):
  650. # Block copied from remove_rubberband for backend_tools convenience.
  651. if self.canvas._rubberband_rect_white:
  652. self.canvas._tkcanvas.delete(self.canvas._rubberband_rect_white)
  653. if self.canvas._rubberband_rect_black:
  654. self.canvas._tkcanvas.delete(self.canvas._rubberband_rect_black)
  655. height = self.canvas.figure.bbox.height
  656. y0 = height - y0
  657. y1 = height - y1
  658. self.canvas._rubberband_rect_black = (
  659. self.canvas._tkcanvas.create_rectangle(
  660. x0, y0, x1, y1))
  661. self.canvas._rubberband_rect_white = (
  662. self.canvas._tkcanvas.create_rectangle(
  663. x0, y0, x1, y1, outline='white', dash=(3, 3)))
  664. def remove_rubberband(self):
  665. if self.canvas._rubberband_rect_white:
  666. self.canvas._tkcanvas.delete(self.canvas._rubberband_rect_white)
  667. self.canvas._rubberband_rect_white = None
  668. if self.canvas._rubberband_rect_black:
  669. self.canvas._tkcanvas.delete(self.canvas._rubberband_rect_black)
  670. self.canvas._rubberband_rect_black = None
  671. def _set_image_for_button(self, button):
  672. """
  673. Set the image for a button based on its pixel size.
  674. The pixel size is determined by the DPI scaling of the window.
  675. """
  676. if button._image_file is None:
  677. return
  678. # Allow _image_file to be relative to Matplotlib's "images" data
  679. # directory.
  680. path_regular = cbook._get_data_path('images', button._image_file)
  681. path_large = path_regular.with_name(
  682. path_regular.name.replace('.png', '_large.png'))
  683. size = button.winfo_pixels('18p')
  684. # Nested functions because ToolbarTk calls _Button.
  685. def _get_color(color_name):
  686. # `winfo_rgb` returns an (r, g, b) tuple in the range 0-65535
  687. return button.winfo_rgb(button.cget(color_name))
  688. def _is_dark(color):
  689. if isinstance(color, str):
  690. color = _get_color(color)
  691. return max(color) < 65535 / 2
  692. def _recolor_icon(image, color):
  693. image_data = np.asarray(image).copy()
  694. black_mask = (image_data[..., :3] == 0).all(axis=-1)
  695. image_data[black_mask, :3] = color
  696. return Image.fromarray(image_data)
  697. # Use the high-resolution (48x48 px) icon if it exists and is needed
  698. with Image.open(path_large if (size > 24 and path_large.exists())
  699. else path_regular) as im:
  700. # assure a RGBA image as foreground color is RGB
  701. im = im.convert("RGBA")
  702. image = ImageTk.PhotoImage(im.resize((size, size)), master=self)
  703. button._ntimage = image
  704. # create a version of the icon with the button's text color
  705. foreground = (255 / 65535) * np.array(
  706. button.winfo_rgb(button.cget("foreground")))
  707. im_alt = _recolor_icon(im, foreground)
  708. image_alt = ImageTk.PhotoImage(
  709. im_alt.resize((size, size)), master=self)
  710. button._ntimage_alt = image_alt
  711. if _is_dark("background"):
  712. # For Checkbuttons, we need to set `image` and `selectimage` at
  713. # the same time. Otherwise, when updating the `image` option
  714. # (such as when changing DPI), if the old `selectimage` has
  715. # just been overwritten, Tk will throw an error.
  716. image_kwargs = {"image": image_alt}
  717. else:
  718. image_kwargs = {"image": image}
  719. # Checkbuttons may switch the background to `selectcolor` in the
  720. # checked state, so check separately which image it needs to use in
  721. # that state to still ensure enough contrast with the background.
  722. if (
  723. isinstance(button, tk.Checkbutton)
  724. and button.cget("selectcolor") != ""
  725. ):
  726. if self._windowingsystem != "x11":
  727. selectcolor = "selectcolor"
  728. else:
  729. # On X11, selectcolor isn't used directly for indicator-less
  730. # buttons. See `::tk::CheckEnter` in the Tk button.tcl source
  731. # code for details.
  732. r1, g1, b1 = _get_color("selectcolor")
  733. r2, g2, b2 = _get_color("activebackground")
  734. selectcolor = ((r1+r2)/2, (g1+g2)/2, (b1+b2)/2)
  735. if _is_dark(selectcolor):
  736. image_kwargs["selectimage"] = image_alt
  737. else:
  738. image_kwargs["selectimage"] = image
  739. button.configure(**image_kwargs, height='18p', width='18p')
  740. def _Button(self, text, image_file, toggle, command):
  741. if not toggle:
  742. b = tk.Button(
  743. master=self, text=text, command=command,
  744. relief="flat", overrelief="groove", borderwidth=1,
  745. )
  746. else:
  747. # There is a bug in tkinter included in some python 3.6 versions
  748. # that without this variable, produces a "visual" toggling of
  749. # other near checkbuttons
  750. # https://bugs.python.org/issue29402
  751. # https://bugs.python.org/issue25684
  752. var = tk.IntVar(master=self)
  753. b = tk.Checkbutton(
  754. master=self, text=text, command=command, indicatoron=False,
  755. variable=var, offrelief="flat", overrelief="groove",
  756. borderwidth=1
  757. )
  758. b.var = var
  759. b._image_file = image_file
  760. if image_file is not None:
  761. # Explicit class because ToolbarTk calls _Button.
  762. NavigationToolbar2Tk._set_image_for_button(self, b)
  763. else:
  764. b.configure(font=self._label_font)
  765. b.pack(side=tk.LEFT)
  766. return b
  767. def _Spacer(self):
  768. # Buttons are also 18pt high.
  769. s = tk.Frame(master=self, height='18p', relief=tk.RIDGE, bg='DarkGray')
  770. s.pack(side=tk.LEFT, padx='3p')
  771. return s
  772. def save_figure(self, *args):
  773. filetypes = self.canvas.get_supported_filetypes_grouped()
  774. tk_filetypes = [
  775. (name, " ".join(f"*.{ext}" for ext in exts))
  776. for name, exts in sorted(filetypes.items())
  777. ]
  778. default_extension = self.canvas.get_default_filetype()
  779. default_filetype = self.canvas.get_supported_filetypes()[default_extension]
  780. filetype_variable = tk.StringVar(self.canvas.get_tk_widget(), default_filetype)
  781. # adding a default extension seems to break the
  782. # asksaveasfilename dialog when you choose various save types
  783. # from the dropdown. Passing in the empty string seems to
  784. # work - JDH!
  785. # defaultextension = self.canvas.get_default_filetype()
  786. defaultextension = ''
  787. initialdir = os.path.expanduser(mpl.rcParams['savefig.directory'])
  788. # get_default_filename() contains the default extension. On some platforms,
  789. # choosing a different extension from the dropdown does not overwrite it,
  790. # so we need to remove it to make the dropdown functional.
  791. initialfile = pathlib.Path(self.canvas.get_default_filename()).stem
  792. fname = tkinter.filedialog.asksaveasfilename(
  793. master=self.canvas.get_tk_widget().master,
  794. title='Save the figure',
  795. filetypes=tk_filetypes,
  796. defaultextension=defaultextension,
  797. initialdir=initialdir,
  798. initialfile=initialfile,
  799. typevariable=filetype_variable
  800. )
  801. if fname in ["", ()]:
  802. return None
  803. # Save dir for next time, unless empty str (i.e., use cwd).
  804. if initialdir != "":
  805. mpl.rcParams['savefig.directory'] = (
  806. os.path.dirname(str(fname)))
  807. # If the filename contains an extension, let savefig() infer the file
  808. # format from that. If it does not, use the selected dropdown option.
  809. if pathlib.Path(fname).suffix[1:] != "":
  810. extension = None
  811. else:
  812. extension = filetypes[filetype_variable.get()][0]
  813. try:
  814. self.canvas.figure.savefig(fname, format=extension)
  815. return fname
  816. except Exception as e:
  817. tkinter.messagebox.showerror("Error saving file", str(e))
  818. def set_history_buttons(self):
  819. state_map = {True: tk.NORMAL, False: tk.DISABLED}
  820. can_back = self._nav_stack._pos > 0
  821. can_forward = self._nav_stack._pos < len(self._nav_stack) - 1
  822. if "Back" in self._buttons:
  823. self._buttons['Back']['state'] = state_map[can_back]
  824. if "Forward" in self._buttons:
  825. self._buttons['Forward']['state'] = state_map[can_forward]
  826. def add_tooltip(widget, text):
  827. tipwindow = None
  828. def showtip(event):
  829. """Display text in tooltip window."""
  830. nonlocal tipwindow
  831. if tipwindow or not text:
  832. return
  833. x, y, _, _ = widget.bbox("insert")
  834. x = x + widget.winfo_rootx() + widget.winfo_width()
  835. y = y + widget.winfo_rooty()
  836. tipwindow = tk.Toplevel(widget)
  837. tipwindow.overrideredirect(1)
  838. tipwindow.geometry(f"+{x}+{y}")
  839. try: # For Mac OS
  840. tipwindow.tk.call("::tk::unsupported::MacWindowStyle",
  841. "style", tipwindow._w,
  842. "help", "noActivates")
  843. except tk.TclError:
  844. pass
  845. label = tk.Label(tipwindow, text=text, justify=tk.LEFT,
  846. relief=tk.SOLID, borderwidth=1)
  847. label.pack(ipadx=1)
  848. def hidetip(event):
  849. nonlocal tipwindow
  850. if tipwindow:
  851. tipwindow.destroy()
  852. tipwindow = None
  853. widget.bind("<Enter>", showtip)
  854. widget.bind("<Leave>", hidetip)
  855. @backend_tools._register_tool_class(FigureCanvasTk)
  856. class RubberbandTk(backend_tools.RubberbandBase):
  857. def draw_rubberband(self, x0, y0, x1, y1):
  858. NavigationToolbar2Tk.draw_rubberband(
  859. self._make_classic_style_pseudo_toolbar(), None, x0, y0, x1, y1)
  860. def remove_rubberband(self):
  861. NavigationToolbar2Tk.remove_rubberband(
  862. self._make_classic_style_pseudo_toolbar())
  863. class ToolbarTk(ToolContainerBase, tk.Frame):
  864. def __init__(self, toolmanager, window=None):
  865. ToolContainerBase.__init__(self, toolmanager)
  866. if window is None:
  867. window = self.toolmanager.canvas.get_tk_widget().master
  868. xmin, xmax = self.toolmanager.canvas.figure.bbox.intervalx
  869. height, width = 50, xmax - xmin
  870. tk.Frame.__init__(self, master=window,
  871. width=int(width), height=int(height),
  872. borderwidth=2)
  873. self._label_font = tkinter.font.Font(size=10)
  874. # This filler item ensures the toolbar is always at least two text
  875. # lines high. Otherwise the canvas gets redrawn as the mouse hovers
  876. # over images because those use two-line messages which resize the
  877. # toolbar.
  878. label = tk.Label(master=self, font=self._label_font,
  879. text='\N{NO-BREAK SPACE}\n\N{NO-BREAK SPACE}')
  880. label.pack(side=tk.RIGHT)
  881. self._message = tk.StringVar(master=self)
  882. self._message_label = tk.Label(master=self, font=self._label_font,
  883. textvariable=self._message)
  884. self._message_label.pack(side=tk.RIGHT)
  885. self._toolitems = {}
  886. self.pack(side=tk.TOP, fill=tk.X)
  887. self._groups = {}
  888. def _rescale(self):
  889. return NavigationToolbar2Tk._rescale(self)
  890. def add_toolitem(
  891. self, name, group, position, image_file, description, toggle):
  892. frame = self._get_groupframe(group)
  893. buttons = frame.pack_slaves()
  894. if position >= len(buttons) or position < 0:
  895. before = None
  896. else:
  897. before = buttons[position]
  898. button = NavigationToolbar2Tk._Button(frame, name, image_file, toggle,
  899. lambda: self._button_click(name))
  900. button.pack_configure(before=before)
  901. if description is not None:
  902. add_tooltip(button, description)
  903. self._toolitems.setdefault(name, [])
  904. self._toolitems[name].append(button)
  905. def _get_groupframe(self, group):
  906. if group not in self._groups:
  907. if self._groups:
  908. self._add_separator()
  909. frame = tk.Frame(master=self, borderwidth=0)
  910. frame.pack(side=tk.LEFT, fill=tk.Y)
  911. frame._label_font = self._label_font
  912. self._groups[group] = frame
  913. return self._groups[group]
  914. def _add_separator(self):
  915. return NavigationToolbar2Tk._Spacer(self)
  916. def _button_click(self, name):
  917. self.trigger_tool(name)
  918. def toggle_toolitem(self, name, toggled):
  919. if name not in self._toolitems:
  920. return
  921. for toolitem in self._toolitems[name]:
  922. if toggled:
  923. toolitem.select()
  924. else:
  925. toolitem.deselect()
  926. def remove_toolitem(self, name):
  927. for toolitem in self._toolitems.pop(name, []):
  928. toolitem.pack_forget()
  929. def set_message(self, s):
  930. self._message.set(s)
  931. @backend_tools._register_tool_class(FigureCanvasTk)
  932. class SaveFigureTk(backend_tools.SaveFigureBase):
  933. def trigger(self, *args):
  934. NavigationToolbar2Tk.save_figure(
  935. self._make_classic_style_pseudo_toolbar())
  936. @backend_tools._register_tool_class(FigureCanvasTk)
  937. class ConfigureSubplotsTk(backend_tools.ConfigureSubplotsBase):
  938. def trigger(self, *args):
  939. NavigationToolbar2Tk.configure_subplots(self)
  940. @backend_tools._register_tool_class(FigureCanvasTk)
  941. class HelpTk(backend_tools.ToolHelpBase):
  942. def trigger(self, *args):
  943. dialog = SimpleDialog(
  944. self.figure.canvas._tkcanvas, self._get_help_text(), ["OK"])
  945. dialog.done = lambda num: dialog.frame.master.withdraw()
  946. Toolbar = ToolbarTk
  947. FigureManagerTk._toolbar2_class = NavigationToolbar2Tk
  948. FigureManagerTk._toolmanager_toolbar_class = ToolbarTk
  949. @_Backend.export
  950. class _BackendTk(_Backend):
  951. backend_version = tk.TkVersion
  952. FigureCanvas = FigureCanvasTk
  953. FigureManager = FigureManagerTk
  954. mainloop = FigureManagerTk.start_main_loop