{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tkinter is the standard GUI library for Python :\n", " * fast and easy way to create GUI applications in Python\n", " * handy for making your CLI applications user friendly and flexible" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Installation**: Tkinter module already comes with Python.(Batteries included)\n", "<br>\n", "**Usage**: for Python 2x versions use **\"import Tkinter\"**, for Python 3x versions use **\"import tkinter\"**." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import tkinter" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "#Basic example - a program, that's creates and displays a window to the user.\n", "\n", "#Window gets created\n", "root = tkinter.Tk()\n", "\n", "#Needed for the window to run as long as we don't close it.\n", "root.mainloop()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tkinter widgets:\n", " * provides various controls, such as buttons, labels and text boxes used in a GUI application. These controls are commonly called widgets\n", " * there are many types of widgets in the Tkinter library. Here are some popular widget examples:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. Button widget - is used to add buttons in a Python application. These buttons can display text or images that convey the purpose of the buttons. You can attach a function or a method to a button which is called automatically when you click the button." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World!\n" ] } ], "source": [ "root = tkinter.Tk()\n", "\n", "def hello():\n", " print(\"Hello, World!\")\n", "\n", "B = tkinter.Button(root, text =\"Hello\", command = hello)\n", "\n", "B.pack()\n", "root.mainloop()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. Canvas widget - the Canvas is a rectangular area intended for drawing pictures or other complex layouts. You can place graphics, text, widgets or frames on a Canvas." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "root = tkinter.Tk()\n", "\n", "C = tkinter.Canvas(root, bg=\"blue\", height=250, width=300)\n", "\n", "coord = 10, 50, 240, 210\n", "arc = C.create_arc(coord, start=0, extent=150, fill=\"red\")\n", "\n", "C.pack()\n", "root.mainloop()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. Label widget - implements a display box where you can place text or images. The text displayed by this widget can be updated at any time you want." ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "from tkinter import *\n", "\n", "root = Tk()\n", "var = StringVar()\n", "label = Label( root, textvariable=var)\n", "\n", "var.set(\"Hey!? How are you doing?\")\n", "label.pack()\n", "root.mainloop()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4. Entry label - used to accept single-line text strings from a user." ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "from tkinter import *\n", "\n", "top = Tk()\n", "L1 = Label(top, text=\"User Name\")\n", "L1.pack( side = LEFT)\n", "E1 = Entry(top, bd =5)\n", "E1.pack(side = RIGHT)\n", "\n", "top.mainloop()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5. tkMessageBox - used to display message boxes in your applications. " ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "import tkinter\n", "from tkinter import messagebox\n", "\n", "top = tkinter.Tk()\n", "\n", "def helloCallBack():\n", " messagebox.showinfo( \"Hello Python\", \"Hello World\")\n", "\n", "B = tkinter.Button(top, text =\"Hello\", command = helloCallBack)\n", "\n", "B.pack()\n", "top.mainloop()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Widget attributes:\n", " * There are many ways we can configure our tkinker widgets\n", " * Some popular ways are :" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "#DIMENSIONS\n", "root = tkinter.Tk()\n", "\n", "root.geometry(\"500x500\")\n", "root.resizable(0, 0)\n", "\n", "root.mainloop()" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World!\n" ] } ], "source": [ "#COLORS\n", "#FONT\n", "root = tkinter.Tk()\n", "\n", "def hello():\n", " print(\"Hello, World!\")\n", "\n", "B = tkinter.Button(root, text =\"Hello\", bg = \"#000000\", fg = \"blue\", font = \"none 16 italic underline\", command = hello)\n", "\n", "B.pack()\n", "root.mainloop()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Addictional resources:\n", "* http://wxpython.org - open-source Python interface for wxWindows\n", "* http://www.jython.org - Python is a Python port for Java which gives Python scripts seamless access to Java class libraries on the local machine\n", "* https://wiki.python.org/moin/TkInter - python wiki page about Tkinter\n", "* https://www.tutorialspoint.com/python/python_gui_programming.htm - more about other Tkinter widgets, atributes" ] } ], "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.5.5" } }, "nbformat": 4, "nbformat_minor": 2 }