{ "cells": [ { "cell_type": "markdown", "id": "7c6099c5", "metadata": {}, "source": [ "## Interactive Tkinter dialog to plot spectra\n", "\n", "*Last update: June 2021*\n", "\n", "This tutorial shows how to create a basic interactive dialog to select files to read and plot XAS spectra.\n", "For this we will be using the [Tkinter](https://docs.python.org/3/library/tkinter.html) (Tk interactive) library of Python.\n", "\n", "The following steps are covered in this notebook:\n", "\n", "1. Creating a TKinter dialog to ask for filenames.\n", "2. Reading the files and plotting the contents through a specified backend.\n", "3. Wrapping both routines in a single Tk widget application.\n", "\n", "This tutorial assumes that the files that you want to visualize are available in your local machine.\n", "If no such files are available, you can download and uncompress the following example files:\n", "[p65_example_files.zip](p65_example_files.zip)" ] }, { "cell_type": "code", "execution_count": 1, "id": "677ac0f8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python version : 3.9.4\n", "Numpy version : 1.20.3\n", "Scipy version : 1.6.3\n", "Lmfit version : 1.0.2\n", "H5py version : 3.2.1\n", "Matplotlib version : 3.4.2\n", "Araucaria version : 0.1.10\n" ] } ], "source": [ "from araucaria.utils import get_version\n", "print(get_version(dependencies=True))" ] }, { "cell_type": "markdown", "id": "ddc1162a", "metadata": {}, "source": [ "### Creating a Tkinter dialog to ask for filepaths\n", "\n", "As a first step we will create a dialog to request the filepaths for the files we want to visualize. We will use the [Tk](https://docs.python.org/3/library/tkinter.html#tkinter.Tk) class to create a top-level widget, and the [askopenfilenames()](https://docs.python.org/3/library/dialog.html#tkinter.filedialog.askopenfiles) function to deploy the open dialog window.\n", "\n", "Note that we assign the retrieved files paths to the `fpaths` variable, and then destroy the top-level widget.\n", "Lets run the code and inspect the results!" ] }, { "cell_type": "code", "execution_count": 2, "id": "5dc100cd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ".../20K_GOE_Fe_K_240.00000.xdi\n" ] } ], "source": [ "from tkinter import Tk, filedialog\n", "root = Tk()\n", "fpaths = filedialog.askopenfilenames(initialdir = \"/\",title = \"Select scan file\")\n", "root.destroy()\n", "\n", "# printing filepahts\n", "for fpath in fpaths:\n", " print(fpath)" ] }, { "cell_type": "markdown", "id": "a27f40b0", "metadata": {}, "source": [ "