wizard-tk-bridge
wizard-ui-bridge is
the user-interface-independent way for a wizard to ask a user questions.
wizard-tk-bridge is the Tk (Tkinter) implementation of this
user-interface-independent way for a wizard to ask a user questions.
A wizard written for wizard-ui-bridge works out of the box if
passed a wizard-tk-bridge as bridge.
Is this package for you?
This package is a good fit when one or more of these apply:
- You have a wizard written for
wizard-ui-bridge and want to run it in
a graphical user interface, without changing a line of the wizard.
- Your application is a Tkinter application, and its questions belong in
real widgets rather than in a terminal.
- You want what a console cannot offer: a native file and directory
picker, a month calendar for dates, help texts as hover tooltips, an
editable table with buttons that add and remove rows, and a whole form
or matrix on one screen that the user fills in any order.
- Your program is otherwise a plain command-line tool, and you want to
ask through a window without writing any Tkinter code of your own.
- You want to decide where the wizard appears: in a window of its own
over your application, or inside a panel your application built.
This package is probably not the right one when:
- Your program runs in a terminal, or on a machine with no display. Use
the console or Textual bridge of
wizard-ui-bridge.
- Your application uses another graphical toolkit, such as Qt, wxPython
or GTK. Write a bridge for that toolkit instead, by subclassing
WizardUiBridge.
- You want a general widget toolkit. Use Tkinter directly.
Installation
wizard-tk-bridge requires Python 3.12 or newer, and the standard
library module tkinter.
pip install --upgrade wizard-tk-bridge
This installs
wizard-ui-bridge as well,
since a Tk bridge is a WizardUiBridge.
tkinter belongs to the standard library, but it is not a pure Python
module, and several Linux distributions leave it out of their base
Python package. A standard library module is not something pip can
install, so add it with the system package manager instead:
sudo apt install python3-tk # Debian, Ubuntu
sudo dnf install python3-tkinter # Fedora, RHEL
The usual CPython installers for Windows and macOS already include it.
Quick start
Ask a few questions from a program that has no window of its own:
from wizard_tk_bridge import WizardUiBridgeTk
bridge = WizardUiBridgeTk()
try:
name = bridge.ask_text('Your name?', nullable=True)
topping = bridge.ask_choice('Favorite topping?',
choices=('Mushroom', 'Pepperoni', 'Olive'))
cheese = bridge.ask_yes_no('Extra cheese?', default=False)
finally:
bridge.close()
print(f'{name or "Anonymous"} orders {topping}, extra cheese: {cheese}')
Every ask method blocks until the user answers, so the wizard stays
straight-line code. No mainloop() call is needed: the ask methods pump
the Tk event loop themselves and return as soon as the answer is in, and
the program is not left running an event loop once the wizard closes.
All questions of one session are asked in the same reused window, so the
session does not jump around the display.
Every question also carries Back, Out one level and Abort
buttons, which raise WizardBack, WizardCancelLevel and WizardAbort
instead of returning an answer. Closing the wizard's own window, or
pressing Cmd-W (Ctrl-W on Windows) in it, is an abort as well. The short
example above catches none of them and simply lets them propagate.
Where the wizard appears
That is the one question the Tk bridge adds to the wizard-ui-bridge API,
and the constructor answers it:
| Your application |
Arguments |
What the bridge builds |
| A command-line program with no Tkinter code |
neither |
a hidden root, and the wizard's own window over it |
| Has windows of its own |
parent=widget |
a new window over the widget's window |
| Has a container set aside for the wizard |
area=container |
the wizard's widgets inside that container |
parent and area are mutually exclusive; giving both raises
ValueError. The visible part is built on the first question, so a
bridge that is never asked anything puts nothing on the screen.
modal (default True) decides whether the wizard grabs its window —
or the window containing area — for the whole session, leaving the
rest of that window unusable meanwhile. Only the application knows
whether its other content should stay usable, which is why it decides.
An embedded wizard normally passes modal=False, since keeping the rest
of the window usable is the reason to embed rather than pop up.
close() is not optional. It releases the modal grab, destroys the
wizard's own window or clears its area, and destroys the hidden root
when the bridge owns one. Call it from a finally, because a wizard's
normal way of ending is often a WizardNavigation exception rather than
a return. Calling it twice is harmless.
What the user gets
WizardUiBridgeTk overrides every ask method except ask_table(), the
inheritable ones included — above all ask_path(), ask_form() and
ask_matrix_form(), where a graphical interface beats the base class
fallback outright. The inherited ask_table() asks the table as a
matrix form, so it is shown on one screen too:
| Ask method |
What the user gets |
ask_text |
a text entry, masked when sensitive |
ask_int |
a numeric entry, re-asked until the value parses and fits its bounds |
ask_path |
an entry with a Browse button opening the native file or directory picker |
ask_yes_no |
a Yes and a No button |
ask_choice / ask_multi |
a single- / multi-selection list |
ask_table |
an editable grid, with + Row and − Row when the question allows a variable number of rows (inherited: the table is asked as a matrix form) |
ask_form |
every field of the form on one screen, answered in any order |
ask_matrix_form |
a grid of labels and inputs on one screen, with + Row, − Row, + Column and − Column buttons when the shape may change |
In a form, a field's help_text becomes a hover tooltip rather than an
extra printed line, the float, time and duration fields show their
accepted format as greyed placeholder text, and a date or date-time
field adds a Pick button opening a month calendar.
show() appends to a message area kept above the current question, so
earlier messages stay readable as the wizard moves on. They disappear
with the window, so a final message needs a question after it to be seen
at all — exactly as on the Textual bridge.
A graphical interface has no console to write side notes to, so
error_file() returns a stream that discards what is written to it.
Pass WizardUiBridgeTk(log=stream) to send those diagnostics somewhere
real, such as an open log file.
A Tk too old for macOS 27
macOS 27 changed a behaviour in a way that makes Tk older than 9.0.5
randomly hang in some user interaction, and sometimes when it first
shows a window after the computer has started. Tk comes with Python,
so the cure is to install a newer Python.
Constructing a WizardUiBridgeTk checks this, and the first time in a
process it finds a too old Tk it warns the user: first on stderr, since
a Tk that may hang cannot be trusted to show the warning, and then in a
pop-up window. The wizard then continues.
Any program using Tkinter can do the same through two functions:
| Function |
What it does |
bad_tk_version_for_os(widget=None) |
Returns whether the Tk in use is too old for the operating system. Always False on other systems than macOS 27 and later. |
warn_if_bad_tk_version(widget=None, *, pop_up=True) |
Warns the user as above, once per process, and returns the same answer. |
Give any widget the program already has: reading the exact Tk version
needs Tk loaded, so without a widget a root is built and destroyed for
the check.
A test suite can skip a test known to hang on a too old Tk by marking it
@pytest.mark.hangs_on_old_tk, after hooking up
wizard_tk_bridge.old_tk_pytest in its conftest.py:
import pytest
from wizard_tk_bridge.old_tk_pytest import register_old_tk, skip_on_old_tk
def pytest_configure(config: pytest.Config) -> None:
"""Register the hangs_on_old_tk marker."""
register_old_tk(config)
def pytest_runtest_setup(item: pytest.Item) -> None:
"""Skip a test marked hangs_on_old_tk when Tk is too old for the OS."""
skip_on_old_tk(item)
skip_on_old_tk() also writes the warning to stderr only, so the pop-up
of a WizardUiBridgeTk built by a test does not block the test run.
Relation to wizard-ui-bridge
wizard-tk-bridge and wizard-ui-bridge are built from the same
repository and released together, so a version of this package requires
the matching version of wizard-ui-bridge, or newer.
Documentation
License
MIT
Test summary
- Test result: 2066 passed, 9 deselected in 63s (0:01:03)
- No flake8 warnings.
- No mypy errors found.
- No pylint warnings.
- No python layout warnings.
- Built version(s): 1.6
- Build and test using Python 3.15.2