issue199:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue199:python [2023/11/25 19:36] – d52fr | issue199:python [2023/11/28 17:13] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 6: | Ligne 6: | ||
While all of the widgets I used to create the demo are ttk widgets (with the exception of the ScrolledListbox), | While all of the widgets I used to create the demo are ttk widgets (with the exception of the ScrolledListbox), | ||
+ | |||
+ | Je vous salue à nouveau, chers amis. J' | ||
+ | |||
+ | Comme je le fais habituellement, | ||
+ | |||
+ | Vous trouverez ci-dessous une image qui montre la démo dans l' | ||
+ | |||
+ | Bien que tous les widgets que j'ai utilisés pour créer la démo soient des widgets Ttk (à l' | ||
+ | |||
**Once I had my GUI designed, I saved the PAGE project, and generated the GUI and Support Python modules. | **Once I had my GUI designed, I saved the PAGE project, and generated the GUI and Support Python modules. | ||
Ligne 22: | Ligne 31: | ||
import platform | import platform | ||
import os** | import os** | ||
+ | |||
+ | Une fois mon interface graphique conçue, j'ai sauvegardé le projet PAGE et généré les modules Python d' | ||
+ | |||
+ | Avant de commencer à coder votre démo, vous devrez installer la bibliothèque en utilisant pip (ou pip3) : | ||
+ | |||
+ | pip3 install tkintermapview | ||
+ | |||
+ | Maintenant que votre système est prêt pour le projet, regardons le module de support. Rappelez-vous, | ||
+ | |||
+ | Tout d' | ||
+ | |||
+ | En réalité, la seule ligne qui doit se trouver ici est la ligne import sys, mais j'ai créé et appelé une petite fonction qui s' | ||
+ | |||
+ | import sys | ||
+ | import platform | ||
+ | import os | ||
+ | |||
**Now for the Tkinter code. The only thing to be added to the support module is the import for the message box. | **Now for the Tkinter code. The only thing to be added to the support module is the import for the message box. | ||
Ligne 37: | Ligne 63: | ||
Next to last, we need to import the tkintermapview library. Since the program won’t run at all without this library, I add the try|except catch to provide information to the user that the library isn’t installed. | Next to last, we need to import the tkintermapview library. Since the program won’t run at all without this library, I add the try|except catch to provide information to the user that the library isn’t installed. | ||
- | Try: | + | try: |
from tkintermapview import TkinterMapView | from tkintermapview import TkinterMapView | ||
Ligne 48: | Ligne 74: | ||
sys.exit()** | sys.exit()** | ||
+ | |||
+ | Passons maintenant au code Tkinter. La seule chose à ajouter au module de support est l' | ||
+ | |||
+ | import tkinter as tk | ||
+ | |||
+ | import tkinter.ttk as ttk | ||
+ | |||
+ | from tkinter.constants import * | ||
+ | |||
+ | import tkinter.messagebox as messagebox | ||
+ | |||
+ | En avant-dernier, | ||
+ | |||
+ | try : // d52fr : cette ligne est une commande // | ||
+ | |||
+ | from tkintermapview import TkinterMapView | ||
+ | |||
+ | except : | ||
+ | |||
+ | msg = "Vous devez installer tkintermapview à l'aide de pip." | ||
+ | |||
+ | print(msg) | ||
+ | |||
+ | sys.exit() | ||
+ | |||
**Finally, the GUI.py file is imported so our GUI definitions are loaded. | **Finally, the GUI.py file is imported so our GUI definitions are loaded. | ||
Ligne 66: | Ligne 117: | ||
cntr = len(markerList)** | cntr = len(markerList)** | ||
+ | |||
+ | Enfin, le fichier GUI.py est importé pour que les définitions de l' | ||
+ | |||
+ | import tkintermapviewdemo | ||
+ | |||
+ | En haut à droite se trouve la fonction main, que PAGE crée pour nous. La seule chose à ajouter est un appel à la fonction startup, qui fournit le code d' | ||
+ | |||
+ | La fonction startup (ci-dessous) ne fait pas partie du squelette fourni par PAGE, donc tout est ajouté. Tout d' | ||
+ | |||
+ | Ensuite, nous fournissons un emplacement de départ pour le widget Map. J'ai décidé d' | ||
+ | |||
+ | search_marker = map_widget.set_address(" | ||
+ | |||
+ | map_widget.set_zoom(defaultZoomLevel) | ||
+ | |||
+ | markerList.append(search_marker) | ||
+ | |||
+ | cntr = len(markerList) | ||
+ | |||
**I then add the text to the Entry widget, insert the text (with the counter number) into the ScrolledListbox. Then I call the set_bindings function to deal with the bindings needed, show the environment debugging information. Next I disable the CreateMarker button, since it was for future development, | **I then add the text to the Entry widget, insert the text (with the counter number) into the ScrolledListbox. Then I call the set_bindings function to deal with the bindings needed, show the environment debugging information. Next I disable the CreateMarker button, since it was for future development, | ||
Ligne 82: | Ligne 152: | ||
on_btnGo()** | on_btnGo()** | ||
+ | |||
+ | J' | ||
+ | |||
+ | La fonction de démarrage étant terminée, nous pouvons maintenant examiner certaines des autres fonctions que j' | ||
+ | |||
+ | Voici (page suivante, en bas à droite) la fonction show_environ_info. Elle affiche des informations génériques sur le système dans le terminal lorsque le programme est lancé. | ||
+ | |||
+ | La fonction set_bindings (page suivante, en haut à gauche) fait ce que son titre indique. Elle définit les bindings pour le widget Entry et la Scrolled1Listbox. | ||
+ | |||
+ | Voici la fonction de rappel keypress pour le widget Entry, qui appellera la fonction on_btnGo lorsque l' | ||
+ | |||
+ | def on_entryKeyPress(e) : | ||
+ | |||
+ | si e.keysym == " | ||
+ | |||
+ | on_btnGo() | ||
+ | |||
**Since the Map widget supports right-clicking on the map to get the Latitude and Longitude of the click point, I’ve included the ability of right-clicking in the Entry widget to allow the pasting from the clipboard (bottom left). | **Since the Map widget supports right-clicking on the map to get the Latitude and Longitude of the click point, I’ve included the ability of right-clicking in the Entry widget to allow the pasting from the clipboard (bottom left). | ||
Ligne 91: | Ligne 178: | ||
The ClearMarkers callback will go through the marker list and use the map_widget.delete() method, then all the markers in the ScrolledListbox will be deleted. Notice this delete ALL markers (next page, top right). | The ClearMarkers callback will go through the marker list and use the map_widget.delete() method, then all the markers in the ScrolledListbox will be deleted. Notice this delete ALL markers (next page, top right). | ||
- | The ClearPath callback simply calls the mapwidget.delete_all_path method to remove the entire path from the map (next page, bottom right).** | + | The ClearPath callback simply calls the mapwidget.delete_all_path method to remove the entire path from the map (next page, bottom right). |
The CreatePath callback walks through the list of markers and calls the map_widget.set_path method. It draws a line on the map between the markers (next page, bottom | The CreatePath callback walks through the list of markers and calls the map_widget.set_path method. It draws a line on the map between the markers (next page, bottom | ||
+ | |||
+ | Comme le widget Map permet de cliquer avec le bouton droit de la souris sur la carte pour obtenir la latitude et la longitude du point de clic, j'ai inclus la possibilité de cliquer avec le bouton droit de la souris dans le widget Entry pour permettre le collage à partir du presse-papier (en bas à gauche). | ||
+ | |||
+ | Lorsque l' | ||
+ | |||
+ | Il est maintenant temps de compléter le code de toutes les fonctions de rappel de nos boutons. Rappelez-vous que PAGE crée les fonctions squelettes, donc seul le code que vous devez ajouter sera en gras. | ||
+ | |||
+ | La fonction ClearMarkers va parcourir la liste des marqueurs et utiliser la méthode map_widget.delete(), | ||
+ | |||
+ | Le callback ClearPath appelle simplement la méthode mapwidget.delete_all_path pour supprimer l' | ||
+ | |||
+ | Le callback CreatePath parcourt la liste des marqueurs et appelle la méthode map_widget.set_path. Elle dessine une ligne sur la carte entre les marqueurs (page suivante, en bas à gauche). | ||
+ | |||
**The btnGo callback gets the address (or coordinates) from the Entry widget then calls the map.widget.set_address method to create a place on the map. We automatically call the method with the marker=True parameter to make sure there is a marker on the map. The return from the method is checked to see if the get address search was successful. If so, the marker is added. If not, an error message box will be shown to the user (next page). | **The btnGo callback gets the address (or coordinates) from the Entry widget then calls the map.widget.set_address method to create a place on the map. We automatically call the method with the marker=True parameter to make sure there is a marker on the map. The return from the method is checked to see if the get address search was successful. If so, the marker is added. If not, an error message box will be shown to the user (next page). | ||
Ligne 104: | Ligne 204: | ||
Until next time, as always; stay safe, healthy, positive and creative!** | Until next time, as always; stay safe, healthy, positive and creative!** | ||
+ | |||
+ | Le callback btnGo obtient l' | ||
+ | |||
+ | Le tour est joué. Le programme devrait maintenant fonctionner et vous permettre d' | ||
+ | |||
+ | Le widget TkinterMapView offre BEAUCOUP plus de fonctionnalités que celles que j'ai explorées ici. Je vous suggère fortement de visiter le dépôt GitHub de Tom et de télécharger le code afin d' | ||
+ | |||
+ | J'ai créé un dépôt sur GitHub pour le code de ce projet. Vous pouvez le trouver à l' | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ | |||
+ | // Traduction de la ligne noire de l' | ||
+ | **This final function simply centres the project in the user’s screen.** | ||
+ | Cette dernière fonction centre le projet dans l' | ||
+ | |||
+ | // Traduction de la ligne noire de l' | ||
+ | **The btnExit callback simply calls sys.exit() to end the program.** | ||
+ | La fonction de rappel btnExit appelle simplement sys.exit() pour terminer le programme. | ||
+ |
issue199/python.1700937386.txt.gz · Dernière modification : 2023/11/25 19:36 de d52fr