tk.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. """
  2. Tkinter GUI progressbar decorator for iterators.
  3. Usage:
  4. >>> from tqdm.tk import trange, tqdm
  5. >>> for i in trange(10):
  6. ... ...
  7. """
  8. import re
  9. import sys
  10. import tkinter
  11. import tkinter.ttk as ttk
  12. from warnings import warn
  13. from .std import TqdmExperimentalWarning, TqdmWarning
  14. from .std import tqdm as std_tqdm
  15. __author__ = {"github.com/": ["richardsheridan", "casperdcl"]}
  16. __all__ = ['tqdm_tk', 'ttkrange', 'tqdm', 'trange']
  17. class tqdm_tk(std_tqdm): # pragma: no cover
  18. """
  19. Experimental Tkinter GUI version of tqdm!
  20. Note: Window interactivity suffers if `tqdm_tk` is not running within
  21. a Tkinter mainloop and values are generated infrequently. In this case,
  22. consider calling `tqdm_tk.refresh()` frequently in the Tk thread.
  23. """
  24. # TODO: @classmethod: write()?
  25. def __init__(self, *args, **kwargs):
  26. """
  27. This class accepts the following parameters *in addition* to
  28. the parameters accepted by `tqdm`.
  29. Parameters
  30. ----------
  31. grab : bool, optional
  32. Grab the input across all windows of the process.
  33. tk_parent : `tkinter.Wm`, optional
  34. Parent Tk window.
  35. cancel_callback : Callable, optional
  36. Create a cancel button and set `cancel_callback` to be called
  37. when the cancel or window close button is clicked.
  38. """
  39. kwargs = kwargs.copy()
  40. kwargs['gui'] = True
  41. # convert disable = None to False
  42. kwargs['disable'] = bool(kwargs.get('disable', False))
  43. self._warn_leave = 'leave' in kwargs
  44. grab = kwargs.pop('grab', False)
  45. tk_parent = kwargs.pop('tk_parent', None)
  46. self._cancel_callback = kwargs.pop('cancel_callback', None)
  47. super(tqdm_tk, self).__init__(*args, **kwargs)
  48. if self.disable:
  49. return
  50. if tk_parent is None: # Discover parent widget
  51. try:
  52. tk_parent = tkinter._default_root
  53. except AttributeError:
  54. raise AttributeError(
  55. "`tk_parent` required when using `tkinter.NoDefaultRoot()`")
  56. if tk_parent is None: # use new default root window as display
  57. self._tk_window = tkinter.Tk()
  58. else: # some other windows already exist
  59. self._tk_window = tkinter.Toplevel()
  60. else:
  61. self._tk_window = tkinter.Toplevel(tk_parent)
  62. warn("GUI is experimental/alpha", TqdmExperimentalWarning, stacklevel=2)
  63. self._tk_dispatching = self._tk_dispatching_helper()
  64. self._tk_window.protocol("WM_DELETE_WINDOW", self.cancel)
  65. self._tk_window.wm_title(self.desc)
  66. self._tk_window.wm_attributes("-topmost", 1)
  67. self._tk_window.after(0, lambda: self._tk_window.wm_attributes("-topmost", 0))
  68. self._tk_n_var = tkinter.DoubleVar(self._tk_window, value=0)
  69. self._tk_text_var = tkinter.StringVar(self._tk_window)
  70. pbar_frame = ttk.Frame(self._tk_window, padding=5)
  71. pbar_frame.pack()
  72. _tk_label = ttk.Label(pbar_frame, textvariable=self._tk_text_var,
  73. wraplength=600, anchor="center", justify="center")
  74. _tk_label.pack()
  75. self._tk_pbar = ttk.Progressbar(
  76. pbar_frame, variable=self._tk_n_var, length=450)
  77. if self.total is not None:
  78. self._tk_pbar.configure(maximum=self.total)
  79. else:
  80. self._tk_pbar.configure(mode="indeterminate")
  81. self._tk_pbar.pack()
  82. if self._cancel_callback is not None:
  83. _tk_button = ttk.Button(pbar_frame, text="Cancel", command=self.cancel)
  84. _tk_button.pack()
  85. if grab:
  86. self._tk_window.grab_set()
  87. def close(self):
  88. if self.disable:
  89. return
  90. self.disable = True
  91. with self.get_lock():
  92. self._instances.remove(self)
  93. def _close():
  94. self._tk_window.after('idle', self._tk_window.destroy)
  95. if not self._tk_dispatching:
  96. self._tk_window.update()
  97. self._tk_window.protocol("WM_DELETE_WINDOW", _close)
  98. # if leave is set but we are self-dispatching, the left window is
  99. # totally unresponsive unless the user manually dispatches
  100. if not self.leave:
  101. _close()
  102. elif not self._tk_dispatching:
  103. if self._warn_leave:
  104. warn("leave flag ignored if not in tkinter mainloop",
  105. TqdmWarning, stacklevel=2)
  106. _close()
  107. def clear(self, *_, **__):
  108. pass
  109. def display(self, *_, **__):
  110. self._tk_n_var.set(self.n)
  111. d = self.format_dict
  112. # remove {bar}
  113. d['bar_format'] = (d['bar_format'] or "{l_bar}<bar/>{r_bar}").replace(
  114. "{bar}", "<bar/>")
  115. msg = self.format_meter(**d)
  116. if '<bar/>' in msg:
  117. msg = "".join(re.split(r'\|?<bar/>\|?', msg, 1))
  118. self._tk_text_var.set(msg)
  119. if not self._tk_dispatching:
  120. self._tk_window.update()
  121. def set_description(self, desc=None, refresh=True):
  122. self.set_description_str(desc, refresh)
  123. def set_description_str(self, desc=None, refresh=True):
  124. self.desc = desc
  125. if not self.disable:
  126. self._tk_window.wm_title(desc)
  127. if refresh and not self._tk_dispatching:
  128. self._tk_window.update()
  129. def cancel(self):
  130. """
  131. `cancel_callback()` followed by `close()`
  132. when close/cancel buttons clicked.
  133. """
  134. if self._cancel_callback is not None:
  135. self._cancel_callback()
  136. self.close()
  137. def reset(self, total=None):
  138. """
  139. Resets to 0 iterations for repeated use.
  140. Parameters
  141. ----------
  142. total : int or float, optional. Total to use for the new bar.
  143. """
  144. if hasattr(self, '_tk_pbar'):
  145. if total is None:
  146. self._tk_pbar.configure(maximum=100, mode="indeterminate")
  147. else:
  148. self._tk_pbar.configure(maximum=total, mode="determinate")
  149. super(tqdm_tk, self).reset(total=total)
  150. @staticmethod
  151. def _tk_dispatching_helper():
  152. """determine if Tkinter mainloop is dispatching events"""
  153. codes = {tkinter.mainloop.__code__, tkinter.Misc.mainloop.__code__}
  154. for frame in sys._current_frames().values():
  155. while frame:
  156. if frame.f_code in codes:
  157. return True
  158. frame = frame.f_back
  159. return False
  160. def ttkrange(*args, **kwargs):
  161. """Shortcut for `tqdm.tk.tqdm(range(*args), **kwargs)`."""
  162. return tqdm_tk(range(*args), **kwargs)
  163. # Aliases
  164. tqdm = tqdm_tk
  165. trange = ttkrange