{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercices du livre - Chapitre 9 - solutions\n",
    "\n",
    "---\n",
    "\n",
    "<table class=\"appbclp1\">\n",
    "<tr> <td style=\"font-weight: bold!;\">auteurs :</td><td style=\"text-align: left!;\">Laurent Pointal &lt;<a href=\"mailtp:laurent.pointal@limsi.fr\">laurent.pointal@limsi.fr</a>&gt; &amp; Bob Cordeau &lt;<a href=\"pycours@kordeo.eu\">pycours@kordeo.eu</a>&gt; </td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">licence :</td><td style=\"text-align: left!;\">Creative Commons Attribution-ShareAlike 4.0 International Public License (CC BY-SA 4.0) </td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">source :</td><td style=\"text-align: left!;\"><a href=\"https://github.com/lpointal/appbclp\">https://github.com/lpointal/appbclp</a></td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">description :</td><td style=\"text-align: left!;\">Les programmes dans ce Notebook Jupyter servent de support au livre <span style=\"font-style: italic!;\">« Python 3 - Apprendre à  programmer dans l'écosystème Python»</span>, édition Dunod, dans lequel les différentes sections sont expliquées </td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">url :</td><td style=\"text-align: left!;\"><a href=\"https://www.dunod.com/sciences-techniques/python-3\">https://www.dunod.com/EAN/9782100809141</a></td> </tr>\n",
    "</table>\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Exercice 1\n",
    "\n",
    "import tkinter as tk\n",
    "\n",
    "fen = tk.Tk()\n",
    "lignezero = tk.Frame(fen)\n",
    "lignezero.pack(side=tk.TOP, fill='x', expand=1)\n",
    "etiq = tk.Label(lignezero, text=\"Valeur:\")\n",
    "etiq.pack(side=tk.LEFT)\n",
    "chp = tk.Entry(lignezero)\n",
    "chp.pack(side=tk.LEFT)\n",
    "ccocher = tk.Checkbutton(fen, text=\"Toujours utiliser cette valeur\")\n",
    "ccocher.pack(side=tk.TOP)\n",
    "lignedeux = tk.Frame(fen)\n",
    "lignedeux.pack(side=tk.TOP)\n",
    "btnok = tk.Button(lignedeux, text=\"Ok\")\n",
    "btnok.pack(side=tk.LEFT)\n",
    "btnann = tk.Button(lignedeux, text=\"Annuler\")\n",
    "btnann.pack(side=tk.LEFT)\n",
    "fen.mainloop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Exercice 2\n",
    "\n",
    "import tkinter as tk\n",
    "root = tk.Tk()\n",
    "tk.Label(root, text=\"Nom         \").grid(row=0, sticky=tk.W)\n",
    "tk.Label(root, text=\"Mot de passe\").grid(row=1, sticky=tk.W)\n",
    "tk.Entry(root).grid(row=0, column=1, sticky=tk.E)\n",
    "tk.Entry(root).grid(row=1, column=1, sticky=tk.E)\n",
    "tk.Button(root, text=\"Login\", command=root.destroy).grid(row=2, column=1, sticky=tk.E)\n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 3\n",
    "\n",
    "import tkinter as tk\n",
    "\n",
    "# Définition de fonction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def verif_passwd(event):\n",
    "    \"\"\"Vérifie un mot de passe.\"\"\"\n",
    "    passwd = password.get()\n",
    "\n",
    "    cap, bdc, nbr = False, False, False\n",
    "    for ch in passwd:\n",
    "        if 'A' <= ch <= 'Z': cap = True  # au moins une majuscule\n",
    "        if 'a' <= ch <= 'z': bdc = True  # au moins une minuscule\n",
    "        if '0' <= ch <= '9': nbr = True  # au moins un chiffre\n",
    "    verif = len(passwd) >= 8 and cap and bdc and nbr\n",
    "    \n",
    "    if verif:\n",
    "        reponse.configure(text = 'Mot de passe valide')\n",
    "    else:\n",
    "        reponse.configure(text = 'Mot de passe invalide')\n",
    "    \n",
    "\n",
    "# Programme principal ==========================================================\n",
    "root = tk.Tk()\n",
    "tk.Label(root, text=\"Nom         \").grid(row=0)\n",
    "tk.Label(root, text=\"Mot de passe\").grid(row=1)\n",
    "tk.Entry(root).grid(row=0, column=1)\n",
    "password = tk.Entry(root)\n",
    "password.grid(row=1, column=1)\n",
    "password.bind('<Return>', verif_passwd)\n",
    "reponse = tk.Label(root)\n",
    "reponse.grid(row=2, column=0)\n",
    "root.mainloop()"
   ]
  }
 ],
 "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.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
