issue199:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue199:python [2023/11/25 18:37] – créée auntiee | issue199:python [2023/11/28 17:13] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Greetings again, fellow Beings. I hope your October (and November so far) has been happy. This month, I will take a look at the TkinterMapView library. You can find it at https:// | + | **Greetings again, fellow Beings. I hope your October (and November so far) has been happy. This month, I will take a look at the TkinterMapView library. You can find it at https:// |
As I usually do, I used PAGE 7.6 (the current release) to throw together a GUI for the demo. Besides the fact that I’m too lazy to make the demo directly in Tkinter, this shows how easy it is to create a map viewer within PAGE without having to use a custom widget. | As I usually do, I used PAGE 7.6 (the current release) to throw together a GUI for the demo. Besides the fact that I’m too lazy to make the demo directly in Tkinter, this shows how easy it is to create a map viewer within PAGE without having to use a custom widget. | ||
Ligne 5: | Ligne 5: | ||
Below is an image that shows the demo in the PAGE designer. You can see how simple the GUI is to put together. | Below is an image that shows the demo in the PAGE designer. You can see how simple the GUI is to put together. | ||
- | 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), |
- | Once I had my GUI designed, I saved the PAGE project, and generated the GUI and Support Python modules. | + | 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. | ||
Before you start coding your demo, you will need to install the library, using pip (or pip3): | Before you start coding your demo, you will need to install the library, using pip (or pip3): | ||
Ligne 18: | Ligne 27: | ||
In reality, the only line that needs to be here is the import sys line, but I created and call a small function that runs at startup to show some information about the system the program is running on, which is often helpful for debugging purposes. So you don’t need the last two lines in this section unless you will be using the “show_environ_info” function. | In reality, the only line that needs to be here is the import sys line, but I created and call a small function that runs at startup to show some information about the system the program is running on, which is often helpful for debugging purposes. So you don’t need the last two lines in this section unless you will be using the “show_environ_info” function. | ||
+ | |||
+ | import sys | ||
+ | import platform | ||
+ | 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 sys | ||
Ligne 23: | Ligne 48: | ||
import os | 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. | ||
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 44: | Ligne 70: | ||
msg = "You must install tkintermapview using pip." | msg = "You must install tkintermapview using pip." | ||
+ | |||
+ | print(msg) | ||
+ | |||
+ | 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) | print(msg) | ||
Ligne 49: | Ligne 99: | ||
sys.exit() | 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. | ||
import tkintermapviewdemo | import tkintermapviewdemo | ||
Ligne 59: | Ligne 110: | ||
Next, we provide a starting location for the Map widget. I decided to use one of my favorite spots in the world, Garden of the Gods in Colorado. This returns a pointer to the location object. I set the marker option to True. Then tell the map widget to use the defaultZoomLevel, | Next, we provide a starting location for the Map widget. I decided to use one of my favorite spots in the world, Garden of the Gods in Colorado. This returns a pointer to the location object. I set the marker option to True. Then tell the map widget to use the defaultZoomLevel, | ||
+ | search_marker = map_widget.set_address(" | ||
+ | map_widget.set_zoom(defaultZoomLevel) | ||
+ | markerList.append(search_marker) | ||
+ | |||
+ | 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(" | search_marker = map_widget.set_address(" | ||
Ligne 70: | Ligne 136: | ||
cntr = len(markerList) | 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, | ||
With the startup function finished, we can now look at some of the other functions that I use to support the program. | With the startup function finished, we can now look at some of the other functions that I use to support the program. | ||
Ligne 83: | Ligne 150: | ||
if e.keysym == " | if e.keysym == " | ||
+ | |||
+ | 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() | 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). | ||
When the user clicks on an item in the ScrolledListbox, | When the user clicks on an item in the ScrolledListbox, | ||
Ligne 96: | Ligne 180: | ||
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 |
- | The btnGo callback gets the address (or coordinates) from the Entry widget | + | Comme le widget |
- | + | ||
- | That’s it. The program should now run and allow you to show maps, markers and paths. | + | |
+ | 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). | ||
+ | That’s it. The program should now run and allow you to show maps, markers and paths. | ||
The TkinterMapView widget provides MUCH MUCH more functionality than I’ve explored here. I strongly suggest you visit Tom’s GitHub repository and download the code so you can get more documentation and his example programs. | The TkinterMapView widget provides MUCH MUCH more functionality than I’ve explored here. I strongly suggest you visit Tom’s GitHub repository and download the code so you can get more documentation and his example programs. | ||
Ligne 114: | Ligne 203: | ||
I’ve created a repository on GitHub for the code for this project. You can find it at https:// | I’ve created a repository on GitHub for the code for this project. You can find it at https:// | ||
- | 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.1700933865.txt.gz · Dernière modification : 2023/11/25 18:37 de auntiee