Skip to content

Commit

Permalink
docs(examples): add tkinter file browser example
Browse files Browse the repository at this point in the history
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
psavery committed Jul 19, 2022
1 parent 49065a0 commit 190b37d
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions examples/validation/core/21_file_browser.py
@@ -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()

0 comments on commit 190b37d

Please sign in to comment.