class tqdm_tk(std_tqdm): # pragma: no cover """
Experimental Tkinter GUI version of tqdm!
Note: Window interactivity suffers if `tqdm_tk` isnot running within
a Tkinter mainloop and values are generated infrequently. In this case,
consider calling `tqdm_tk.refresh()` frequently in the Tk thread. """
# TODO: @classmethod: write()?
def __init__(self, *args, **kwargs): """
This class accepts the following parameters *in addition* to
the parameters accepted by `tqdm`.
Parameters
----------
grab : bool, optional
Grab the input across all windows of the process.
tk_parent : `tkinter.Wm`, optional
Parent Tk window.
cancel_callback : Callable, optional
Create a cancel button and set `cancel_callback` to be called
when the cancel or window close button is clicked. """
kwargs = kwargs.copy()
kwargs['gui'] = True # convert disable = None to False
kwargs['disable'] = bool(kwargs.get('disable', False))
self._warn_leave = 'leave'in kwargs
grab = kwargs.pop('grab', False)
tk_parent = kwargs.pop('tk_parent', None)
self._cancel_callback = kwargs.pop('cancel_callback', None)
super(tqdm_tk, self).__init__(*args, **kwargs)
if self.disable: return
if tk_parent isNone: # Discover parent widget try:
tk_parent = tkinter._default_root except AttributeError: raise AttributeError( "`tk_parent` required when using `tkinter.NoDefaultRoot()`") if tk_parent isNone: # use new default root window as display
self._tk_window = tkinter.Tk() else: # some other windows already exist
self._tk_window = tkinter.Toplevel() else:
self._tk_window = tkinter.Toplevel(tk_parent)
warn("GUI is experimental/alpha", TqdmExperimentalWarning, stacklevel=2)
self._tk_dispatching = self._tk_dispatching_helper()
# if leave is set but we are self-dispatching, the left window is # totally unresponsive unless the user manually dispatches ifnot self.leave:
_close() elifnot self._tk_dispatching: if self._warn_leave:
warn("leave flag ignored if not in tkinter mainloop",
TqdmWarning, stacklevel=2)
_close()
def cancel(self): """
`cancel_callback()` followed by `close()`
when close/cancel buttons clicked. """ if self._cancel_callback isnotNone:
self._cancel_callback()
self.close()
def reset(self, total=None): """
Resets to 0 iterations for repeated use.
Parameters
----------
total : int or float, optional. Total to use for the new bar. """ if hasattr(self, '_tk_pbar'): if total isNone:
self._tk_pbar.configure(maximum=100, mode="indeterminate") else:
self._tk_pbar.configure(maximum=total, mode="determinate")
super(tqdm_tk, self).reset(total=total)
@staticmethod def _tk_dispatching_helper(): """determine if Tkinter mainloop is dispatching events"""
codes = {tkinter.mainloop.__code__, tkinter.Misc.mainloop.__code__} for frame in sys._current_frames().values(): while frame: if frame.f_code in codes: returnTrue
frame = frame.f_back returnFalse
def ttkrange(*args, **kwargs): """
A shortcut for `tqdm.tk.tqdm(xrange(*args), **kwargs)`.
On Python3+, `range` is used instead of `xrange`. """ return tqdm_tk(_range(*args), **kwargs)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.