ncvue.ncvwidgets

Widget functions for ncvue.

Convenience functions for adding Tkinter widgets.

This module was written by Matthias Cuntz while at Institut National de Recherche pour l’Agriculture, l’Alimentation et l’Environnement (INRAE), Nancy, France.

Copyright (c) 2020-2021 Matthias Cuntz - mc (at) macu (dot) de

Released under the MIT License; see LICENSE file for details.

History:

  • Written Nov-Dec 2020 by Matthias Cuntz (mc (at) macu (dot) de)

  • Added tooltips to all widgets with class Tooltip, Jan 2021, Matthias Cuntz

  • Added add_tooltip widget, Jan 2021, Matthias Cuntz

  • add_spinbox returns also label widget, Jan 2021, Matthias Cuntz

The following functions are provided:

Tooltip(anchor_widget, text[, hover_delay])

A tooltip that pops up when a mouse hovers over an anchor widget.

add_checkbutton(frame[, label, value, …])

Add a left-aligned ttk.Checkbutton.

add_combobox(frame[, label, values, …])

Add a left-aligned ttk.Combobox with a ttk.Label before.

add_entry(frame[, label, text, command, tooltip])

Add a left-aligned ttk.Entry with a ttk.Label before.

add_imagemenu(frame[, label, values, …])

Add a left-aligned menu with menubuttons having text and images with a ttk.Label before.

add_menu(frame[, label, values, command, …])

Add a left-aligned menu with menubuttons with a ttk.Label before.

add_scale(frame[, label, ini, tooltip])

Add a left-aligned ttk.Scale with a ttk.Label before.

add_spinbox(frame[, label, values, command, …])

Add a left-aligned tk.Spinbox with a ttk.Label before.

add_tooltip(frame[, tooltip])

Add a writeable tooltip to a widget using the class Tooltip.

class Tooltip(anchor_widget, text, hover_delay=1000)[source]

Bases: idlelib.tooltip.Hovertip

A tooltip that pops up when a mouse hovers over an anchor widget.

This is a copy of the class Hovertip of Python’s idlelib/tooltip.py. In addition, it sets the foreground colour to see the tip also in macOS dark mode, and displays a textvariable rather than simple text so one can change the tip during run time.

Methods

get_position()

choose a screen position for the tooltip

hidetip()

hide the tooltip

position_window()

(re)-set the tooltip’s screen position

schedule()

schedule the future display of the tooltip

showcontents()

content display hook for sub-classes

showtip()

display the tooltip

unschedule()

cancel the future display of the tooltip

get_position()

choose a screen position for the tooltip

hidetip()

hide the tooltip

position_window()

(re)-set the tooltip’s screen position

schedule()

schedule the future display of the tooltip

showcontents()[source]

content display hook for sub-classes

showtip()

display the tooltip

unschedule()

cancel the future display of the tooltip

add_checkbutton(frame, label='', value=False, command=None, tooltip='', **kwargs)[source]

Add a left-aligned ttk.Checkbutton.

Parameters
frametk widget

Parent widget

labelstr, optional

Text that appears on the checkbutton (default: “”)

valuebool, optional

Initial state of the checkbutton (default: False)

commandfunction, optional

Function to be called whenever the state of the checkbutton changes (default: None).

tooltipstr, optional

Tooltip appearing after one second when hovering over the checkbutton (default: “” = no tooltip)

**kwargsoption=value pairs, optional

All other options will be passed to ttk.Checkbutton

Returns
tk.StringVar, tk.BooleanVar

variable for the text on the checkbutton, control variable tracking current state of checkbutton

tk.StringVar

variable for the text of the tooltip, if given.

Examples

>>> self.rowzxy = ttk.Frame(self)
>>> self.rowzxy.pack(side=tk.TOP, fill=tk.X)
>>> self.inv_xlbl, self.inv_x = add_checkbutton(
...     self.rowzxy, label="invert x", value=False, command=self.checked)
add_combobox(frame, label='', values=[], command=None, tooltip='', **kwargs)[source]

Add a left-aligned ttk.Combobox with a ttk.Label before.

Parameters
frametk widget

Parent widget

labelstr, optional

Text that appears in front of the combobox (default: “”)

valueslist of str, optional

The choices that will appear in the drop-down menu (default: [])

commandfunction, optional

Handler function to be bound to the combobox for the event <<ComboboxSelected>> (default: None).

tooltipstr, optional

Tooltip appearing after one second when hovering over the combobox (default: “” = no tooltip)

**kwargsoption=value pairs, optional

All other options will be passed to ttk.Combobox

Returns
tk.StringVar, ttk.Combobox

variable for the text before the combobox, combobox widget

tk.StringVar

variable for the text of the tooltip, if given.

Examples

>>> self.rowzxy = ttk.Frame(self)
>>> self.rowzxy.pack(side=tk.TOP, fill=tk.X)
>>> self.xlbl, self.x = add_combobox(
...     self.rowzxy, label="x", values=columns, command=self.selected)
add_entry(frame, label='', text='', command=None, tooltip='', **kwargs)[source]

Add a left-aligned ttk.Entry with a ttk.Label before.

Parameters
frametk widget

Parent widget

labelstr, optional

Text that appears in front of the entry (default: “”)

textstr, optional

Initial text in the entry area (default: “”)

commandfunction, optional

Handler function to be bound to the entry for the events <Return>, ‘<Key-Return>’, <KP_Enter>, and ‘<FocusOut>’ (default: None).

tooltipstr, optional

Tooltip appearing after one second when hovering over the entry (default: “” = no tooltip)

**kwargsoption=value pairs, optional

All other options will be passed to ttk.Entry

Returns
tk.StringVar, tk.StringVar

variable for the text before the entry, variable for the text in the entry area

tk.StringVar

variable for the text of the tooltip, if given.

Examples

>>> self.rowxyopt = ttk.Frame(self)
>>> self.rowxyopt.pack(side=tk.TOP, fill=tk.X)
>>> self.lslbl, self.ls = add_entry(
...     self.rowxyopt, label="ls", text='-',
...     width=4, command=self.selected_y)
add_imagemenu(frame, label='', values=[], images=[], command=None, tooltip='', **kwargs)[source]

Add a left-aligned menu with menubuttons having text and images with a ttk.Label before.

Parameters
frametk widget

Parent widget

labelstr, optional

Text that appears in front of the menu (default: “”)

valueslist of str, optional

The choices that will appear in the drop-down menu (default: [])

imageslist of str, optional

The images before the choices that will appear in the drop-down menu (default: [])

commandfunction, optional

Handler function to be called if new menu entry chosen (default: None).

tooltipstr, optional

Tooltip appearing after one second when hovering over the menu (default: “” = no tooltip)

**kwargsoption=value pairs, optional

All other options will be passed to the main ttk.Menubutton

tk.StringVar

variable for the text of the tooltip, if given.

Returns
tk.StringVar, ttk.Menubutton

variable for the text before the menu, main tt.Menubutton widget

Examples

>>> self.rowcmap = ttk.Frame(self)
>>> self.rowcmap.pack(side=tk.TOP, fill=tk.X)
>>> self.cmaplbl, self.cmap = add_imagemenu(
...     self.rowcmap, label="cmap", values=self.cmaps,
...     images=self.imaps, command=self.selected_cmap)
>>> self.cmap['text']  = 'RdYlBu'
>>> self.cmap['image'] = self.imaps[self.cmaps.index('RdYlBu')]
add_menu(frame, label='', values=[], command=None, tooltip='', **kwargs)[source]

Add a left-aligned menu with menubuttons with a ttk.Label before.

Parameters
frametk widget

Parent widget

labelstr, optional

Text that appears in front of the menu (default: “”)

valueslist of str, optional

The choices that will appear in the drop-down menu (default: [])

commandfunction, optional

Handler function to be called if new menu entry chosen (default: None).

tooltipstr, optional

Tooltip appearing after one second when hovering over the menu (default: “” = no tooltip)

**kwargsoption=value pairs, optional

All other options will be passed to the main ttk.Menubutton

tk.StringVar

variable for the text of the tooltip, if given.

Returns
tk.StringVar, ttk.Menubutton

variable for the text before the menu, main tt.Menubutton widget

Examples

>>> self.rowzxy = ttk.Frame(self)
>>> self.rowzxy.pack(side=tk.TOP, fill=tk.X)
>>> self.xlbl, self.x = add_combobox(
...     self.rowzxy, label="x", values=columns, command=self.selected)
add_scale(frame, label='', ini=0, tooltip='', **kwargs)[source]

Add a left-aligned ttk.Scale with a ttk.Label before.

Parameters
frametk widget

Parent widget

labelstr, optional

Text that appears in front of the scale (default: “”)

inifloat, optional

Initial value of scale (default: 0)

tooltipstr, optional

Tooltip appearing after one second when hovering over the scale (default: “” = no tooltip)

**kwargsoption=value pairs, optional

All other options will be passed to ttk.Scale

Returns
tk.StringVar, tk.DoubleVar, ttk.Scale

variable for the text before the scale, value of scale, scale widget

tk.StringVar

variable for the text of the tooltip, if given.

Examples

>>> self.rowzxy = ttk.Frame(self)
>>> self.rowzxy.pack(side=tk.TOP, fill=tk.X)
>>> self.xlbl, self.x = add_scale(
...     self.rowzxy, label="x", values=columns, command=self.selected)
add_spinbox(frame, label='', values=[], command=None, tooltip='', **kwargs)[source]

Add a left-aligned tk.Spinbox with a ttk.Label before.

Parameters
frametk widget

Parent widget

labelstr, optional

Text that appears in front of the spinbox (default: “”)

valueslist of str, optional

The list of choices on the spinbox (default: [])

commandfunction, optional

Handler function bound to <Return>, ‘<Key-Return>’, <KP_Enter>, and ‘<FocusOut>’ (default: None).

tooltipstr, optional

Tooltip appearing after one second when hovering over the spinbox (default: “” = no tooltip)

**kwargsoption=value pairs, optional

All other options will be passed to tk.Spinbox

Returns
tk.StringVar, ttk.Label, tk.StringVar, tk.Spinbox

variable for the text before the spinbox, label widget, variable for the text in the spinbox area, spinbox widget

tk.StringVar

variable for the text of the tooltip, if given.

Examples

>>> self.rowlev = ttk.Frame(self)
>>> self.rowlev.pack(side=tk.TOP, fill=tk.X)
>>> self.dlval, self.dl, self.dval, self.d = add_spinbox(
...     self.rowlev, label="dim", values=range(0,10),
...     command=self.spinned)
add_tooltip(frame, tooltip='', **kwargs)[source]

Add a writeable tooltip to a widget using the class Tooltip.

Parameters
frametk widget

Parent widget

tooltipstr, optional

Tooltip appearing after one second when hovering over the parent widget (default: “” = no tooltip)

**kwargsoption=value pairs, optional

All other options will be passed to tk.StringVar

Returns
tk.StringVar

variable for the text of the tooltip.

Examples

>>> self.rowlev = ttk.Frame(self)
>>> self.rowlev.pack(side=tk.TOP, fill=tk.X)
>>> self.dlbl, self.dval, self.d = add_spinbox(
...     self.rowlev, label="dim", values=range(0,10),
...     command=self.spinned)
>>> self.dtip = add_tooltip(self.d, 'Dimension')