ttkbootstrap
ttkbootstrap is a supercharged theme extension for tkinter that provides modern flat-style themes inspired by Bootstrap. It offers a wide range of pre-built widgets and styles, making it easy to create visually appealing and responsive GUI applications with Tkinter. The library is actively maintained with frequent minor and patch releases, typically every few weeks.
Common errors
-
TclError: image "pyimageX" doesn't exist
cause A `PhotoImage` object used by a widget (e.g., a button or label) has been garbage collected because no strong reference to it was maintained.fixStore the `PhotoImage` object as an attribute of the widget's parent (e.g., `self.my_image = PhotoImage(...)`) or as a global variable. This ensures it remains in memory as long as the widget needs it. -
ModuleNotFoundError: No module named 'ttkbootstrap.dialog'
cause Attempting to import dialogs (like `Dialog` or `Messagebox`) from an old path that was removed or changed in `ttkbootstrap` v1.19.1 and later.fixUpdate your import statement. For general dialogs, use `from ttkbootstrap.dialogs import Messagebox` or `Querybox`. For the base custom `Dialog` class, use `from ttkbootstrap.dialogs.dialog import Dialog`. -
AttributeError: 'Window' object has no attribute 'mainloop'
cause Often happens if `tb.Window()` is called but the `mainloop()` method is not invoked at the end of the script to start the Tkinter event loop.fixEnsure `app.mainloop()` (where `app` is your `tb.Window` instance) is called at the very end of your application's setup code to start the GUI event loop.
Warnings
- breaking ttkbootstrap dropped support for Python 3.9 in version 1.19.0. Applications using Python 3.9 or older will fail to install or run with newer versions.
- deprecated The import path for certain dialog classes, such as `Dialog` (for creating custom dialogs), changed from `ttkbootstrap.dialog` to `ttkbootstrap.dialogs` in version 1.19.1. While `ttkbootstrap.dialogs` now holds pre-built dialogs like `Messagebox`, the base `Dialog` class also moved there.
- gotcha When using `PhotoImage` objects (e.g., for icons on buttons or labels), ensure you maintain a reference to the image object. If the `PhotoImage` object is garbage collected, the image will disappear from the widget.
Install
-
pip install ttkbootstrap
Imports
- Window
import ttkbootstrap as tb app = tb.Window(themename="flatly")
- PhotoImage
from ttkbootstrap import PhotoImage
- Messagebox
from ttkbootstrap.dialog import Messagebox
from ttkbootstrap.dialogs import Messagebox
Quickstart
import ttkbootstrap as tb
def main():
app = tb.Window(themename="cosmo") # Try 'darkly', 'superhero', 'flatly', etc.
app.title("My ttkbootstrap App")
app.geometry("400x200")
label = tb.Label(app, text="Hello from ttkbootstrap!", font=('Helvetica', 16))
label.pack(pady=30)
button = tb.Button(app, text="Click Me", command=lambda: label.config(text="Button Clicked!"), bootstyle="primary")
button.pack(pady=10)
app.mainloop()
if __name__ == '__main__':
main()