def show(widget: tk.Misc, text, mouse_x, mouse_y): """Show the context window.""" context_label['text'] = text window.deiconify() window.update_idletasks() window.lift() # We're going to position tooltips towards the center of the main window. # That way they don't tend to stick out, even in multi-window setups. # To decide where to put the tooltip, we first want the center of the # main window. cent_x = TK_ROOT.winfo_rootx() + TK_ROOT.winfo_width() / 2 cent_y = TK_ROOT.winfo_rooty() + TK_ROOT.winfo_height() / 2 x_centered = y_centered = True # If the widget is smaller than the context window, always center. if widget.winfo_width() > window.winfo_width(): if cent_x > mouse_x + CENT_DIST: # Left of center, so place right of the target x = widget.winfo_rootx() + widget.winfo_width() + PADDING x_centered = False elif cent_x < mouse_x - CENT_DIST: # Right of center, so place left of the target x = widget.winfo_rootx() - window.winfo_width() - PADDING x_centered = False if widget.winfo_height() > window.winfo_height(): if cent_y > mouse_y + CENT_DIST: # Above center, so place below target y = widget.winfo_rooty() + widget.winfo_height() + PADDING y_centered = False elif cent_y < mouse_y - CENT_DIST: # Below center, so place above target y = widget.winfo_rooty() - window.winfo_height() - PADDING y_centered = False if x_centered: # Center horizontally x = ( widget.winfo_rootx() + (widget.winfo_width() - window.winfo_width()) // 2 ) if y_centered: y = ( widget.winfo_rooty() + (widget.winfo_height() - window.winfo_height()) // 2 ) # If both X and Y are centered, the tooltip will appear on top of # the mouse and immediately hide. Offset it to fix that. if x_centered: if mouse_y < cent_y: y = widget.winfo_rooty() + widget.winfo_height() + PADDING else: y = widget.winfo_rooty() - window.winfo_height() - PADDING window.geometry('+{}+{}'.format(int(x), int(y)))
def bounds_from_object(obj: tk.Misc): """ Generate a bounding box for a widget relative to its parent which can then be used to position the highlight or by any other position dependent action. :param obj: a tk object :return: """ obj.update_idletasks() x1 = obj.winfo_x() y1 = obj.winfo_y() x2 = obj.winfo_width() + x1 y2 = obj.winfo_height() + y1 return x1, y1, x2, y2
def _show(widget: tk.Misc, mouse_x, mouse_y) -> None: """Show the context window.""" # noinspection PyUnresolvedReferences, PyProtectedMember context_label['text'] = widget._bee2_tooltip_text # noinspection PyUnresolvedReferences, PyProtectedMember context_label['image'] = widget._bee2_tooltip_img window.deiconify() window.update_idletasks() window.lift() # We're going to position tooltips towards the center of the main window. # That way they don't tend to stick out, even in multi-window setups. # To decide where to put the tooltip, we first want the center of the # main window. cent_x = TK_ROOT.winfo_rootx() + TK_ROOT.winfo_width() / 2 cent_y = TK_ROOT.winfo_rooty() + TK_ROOT.winfo_height() / 2 x_centered = y_centered = True # If the widget is smaller than the context window, always center. if widget.winfo_width() > window.winfo_width(): if cent_x > mouse_x + CENT_DIST: # Left of center, so place right of the target x = widget.winfo_rootx() + widget.winfo_width() + PADDING x_centered = False elif cent_x < mouse_x - CENT_DIST: # Right of center, so place left of the target x = widget.winfo_rootx() - window.winfo_width() - PADDING x_centered = False if widget.winfo_height() > window.winfo_height(): if cent_y > mouse_y + CENT_DIST: # Above center, so place below target y = widget.winfo_rooty() + widget.winfo_height() + PADDING y_centered = False elif cent_y < mouse_y - CENT_DIST: # Below center, so place above target y = widget.winfo_rooty() - window.winfo_height() - PADDING y_centered = False if x_centered: # Center horizontally x = ( widget.winfo_rootx() + (widget.winfo_width() - window.winfo_width()) // 2 ) if y_centered: y = ( widget.winfo_rooty() + (widget.winfo_height() - window.winfo_height()) // 2 ) # If both X and Y are centered, the tooltip will appear on top of # the mouse and immediately hide. Offset it to fix that. if x_centered: if mouse_y < cent_y: y = widget.winfo_rooty() + widget.winfo_height() + PADDING else: y = widget.winfo_rooty() - window.winfo_height() - PADDING window.geometry('+{}+{}'.format(int(x), int(y)))