https://github.com/pyrustic/viewable
Class to implement a GUI view with lifecycle
https://github.com/pyrustic/viewable
app desktop event frontend gui library lifecycle lightweight pyrustic python tkinter
Last synced: 7 months ago
JSON representation
Class to implement a GUI view with lifecycle
- Host: GitHub
- URL: https://github.com/pyrustic/viewable
- Owner: pyrustic
- License: mit
- Created: 2021-06-22T21:59:22.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-02-25T01:59:50.000Z (over 2 years ago)
- Last Synced: 2024-04-25T18:21:06.574Z (over 1 year ago)
- Topics: app, desktop, event, frontend, gui, library, lifecycle, lightweight, pyrustic, python, tkinter
- Language: Python
- Homepage: https://pyrustic.github.io
- Size: 30.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![]()
# Viewable
Implement better Views for your Tkinter Python appThis project is part of the [Pyrustic Open Ecosystem](https://pyrustic.github.io).
[Installation](#installation) | [Reference](https://github.com/pyrustic/viewable/tree/master/docs/modules#readme)
## Overview
Views are the building blocks of your desktop application GUI. `Viewable` allows you to implement Views that are maintainable and easily extensible. Viewable defines a View in terms of its lifecycle. And so, you can split your source code to align with the main states a View goes through: `init`, `build`, `map`, and `destroy`.
Here's how to implement a View with `Viewable`:
```python
import tkinter as tk
from viewable import Viewableclass View(Viewable):
def __init__(self, master):
super().__init__()
self._master = masterdef _build(self):
"""
This is the only mandatory method to implement.
You define the body of the view here
"""
# the body is generally either
# a tk.Frame instance
# or a tk.Toplevel instance
self._body = tk.Frame(self._master)
label = tk.Label(self._body, text="Hello Friend !")
label.pack()def _on_map(self):
""" This method is called when the view is mapped for the first time """def _on_destroy(self):
""" This method is called when the view is destroyed """# root
root = tk.Tk()# the view
view = View(root)# the method build_pack() builds then packs the view
# In fact you could do:
# view.build() then view.pack()
# or:
# view.build() then view.body.pack()
view.build_pack() # it accepts arguments like the Tkinter pack() method# others ways to install a view:
# .build_grid(), .build_place(), .build_wait()# you can access the body of the view via
# its .body property
view.body # here, the body is a tk.Frame# To destroy a view, call the method .destroy()
view.destroy()# The .state property reveals the state of the view:
# 'new', 'built', 'mapped', 'destroyed'.
print(view.state)# mainloop
root.mainloop()```
## Installation
```bash
pip install viewable
```