Font Dialog#
bs.ask_font() opens a modal font selector and returns the selected font.
FontDialog gives the same selector as a reusable object.
Usage#
Convenience function#
bs.ask_font() is the one-liner shorthand. Pass default_font= a font
token to pre-select the starting font:
choice = bs.ask_font(title="Body Font")
if choice:
print(choice.family) # e.g. 'Segoe UI'
print(choice.size) # e.g. 11
The return value is a FontChoice with six attributes:
Attribute |
Description |
|---|---|
|
Font family name, e.g. |
|
Point size (int). |
|
|
|
|
|
|
|
|
default_font= accepts any font token ('body', 'code',
'heading-lg', …); it defaults to 'body'. See Typography
for the full token list. None is returned if the user cancels.
Reusable dialog object#
Use FontDialog when you need to inspect the result after show():
from bootstack.dialogs import FontDialog
dlg = FontDialog(title="Code Font", default_font="code")
dlg.show()
if dlg.result:
family = dlg.result.family
size = dlg.result.size
Using the result#
FontChoice is a plain namedtuple, so its fields read directly:
choice = bs.ask_font()
if choice:
print(f"{choice.family} {choice.size}pt "
f"{choice.weight} {choice.slant}")
See also#
Color Dialog — ask_color() for choosing a color.
Input Dialogs — ask_string(), ask_integer(), and other value-input dialogs.
API#
The complete reference for ask_font() and
FontDialog (and its FontChoice result)
lives on the Dialogs API page. At a glance:
Show a font selector dialog. |
|
A dialog for selecting a font family, size, weight, slant, and effects. |
|
A font selected from |
Full Example#
1
2from bootstack.dialogs import FontDialog
3def show_font():
4 choice = bs.ask_font(title="Select Font")
5 if choice:
6 print(f"Selected: {choice.family} {choice.size}pt {choice.weight}")
7
8def show_code():
9 dlg = FontDialog(title="Code Font", default_font="code")
10 dlg.show()
11 if dlg.result:
12 print(f"font={dlg.result.family} {dlg.result.size}pt {dlg.result.weight}")
13
14with bs.App(title="Font Dialog", size=(1000, 800), padding=20, gap=16) as app:
15
16 bs.Label("Font Dialog", font="heading-sm")
17 with bs.HStack(gap=8):
18 bs.Button("ask_font()", on_click=show_font)
19 bs.Button("FontDialog()", on_click=show_code)
20
21app.run()