issue207:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue207:python [2024/07/27 10:21] – créée auntiee | issue207:python [2024/07/29 17:44] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Simple Calculator in PAGE | + | **Simple Calculator in PAGE |
Greetings again fellow Sentient Lifeforms. Things here at landing pad 2997 on Terra haven' | Greetings again fellow Sentient Lifeforms. Things here at landing pad 2997 on Terra haven' | ||
Ligne 5: | Ligne 5: | ||
This month, I’m going to take you through creating a simple calculator in PAGE 8.0, Tkinter. Why? Well, in the last month, I’ve seen two different articles about creating a simple calculator in straight Tkinter and in Wxpython, but nothing about using a great GUI designer like PAGE (and one of them was behind a paywall (ICK)), so I thought I’d throw my two cents into the mix. | This month, I’m going to take you through creating a simple calculator in PAGE 8.0, Tkinter. Why? Well, in the last month, I’ve seen two different articles about creating a simple calculator in straight Tkinter and in Wxpython, but nothing about using a great GUI designer like PAGE (and one of them was behind a paywall (ICK)), so I thought I’d throw my two cents into the mix. | ||
- | Within the article, I’ll show you a couple of tricks that can be used within PAGE, or from just Tkinter if you want to “do it the hard way”. | + | Within the article, I’ll show you a couple of tricks that can be used within PAGE, or from just Tkinter if you want to “do it the hard way”.** |
- | Just because I’m a nice guy (according to some people), I’ve set up a github repository for you that you can download the entire project which includes all the PAGE files and Python files – so you don’t really have to deal with PAGE if you don’t want to. The URL for the repository can be found at the end of the article. | + | Calculatrice simple dans PAGE |
+ | |||
+ | Re-bonjour à tous les formes de vie sensibles. Les choses ici, sur la plateforme d' | ||
+ | |||
+ | Ce mois-ci, je vais vous aider à créer une simple calculatrice avec PAGE 8.0, Tkinter. Pourquoi ? Eh bien, le mois dernier, j'ai vu deux articles différents sur la création d'une calculatrice simple en Tkinter et en Wxpython, mais rien sur l' | ||
+ | |||
+ | Dans cet article, je vous montrerai quelques astuces qui peuvent être utilisées avec PAGE, ou simplement avec Tkinter si vous voulez « faire les choses à la dure ». | ||
+ | |||
+ | |||
+ | **Just because I’m a nice guy (according to some people), I’ve set up a github repository for you that you can download the entire project which includes all the PAGE files and Python files – so you don’t really have to deal with PAGE if you don’t want to. The URL for the repository can be found at the end of the article. | ||
Basic Requirements | Basic Requirements | ||
Ligne 13: | Ligne 22: | ||
So, the basic requirements for our project will be to create a “4 banger” calculator. Since the phrase “4 banger” goes back to the early days of electronic calculators (1970s), you might not be aware of what that means. Simply put, it’s a calculator that does only addition, subtraction, | So, the basic requirements for our project will be to create a “4 banger” calculator. Since the phrase “4 banger” goes back to the early days of electronic calculators (1970s), you might not be aware of what that means. Simply put, it’s a calculator that does only addition, subtraction, | ||
- | Of course, since it’s me, we will use PAGE 8.0 to design the GUI form. I’m not even going to go into depth on creating the form in PAGE, just some of the highlights. We won’t be using any graphics, only text on the buttons. The only “special characters” we will be using are the “< | + | Of course, since it’s me, we will use PAGE 8.0 to design the GUI form. I’m not even going to go into depth on creating the form in PAGE, just some of the highlights. We won’t be using any graphics, only text on the buttons. The only “special characters” we will be using are the “< |
+ | |||
+ | Juste parce que je suis un gars sympa (selon certaines personnes), j'ai mis en place un dépôt github pour vous permettre de télécharger le projet entier qui comprend tous les fichiers PAGE et les fichiers Python - ainsi vous n'avez pas vraiment à vous occuper de PAGE si vous ne le souhaitez pas. L'URL du dépôt se trouve à la fin de l' | ||
+ | |||
+ | Exigences de base | ||
+ | |||
+ | Les exigences de base de notre projet seront donc de créer une calculatrice « 4 banger ». Comme l' | ||
+ | |||
+ | Bien sûr, puisque c'est moi, nous utiliserons PAGE 8.0 pour concevoir le formulaire de l' | ||
- | Since we will be using PAGE 8.0, we’ll use one of the themes I created for it named “cornsilk-light”. Since we will be concentrating more on the Python code instead of the PAGE portion, I’ll give you a small function that will modify that theme to create a bold 18-point font that will apply to all the TButtons, and a slightly darker background to make them stand out from the background of the form a little bit. | + | **Since we will be using PAGE 8.0, we’ll use one of the themes I created for it named “cornsilk-light”. Since we will be concentrating more on the Python code instead of the PAGE portion, I’ll give you a small function that will modify that theme to create a bold 18-point font that will apply to all the TButtons, and a slightly darker background to make them stand out from the background of the form a little bit. |
Our callbacks for the buttons will be somewhat on the special side, since I’m going to use the lambda function to send a “key definition” via the command attribute for each of the TButtons. | Our callbacks for the buttons will be somewhat on the special side, since I’m going to use the lambda function to send a “key definition” via the command attribute for each of the TButtons. | ||
Ligne 24: | Ligne 42: | ||
So, we’ll need to create a folder to hold our PAGE project, and the themes folder to hold the cornsilk-light theme files. Then copy from the PAGE 8.0 themes folder (or from the repository files) the cornsilk-light.tcl, | So, we’ll need to create a folder to hold our PAGE project, and the themes folder to hold the cornsilk-light theme files. Then copy from the PAGE 8.0 themes folder (or from the repository files) the cornsilk-light.tcl, | ||
+ | |||
+ | $ page calc1** | ||
+ | |||
+ | Comme nous allons utiliser PAGE 8.0, nous allons utiliser l'un des thèmes que j'ai créé pour lui, nommé « cornsilk-light ». Comme nous allons nous concentrer sur le code Python plutôt que sur la partie PAGE, je vais vous donner une petite fonction qui va modifier ce thème pour créer une police de 18 points en gras qui s' | ||
+ | |||
+ | Pour les boutons, nos fonctions de rappel seront un peu spéciales, puisque je vais utiliser la fonction lambda pour envoyer une « définition de clé » via l' | ||
+ | |||
+ | Le projet fini devrait ressembler à ceci... | ||
+ | |||
+ | Création de l' | ||
+ | |||
+ | Nous allons donc devoir créer un dossier pour contenir notre projet PAGE et le dossier themes pour contenir les fichiers du thème cornsilk-light. Copiez ensuite le fichier cornsilk-light.tcl depuis le dossier themes de PAGE 8.0 (ou depuis les fichiers du dépôt), ainsi que l' | ||
$ page calc1 | $ page calc1 | ||
- | Now, set your Toplevel form to width=359 and height to 467. Set the title to “Calc1”. Make sure that you are in the Absolute mode so the form won’t resize. | + | |
+ | **Now, set your Toplevel form to width=359 and height to 467. Set the title to “Calc1”. Make sure that you are in the Absolute mode so the form won’t resize. | ||
Add a TLabel widget at x=2, y=60, with a height of 50 and a width of 356. Set the background color to “white”, | Add a TLabel widget at x=2, y=60, with a height of 50 and a width of 356. Set the background color to “white”, | ||
Ligne 35: | Ligne 66: | ||
Now, add 10 more TButton widgets aligning them into a “keypad” orientation as in the image above. Set the text to the proper number for that key and the command to “lambda : on_numKey(X)” where X is the number of the key (or for the period key simply ‘.’. Be sure to use single quotes when using the PAGE command attribute text box). | Now, add 10 more TButton widgets aligning them into a “keypad” orientation as in the image above. Set the text to the proper number for that key and the command to “lambda : on_numKey(X)” where X is the number of the key (or for the period key simply ‘.’. Be sure to use single quotes when using the PAGE command attribute text box). | ||
- | Now, add 6 more TButtons, and set their text as seen in the image above. For the clear and divide keys, there’s a file in the repo called symbols.txt that has the two symbols. Just copy each and paste them into the text attribute column. | + | Now, add 6 more TButtons, and set their text as seen in the image above. For the clear and divide keys, there’s a file in the repo called symbols.txt that has the two symbols. Just copy each and paste them into the text attribute column.** |
- | At this point, the final step is to enter each of the command line attributes. The one for the Divide button would be “lambda : on_funcKey(‘Divide’)”. From there the text for the command attribute sets would be: | + | Maintenant, réglez votre formulaire Toplevel sur une largeur de 359 et une hauteur de 467. Fixez le titre à « Calc1 ». Assurez-vous que vous êtes en mode Absolute afin que le formulaire ne soit pas redimensionné. |
+ | |||
+ | Ajoutez un widget TLabel à x=2, y=60, avec une hauteur de 50 et une largeur de 356. Réglez la couleur d' | ||
+ | |||
+ | Ensuite, placez un widget TButton à x=10, y=140, height=49 et width=62. Puis, définissez le texte à « 7 ». Enfin, définissez l' | ||
+ | |||
+ | Maintenant, ajoutez 10 autres widgets TButton en les alignant dans une orientation « clavier » comme dans l' | ||
+ | |||
+ | Ajoutez maintenant 6 autres boutons TB et définissez leur texte comme indiqué dans l' | ||
+ | |||
+ | |||
+ | **At this point, the final step is to enter each of the command line attributes. The one for the Divide button would be “lambda : on_funcKey(‘Divide’)”. From there the text for the command attribute sets would be: | ||
lambda : on_funcKey(‘Mult’) | lambda : on_funcKey(‘Mult’) | ||
Ligne 55: | Ligne 97: | ||
Now we can begin to create our functions and callbacks in the support module (“calc1_support.py”). | Now we can begin to create our functions and callbacks in the support module (“calc1_support.py”). | ||
- | PAGE created the majority of the program for us, but we still need to flesh the skeletons out. | + | PAGE created the majority of the program for us, but we still need to flesh the skeletons out.** |
- | First, we need to add an import statement. Near the top of the file are all of the import statements. Just after the “from tkinter.constants import *” line, you need to add | + | À ce stade, la dernière étape consiste à saisir chacun des attributs de la ligne de commande. Celui du bouton Diviser serait « lambda : on_funcKey(' |
+ | |||
+ | lambda : on_funcKey(' | ||
+ | lambda : on_funcKey(' | ||
+ | lambda : on_funcKey(' | ||
+ | lambda : on_funcKey(' | ||
+ | lambda : on_funcKey(' | ||
+ | |||
+ | Une fois que vous avez défini toutes vos clés, sauvegardez votre projet sous le nom « calc1 » et générez le fichier GUI.py et le module Support. | ||
+ | |||
+ | Votre Toplevel devrait ressembler à ceci... | ||
+ | |||
+ | Vous pouvez fermer PAGE à ce stade. Encore une fois, aucun des boutons n'a de police en gras ou d' | ||
+ | |||
+ | Le code | ||
+ | |||
+ | Nous pouvons maintenant commencer à créer nos fonctions et nos rappels dans le module de support (« calc1_support.py »). | ||
+ | |||
+ | PAGE a créé la majorité du programme pour nous, mais nous devons encore étoffer les squelettes. | ||
+ | |||
+ | |||
+ | **First, we need to add an import statement. Near the top of the file are all of the import statements. Just after the “from tkinter.constants import *” line, you need to add | ||
from tkinter.font import Font | from tkinter.font import Font | ||
Ligne 67: | Ligne 130: | ||
At this point, we need to add the startup function code. I usually put this just after the main function. | At this point, we need to add the startup function code. I usually put this just after the main function. | ||
- | The first task we do is clear the ‘Display’ Label widget by calling the .set() method of the Label widget. Then we define a global variable named “dbuf” and set it to an empty string. After that, we call a function called set_button_fonts(), | + | The first task we do is clear the ‘Display’ Label widget by calling the .set() method of the Label widget. Then we define a global variable named “dbuf” and set it to an empty string. After that, we call a function called set_button_fonts(), |
- | Here is the set_button_fonts() function. As you can see from the comments, we create an instance of the ttk.style object, define a new font using “DejaVu Sans”, font size 18 points and weight bold, and assign that a name of “myFont”. Then we call the style.configuration method to set the font to myFont and the background to “cornsilk4” to every TButton widget in the project. Finally we call the update function for the Toplevel to make sure it is done before we show it to the user. | + | Tout d' |
+ | |||
+ | from tkinter.font import Font | ||
+ | |||
+ | C'est parce que nous allons définir une police « personnalisée » dans un petit moment. | ||
+ | |||
+ | Ensuite, descendez jusqu' | ||
+ | |||
+ | À ce stade, nous devons ajouter le code de la fonction de démarrage. J'ai l' | ||
+ | |||
+ | La première chose à faire est d' | ||
+ | |||
+ | |||
+ | **Here is the set_button_fonts() function. As you can see from the comments, we create an instance of the ttk.style object, define a new font using “DejaVu Sans”, font size 18 points and weight bold, and assign that a name of “myFont”. Then we call the style.configuration method to set the font to myFont and the background to “cornsilk4” to every TButton widget in the project. Finally we call the update function for the Toplevel to make sure it is done before we show it to the user. | ||
At this point, we have only two more functions to deal with. They are the callbacks for the buttons that make up the “numKey” group and for those that make up the “funcKey” group. The on_numKey callback is REALLY easy (below). | At this point, we have only two more functions to deal with. They are the callbacks for the buttons that make up the “numKey” group and for those that make up the “funcKey” group. The on_numKey callback is REALLY easy (below). | ||
We simply append the value that is sent into the on_numKey function from the button to the variable dbuf and display that in the TLable widget. When we set the command function for the number keys (and the period key), we use the lambda function that allows the command function to send a value into the callback. (Normally callbacks can not include any parameters. We can get around this by using the lambda function). So for the # 4 key, the command attribute would be… | We simply append the value that is sent into the on_numKey function from the button to the variable dbuf and display that in the TLable widget. When we set the command function for the number keys (and the period key), we use the lambda function that allows the command function to send a value into the callback. (Normally callbacks can not include any parameters. We can get around this by using the lambda function). So for the # 4 key, the command attribute would be… | ||
+ | |||
+ | lambda : on_numKey(4)** | ||
+ | |||
+ | Voici la fonction set_button_fonts(). Comme vous pouvez le voir dans les commentaires, | ||
+ | |||
+ | À ce stade, il ne nous reste plus que deux fonctions à gérer. Il s'agit des rappels pour les boutons qui composent le groupe « numKey » et pour ceux qui composent le groupe « funcKey ». Le rappel on_numKey est VRAIMENT facile (ci-dessous). | ||
+ | |||
+ | Nous ajoutons simplement la valeur envoyée par le bouton dans la fonction on_numKey à la variable dbuf et l' | ||
lambda : on_numKey(4) | lambda : on_numKey(4) | ||
- | When it gets to the on_numKey, it is sent in to the *args, which contains all the arguments as a list. Since we are sending in only one value, it will come in as args[0]. That is converted into a string. | + | |
+ | **When it gets to the on_numKey, it is sent in to the *args, which contains all the arguments as a list. Since we are sending in only one value, it will come in as args[0]. That is converted into a string. | ||
Unfortunately, | Unfortunately, | ||
Ligne 86: | Ligne 171: | ||
So for the “Add” function we add a “+” to the string and so on. Then we send it to the TLabel widget to show to the user. | So for the “Add” function we add a “+” to the string and so on. Then we send it to the TLabel widget to show to the user. | ||
- | For the Clear function, we simply set the TLabel widget and the dbuf variable to an empty string (shown right). | + | For the Clear function, we simply set the TLabel widget and the dbuf variable to an empty string (shown right).** |
- | Now… the part of the function that makes everything work. That’s the “Equal” case. When the user clicks on the Equal button, we simply take the string that we’ve been building all along and then use python’s eval function. | + | Lorsqu' |
+ | |||
+ | Malheureusement, | ||
+ | |||
+ | La première chose que nous faisons est d' | ||
+ | |||
+ | Ainsi, pour la fonction « Add », nous ajoutons un « + » à la chaîne et ainsi de suite. Nous l' | ||
+ | |||
+ | Pour la fonction Clear, nous définissons simplement le widget TLabel et la variable dbuf à une chaîne vide (illustrée à droite). | ||
+ | |||
+ | |||
+ | **Now… the part of the function that makes everything work. That’s the “Equal” case. When the user clicks on the Equal button, we simply take the string that we’ve been building all along and then use python’s eval function. | ||
So if the user clicks the buttons “2”, “+”, “2”, and then presses the equal key, the dbuf variable would be “2+2” and the eval then can deal this as a math statement. Therefore, the answer is (of course) 4. | So if the user clicks the buttons “2”, “+”, “2”, and then presses the equal key, the dbuf variable would be “2+2” and the eval then can deal this as a math statement. Therefore, the answer is (of course) 4. | ||
Ligne 96: | Ligne 192: | ||
You can find the repository at https:// | You can find the repository at https:// | ||
- | Until next time, as always; stay safe, healthy, positive and creative! | + | Until next time, as always; stay safe, healthy, positive and creative!** |
+ | |||
+ | Maintenant... la partie de la fonction qui fait que tout fonctionne. C'est le cas « Equal ». Lorsque l' | ||
+ | |||
+ | Ainsi, si l' | ||
+ | |||
+ | C'est simple, mais ce n'est en aucun cas à l' | ||
+ | |||
+ | Vous pouvez trouver le dépôt à https:// | ||
+ | |||
+ | Jusqu' |
issue207/python.1722068472.txt.gz · Dernière modification : 2024/07/27 10:21 de auntiee