{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Toolbar\n\nThis demo shows how buttons are added and removed from the ``itom`` toolbar.\nFrequently used methods are thus easier to access.\nBy clicking the button, these are executed. \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from functools import partial\nfrom itom import addButton\nfrom itom import removeButton\nfrom itom import version as itomVersion\n\n\n\ndef method():\n \"\"\"Sample callback method 1.\"\"\"\n print(\"The method 'method' has been clicked\")\n\n\ndef methodArgs(arg1, arg2):\n \"\"\"Sample callback method 2.\"\"\"\n print(\"The method 'methodArgs' has been clicked. Args:\", arg1, arg2)\n\n\nclass Test:\n \"\"\"Sample class.\"\"\"\n\n def doit(self):\n \"\"\"Sample member method of this class.\"\"\"\n print(\"The member 'doit' of class 'Test' has been clicked.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If this demo is executed multiple times, try to remove all\nexisting toolbars ``demobar`` and ``otherbar``. This command is\nused and explained later.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "try:\n removeButton(\"demobar\")\nexcept RuntimeError:\n pass\n\ntry:\n removeButton(\"otherbar\")\nexcept RuntimeError:\n pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add a single button without icon to a new toolbar with the name ``demobar``.\nThe callback function is the unbounded method ``method`` without arguments.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "addButton(\"demobar\", \"call method\", method)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is quite similar than the addButton above, however internally it\nmakes a difference if a Python-scripted method is used as callback or\na method from the itom module, implemented in C.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "addButton(\"demobar\", \"call itom.version()\", itomVersion)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add another button with an icon to the same toolbar. This time,\nthe unbounded method ``methodArgs`` should be triggered if the button is clicked.\nthe name of the button is shown in the tooltip text of the button.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "addButton(\"demobar\", \"call methodArgs\", methodArgs, icon=\":/arrows/icons/plus.png\", argtuple=(\"arg1\", 23))\n# add another button to 'demobar' and use a lambda function as callback\naddButton(\"demobar\", \"call lambda function\", lambda: print(\"lambda func call\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Call a partial method. This is a method, that wraps a base method with\nmore arguments, but selected arguments are already preset.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "addButton(\n \"demobar\", \"call partial method\", partial(lambda num, base: print(int(num, base)), base=2), argtuple=(\"10010\",)\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add a button to the 'demobar' toolbar, that evaluates a Python code string.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "addButton(\"demobar\", \"call code string\", \"print('code string')\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add a button that triggers a member method of the object ``myTest``.\n.. hint:: If a button triggers such a member method, the button does not\n explicitly keep a reference to the object, such that this object must\n be kept by any other variable. Else, a RuntimeError is raised when the\n button is triggered.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "myTest = Test()\naddButton(\"demobar\", \"call bounded method\", code=myTest.doit, icon=\":/classNavigator/icons/class.png\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a new button and get its handle\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "handle = addButton(\"demobar\", \"temp\", method)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And remove the button again\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "removeButton(handle)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next step: create some buttons in another toolbar ``otherbar`` and\nthen remove the entire toolbar ``otherbar``:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for i in range(0, 5):\n addButton(\"otherbar\", \"btn%i\" % i, method)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At first remove one button\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "removeButton(\"otherbar\", \"btn3\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then remove all remaining buttons including the toolbar 'otherbar'.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "removeButton(\"otherbar\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Following buttons/bar will be added to the ``itom`` toolbar.\n\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }