Experimental Tkinter GUI version of tqdm! Note: Window interactivity suffers if `tqdm_tk` is not running within a Tkinter mainloop and values are generated infrequently. In this case, consider calling `tqdm_tk.refresh()` frequently in the Tk thread.
| 20 | |
| 21 | |
| 22 | class tqdm_tk(std_tqdm): # pragma: no cover |
| 23 | """ |
| 24 | Experimental Tkinter GUI version of tqdm! |
| 25 | |
| 26 | Note: Window interactivity suffers if `tqdm_tk` is not running within |
| 27 | a Tkinter mainloop and values are generated infrequently. In this case, |
| 28 | consider calling `tqdm_tk.refresh()` frequently in the Tk thread. |
| 29 | """ |
| 30 | |
| 31 | # TODO: @classmethod: write()? |
| 32 | |
| 33 | def __init__(self, *args, **kwargs): |
| 34 | """ |
| 35 | This class accepts the following parameters *in addition* to |
| 36 | the parameters accepted by `tqdm`. |
| 37 | |
| 38 | Parameters |
| 39 | ---------- |
| 40 | grab : bool, optional |
| 41 | Grab the input across all windows of the process. |
| 42 | tk_parent : `tkinter.Wm`, optional |
| 43 | Parent Tk window. |
| 44 | cancel_callback : Callable, optional |
| 45 | Create a cancel button and set `cancel_callback` to be called |
| 46 | when the cancel or window close button is clicked. |
| 47 | """ |
| 48 | kwargs = kwargs.copy() |
| 49 | kwargs['gui'] = True |
| 50 | # convert disable = None to False |
| 51 | kwargs['disable'] = bool(kwargs.get('disable', False)) |
| 52 | self._warn_leave = 'leave' in kwargs |
| 53 | grab = kwargs.pop('grab', False) |
| 54 | tk_parent = kwargs.pop('tk_parent', None) |
| 55 | self._cancel_callback = kwargs.pop('cancel_callback', None) |
| 56 | super().__init__(*args, **kwargs) |
| 57 | |
| 58 | if self.disable: |
| 59 | return |
| 60 | |
| 61 | if tk_parent is None: # Discover parent widget |
| 62 | try: |
| 63 | tk_parent = tkinter._default_root |
| 64 | except AttributeError: |
| 65 | raise AttributeError( |
| 66 | "`tk_parent` required when using `tkinter.NoDefaultRoot()`") |
| 67 | if tk_parent is None: # use new default root window as display |
| 68 | self._tk_window = tkinter.Tk() |
| 69 | else: # some other windows already exist |
| 70 | self._tk_window = tkinter.Toplevel() |
| 71 | else: |
| 72 | self._tk_window = tkinter.Toplevel(tk_parent) |
| 73 | |
| 74 | warn("GUI is experimental/alpha", TqdmExperimentalWarning, stacklevel=2) |
| 75 | self._tk_dispatching = self._tk_dispatching_helper() |
| 76 | |
| 77 | self._tk_window.protocol("WM_DELETE_WINDOW", self.cancel) |
| 78 | self._tk_window.wm_title(self.desc) |
| 79 | self._tk_window.wm_attributes("-topmost", 1) |