Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(examples): add tkinter file browser example
This adds an example that uses tkinter for the selection of a directory. Signed-off-by: Patrick Avery <patrick.avery@kitware.com>
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from tkinter import filedialog, Tk | ||
|
||
from trame.app import get_server | ||
from trame.ui.vuetify import SinglePageLayout | ||
from trame.widgets import trame, vuetify | ||
|
||
# ----------------------------------------------------------------------------- | ||
# Trame setup | ||
# ----------------------------------------------------------------------------- | ||
|
||
server = get_server() | ||
state, ctrl = server.state, server.controller | ||
|
||
# Keep track of the currently selected directory | ||
state.selected_dir = "None" | ||
|
||
# Ensure the tkinter main window is hidden | ||
Tk().withdraw() | ||
|
||
|
||
def open_directory(): | ||
kwargs = { | ||
"title": "Select Directory", | ||
} | ||
dirpath = filedialog.askdirectory(**kwargs) | ||
if not dirpath: | ||
# User canceled. | ||
print("Canceled") | ||
return | ||
|
||
print("Selected directory:", dirpath) | ||
state.selected_dir = dirpath | ||
|
||
|
||
# The controller would let us set up callback logic in a separate file | ||
ctrl.open_directory = open_directory | ||
|
||
# ----------------------------------------------------------------------------- | ||
# UI setup | ||
# ----------------------------------------------------------------------------- | ||
|
||
layout = SinglePageLayout(server) | ||
|
||
with layout: | ||
|
||
# Toolbar | ||
with layout.toolbar as toolbar: | ||
toolbar.clear() | ||
|
||
vuetify.VSpacer() | ||
|
||
vuetify.VBtn("Select Directory", click=ctrl.open_directory) | ||
|
||
vuetify.VSpacer() | ||
|
||
with layout.content: | ||
with vuetify.VContainer() as container: | ||
container.add_child("Selected Directory: {{ selected_dir }}") | ||
|
||
# ----------------------------------------------------------------------------- | ||
# start server | ||
# ----------------------------------------------------------------------------- | ||
|
||
if __name__ == "__main__": | ||
server.start() |