Implements flexbox like functionality to tkinter. Makes creating a reactive GUI far easier.
Project description
tkflexbox
A python library to implement flexbox like functionality to tkinter.
Latest release: I have fully recoded the internal engine so that now I store all details about widgets as well as their ID for easy manipulation. I should now be able to start adding wrapping and auto placement :D
Roadmap
- Create dynamic grid sizing engine base thing
- Add margin paremeter functionality
- Create place_widget function
- Add direction parameter ('row' | 'column')
- Dynanmic wrapping to start new rows
- Track widget cells
- Track widget size
- Create a declarative API such as flex.add(widget, grow=2, shrink=1, basis=0.3, align='center') rather than using place_widget
- Recalculate layout based on window size
- scrolling integration if can't fit content on page
- Alignment (justify-content / align-items)
- Gap system (gap, row_gap, column_gap)
- Z-index property
See the open issues for a full list of proposed features (and known issues).
install
pip install tkflexbox
example usage
import tkflexbox
from tkflexbox import FlexBoxPage
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('800x800')
page = FlexBoxPage(root, rows=5, columns=5)
button1 = page.place_widget(ttk.Button, row=1, column=2, columnspan=2, text='Button 1', margin=(0.1, 0.05, 0.05, 0.2))
label1 = page.place_widget(ttk.Label, row=0, column=0, text=f'Button 1\'s id is {button1}')
label2 = page.place_widget(ttk.Label, row=1, column=0, text=f'Label 1\'s id is {label1}')
root.mainloop()
Wiki:
FlexBoxPage class (recommended):
When using tkflexbox I recommend creating a class for each page in your app, and having that class inherit FlexBoxFrame and then call super().init()
Since FlexBoxPage already inherits tk.Frame you can then just use this new class as a Frame/Page as you normally would in tkinter, but with the ease of strictly expanding elements to create a nice reactive application.
To create a FlexBoxPage object, use
flexboxpage = FlexBoxPage(parent, rows=10, columns=10)
This creates a Frame/Page that is split up evenly into rows and columns. This grid will expand to fill the current page, and so will elements inside making the page reactive.
Methods:
place_widget
flexboxpage.place_widget(widget, row, column, rowspan=1, columnspan=1, relx=0, rely=0, relwidth=1, relheight=1, margin=0, **kwargs)
This places a widget into a strict box. The strict box's area is defined using row, column, rowspan and columnspan. The widget will fill this frame by default but by using the margin feature you choose to only fill a portion of this subframe. The method also returns the widget so that you can modify it using normal tkinter methods.
Example:
MainMenuButton = flexboxpage.place_widget(ttk.Button, row=1, column=2, columnspan=2, text='Hello world', margin=(0.1, 0.05, 0.05, 0.2))
StrictGridFrame (deprecated for now, I might add back in future.)
flexboxpage.StrictGridFrame(self, column=0, row=0, columnspan=1, rowspan=1)
This creates a subFrame for you to use however you wish. If adding a widget I would recommend using place_widget but there will be times that you must create your own frame and this is an easier way of doing so rather than using my other function not owned by the page class to do so.
This function also returns the Frame so that it can be stored/modified later.
Example:
MyFrame = flexboxframe.StrictGridFrame(self, column=2, row=4, columnspan=5, rowspan=1)
Functions
[!WARNING]
It is recommended to use the FlexBoxFrame class as this better follows OOP principles and will result in cleaner and possibly more performant code. Only use external functions for rapid prototyping or if you can't use OOP for some reason.
StrictGridFrame
flexboxframe.StrictGridFrame(self, column=0, row=0, columnspan=1, rowspan=1)
This creates a subFrame for you to use however you wish. Returns the frame to be stored/modified later.
Example:
MyFrame = StrictGridFrame(self, column=2, row=4, columnspan=5, rowspan=1)
FullPageGrid
FullPageGrid(parent, rows=10, columns=10)
Creates rows amount of equal rows and columns amount of equal columns in the parent page. All are weighted and so expand to fill the page.
Example:
root = tk.Tk()
FullPageGrid(root, rows=10, columns=10)
Note that this does NOT return the page, only modifies the existing one.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tkflexbox-1.1.0.tar.gz.
File metadata
- Download URL: tkflexbox-1.1.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5bfc6ee11964f8a902c4aa68221684f0d6fb3e68b12c45a6a9beab7f42eb244a
|
|
| MD5 |
2e050aad0d4ddd646bfac0e96bae2583
|
|
| BLAKE2b-256 |
1591ecd300b816946db7d13e3c047e77412880322c6d76ac866baa6334fa35e5
|
File details
Details for the file tkflexbox-1.1.0-py3-none-any.whl.
File metadata
- Download URL: tkflexbox-1.1.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d90279f9f6e895291dbeb0598d08ad85f5c488d214dc3c05275375b8682a548d
|
|
| MD5 |
7b30416971ca48c33b0df9e85003f698
|
|
| BLAKE2b-256 |
bd9e2bbf65d5bbc09e93246941849607ec108444da72e862a9ed76bd4575b34a
|