|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "bacterial-notice", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Day 27" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "human-import", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "###### Tkinter, \\*args, **kwargs, Creating GUI Programs" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": 15, |
| 22 | + "id": "excess-injection", |
| 23 | + "metadata": {}, |
| 24 | + "outputs": [], |
| 25 | + "source": [ |
| 26 | + "import tkinter" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": 14, |
| 32 | + "id": "vocal-arkansas", |
| 33 | + "metadata": {}, |
| 34 | + "outputs": [], |
| 35 | + "source": [ |
| 36 | + "window = tkinter.Tk()\n", |
| 37 | + "window.title('GUI title')\n", |
| 38 | + "window.minsize(width=500, height=300)\n", |
| 39 | + "window.config(padx=20, pady=20)\n", |
| 40 | + "\n", |
| 41 | + "# Label\n", |
| 42 | + "FONT = ('arial',24,'bold')\n", |
| 43 | + "my_label = tkinter.Label(text='I am label', font=FONT)\n", |
| 44 | + "# pack() used to lay the components built\n", |
| 45 | + "# my_label.pack(side='bottom',expand=1)\n", |
| 46 | + "my_label.pack()\n", |
| 47 | + "\n", |
| 48 | + "\n", |
| 49 | + "# Alternate ways of updating properties of built component\n", |
| 50 | + "my_label['text'] = 'New label'\n", |
| 51 | + "my_label.config(text='Another new label')\n", |
| 52 | + "\n", |
| 53 | + "def btnClick():\n", |
| 54 | + " user_input = input.get()\n", |
| 55 | + " my_label['text'] = user_input\n", |
| 56 | + " \n", |
| 57 | + "# Button\n", |
| 58 | + "my_button = tkinter.Button(text='Click me', command=btnClick)\n", |
| 59 | + "my_button.pack()\n", |
| 60 | + "\n", |
| 61 | + "# Entry\n", |
| 62 | + "input = tkinter.Entry(width=20)\n", |
| 63 | + "input.pack()\n", |
| 64 | + "\n", |
| 65 | + "window.mainloop()" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "markdown", |
| 70 | + "id": "hungry-local", |
| 71 | + "metadata": {}, |
| 72 | + "source": [ |
| 73 | + "**Layout Manager**\n", |
| 74 | + "\n", |
| 75 | + "- **pack()**\n", |
| 76 | + " - Adds components one below other, aligned at center\n", |
| 77 | + " ---\n", |
| 78 | + "- **place()**\n", |
| 79 | + " ```python\n", |
| 80 | + " input = tkinter.Entry(width=20)\n", |
| 81 | + " input.pack()\n", |
| 82 | + " input.place(x=0,y=0)\n", |
| 83 | + " ```\n", |
| 84 | + " ---\n", |
| 85 | + "- **grid()** => Divides screen into num of rows & columns\n", |
| 86 | + " ```python\n", |
| 87 | + " input = tkinter.Entry(width=20)\n", |
| 88 | + " input.pack()\n", |
| 89 | + " input.column(row=0,column=0)\n", |
| 90 | + " ```" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "markdown", |
| 95 | + "id": "substantial-summer", |
| 96 | + "metadata": {}, |
| 97 | + "source": [ |
| 98 | + "###### Advanced Python Arguments\n", |
| 99 | + "###### *args: Many Positional Arguments => args - Tuple" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": 24, |
| 105 | + "id": "falling-opposition", |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [ |
| 108 | + { |
| 109 | + "name": "stdout", |
| 110 | + "output_type": "stream", |
| 111 | + "text": [ |
| 112 | + "7\n", |
| 113 | + "15\n" |
| 114 | + ] |
| 115 | + } |
| 116 | + ], |
| 117 | + "source": [ |
| 118 | + "# Here 'add' function takes 2 arguments only\n", |
| 119 | + "def add(n1,n2):\n", |
| 120 | + " print(n1+n2)\n", |
| 121 | + "add(5,2)\n", |
| 122 | + "\n", |
| 123 | + "# For unlimited args:\n", |
| 124 | + "# Note: 'args' can be changed to any string, but '*' is required\n", |
| 125 | + "def add(*args):\n", |
| 126 | + " numbers = [n for n in args]\n", |
| 127 | + " print(sum(numbers))\n", |
| 128 | + "add(1,2,3,4,5)" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "markdown", |
| 133 | + "id": "attractive-particle", |
| 134 | + "metadata": {}, |
| 135 | + "source": [ |
| 136 | + "###### **kwargs: Many Keyword Arguments => kwargs - Dictionary" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "code", |
| 141 | + "execution_count": 35, |
| 142 | + "id": "cleared-necessity", |
| 143 | + "metadata": {}, |
| 144 | + "outputs": [ |
| 145 | + { |
| 146 | + "name": "stdout", |
| 147 | + "output_type": "stream", |
| 148 | + "text": [ |
| 149 | + "Addition: 4\n", |
| 150 | + "Multiplication: 9\n" |
| 151 | + ] |
| 152 | + } |
| 153 | + ], |
| 154 | + "source": [ |
| 155 | + "def calculate(n,**kwargs):\n", |
| 156 | + " add = n + kwargs['add']\n", |
| 157 | + " multiply = n * kwargs['multiply']\n", |
| 158 | + " print(f'Addition: {add}')\n", |
| 159 | + " print(f'Multiplication: {multiply}')\n", |
| 160 | + "calculate(3, add=1, multiply=3)" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": 37, |
| 166 | + "id": "ongoing-mentor", |
| 167 | + "metadata": {}, |
| 168 | + "outputs": [ |
| 169 | + { |
| 170 | + "name": "stdout", |
| 171 | + "output_type": "stream", |
| 172 | + "text": [ |
| 173 | + "BMW\n", |
| 174 | + "GT\n" |
| 175 | + ] |
| 176 | + } |
| 177 | + ], |
| 178 | + "source": [ |
| 179 | + "class Car:\n", |
| 180 | + " def __init__(self,**kw):\n", |
| 181 | + " self.brand = kw['brand']\n", |
| 182 | + " self.model = kw['model']\n", |
| 183 | + "my_car = Car(brand='BMW',model='GT')\n", |
| 184 | + "print(my_car.brand)\n", |
| 185 | + "print(my_car.model)" |
| 186 | + ] |
| 187 | + }, |
| 188 | + { |
| 189 | + "cell_type": "markdown", |
| 190 | + "id": "psychological-terminal", |
| 191 | + "metadata": {}, |
| 192 | + "source": [ |
| 193 | + "**Important Note:**\n", |
| 194 | + "- For accessing values from dictionary, we use say \n", |
| 195 | + "```car['brand']``` \n", |
| 196 | + "- Reduced to ```car.get('brand')```" |
| 197 | + ] |
| 198 | + }, |
| 199 | + { |
| 200 | + "cell_type": "markdown", |
| 201 | + "id": "color-reputation", |
| 202 | + "metadata": {}, |
| 203 | + "source": [ |
| 204 | + "---" |
| 205 | + ] |
| 206 | + }, |
| 207 | + { |
| 208 | + "cell_type": "code", |
| 209 | + "execution_count": null, |
| 210 | + "id": "collectible-naples", |
| 211 | + "metadata": {}, |
| 212 | + "outputs": [], |
| 213 | + "source": [] |
| 214 | + } |
| 215 | + ], |
| 216 | + "metadata": { |
| 217 | + "kernelspec": { |
| 218 | + "display_name": "Python 3", |
| 219 | + "language": "python", |
| 220 | + "name": "python3" |
| 221 | + }, |
| 222 | + "language_info": { |
| 223 | + "codemirror_mode": { |
| 224 | + "name": "ipython", |
| 225 | + "version": 3 |
| 226 | + }, |
| 227 | + "file_extension": ".py", |
| 228 | + "mimetype": "text/x-python", |
| 229 | + "name": "python", |
| 230 | + "nbconvert_exporter": "python", |
| 231 | + "pygments_lexer": "ipython3", |
| 232 | + "version": "3.9.1" |
| 233 | + } |
| 234 | + }, |
| 235 | + "nbformat": 4, |
| 236 | + "nbformat_minor": 5 |
| 237 | +} |
0 commit comments