issue149: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 | ||
issue149:python [2019/10/08 09:10] – d52fr | issue149:python [2019/10/10 16:14] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 2: | Ligne 2: | ||
If you are unfamiliar with ttk widgets, it is an additional set of widgets available for Tkinter from the Tk toolkit, which is part of Tcl. The Tk toolkit is available for Perl and Ruby as well as Tcl and Python. The ttk portion of the toolkit gives alternate widgets for many of the standard Tk widgets…** | If you are unfamiliar with ttk widgets, it is an additional set of widgets available for Tkinter from the Tk toolkit, which is part of Tcl. The Tk toolkit is available for Perl and Ruby as well as Tcl and Python. The ttk portion of the toolkit gives alternate widgets for many of the standard Tk widgets…** | ||
+ | |||
+ | De temps en temps, j'aide Don Rozenberg, le créateur de Page (un concepteur d' | ||
+ | |||
+ | Si vous ne connaissez pas les gadgets de ttk, c'est un ensemble de gadgets complémentaires disponibles pour Tkinter dans la boîte à outils Tk, qui fait partie de Tcl. La boîte à outils Tk est disponible pour Perl et Ruby ainsi que pour Tcl et Python. La portion pour ttk de la boîte à outils donne des gadgets de remplacement pour beaucoup de gadgets Tk standards... | ||
**They pretty much act as the standard widgets do, they just have a different, more updated look to them (at least to some people). There are also preset styles that you can use that changes the look of them. On Linux machines, there are 4 pre-defined styles, named clam, alt, default and classic). On Windows machines, there are three others as well, called “winnative’, | **They pretty much act as the standard widgets do, they just have a different, more updated look to them (at least to some people). There are also preset styles that you can use that changes the look of them. On Linux machines, there are 4 pre-defined styles, named clam, alt, default and classic). On Windows machines, there are three others as well, called “winnative’, | ||
When I am making a UI, I usually shy away from the ttk widgets for the most part because they have less “visible” attributes than the standard widgets and I feel like I lose some control over my program. Here is an example using Page’s attribute editor…** | When I am making a UI, I usually shy away from the ttk widgets for the most part because they have less “visible” attributes than the standard widgets and I feel like I lose some control over my program. Here is an example using Page’s attribute editor…** | ||
+ | |||
+ | Ils fonctionnent de façon assez similaire aux gadgets standards ; ils ont surtout un aspect différent, plus à jour (du moins selon certaines personnes). Ils disposent aussi de thèmes pré-réglés que vous pouvez utiliser pour changer leur aspect. Sur les machines Linux, il y a 4 styles prédéfinis, | ||
+ | |||
+ | Quand je fais une interface utilisateur, | ||
**I know this graphic is mostly unreadable, but it is illustrative of the number of attributes that you can control for a standard button (on the left side) compared to the ttk button (on the right). Part of the discrepancy stems from the fact that the style controls most of the attributes that seem to be missing. In a good way, this is a good thing. On the other hand, since the style is “taking care of these attributes for you”, for the most part the style keeps you from making changes to what could normally be an important attribute when you are trying to “push the envelope” and make your GUI program look more modern than standard Tkinter widgets allow. | **I know this graphic is mostly unreadable, but it is illustrative of the number of attributes that you can control for a standard button (on the left side) compared to the ttk button (on the right). Part of the discrepancy stems from the fact that the style controls most of the attributes that seem to be missing. In a good way, this is a good thing. On the other hand, since the style is “taking care of these attributes for you”, for the most part the style keeps you from making changes to what could normally be an important attribute when you are trying to “push the envelope” and make your GUI program look more modern than standard Tkinter widgets allow. | ||
While the above might dissuade many users from even trying ttk widgets, a larger number of users are overwhelmed by how hard it is to find really good documentation on the use and customization of ttk widgets. Even those sites on the Internet that touch on how to do it, only give a small amount of information and even then, there isn’t much about using ttk goodies in Page. So, I thought I’d try to fix this in some small way.** | While the above might dissuade many users from even trying ttk widgets, a larger number of users are overwhelmed by how hard it is to find really good documentation on the use and customization of ttk widgets. Even those sites on the Internet that touch on how to do it, only give a small amount of information and even then, there isn’t much about using ttk goodies in Page. So, I thought I’d try to fix this in some small way.** | ||
+ | |||
+ | Je sais que cette image est à peu près illisible, mais elle illustre bien le nombre d' | ||
+ | |||
+ | Bien que cela puisse dissuader certains utilisateurs de donner une chance aux gadgets ttk, un grand nombre d' | ||
**One of the problems with using ttk widgets in Page is that Page has (at least currently) no way to set which of the styles you want to use either when you are designing your GUI and to make it work when you try to run your program shows the ‘default’ style at least under Linux. I believe that Windows users have the ‘winnative’ style as the default. While it is easy to change/ | **One of the problems with using ttk widgets in Page is that Page has (at least currently) no way to set which of the styles you want to use either when you are designing your GUI and to make it work when you try to run your program shows the ‘default’ style at least under Linux. I believe that Windows users have the ‘winnative’ style as the default. While it is easy to change/ | ||
+ | |||
+ | Un des problèmes lors de l' | ||
**So we’ll try to get a handle on some of the issues and get the hard stuff out of the way in this article. The latest version of Page is 4.25.1 and you can get the latest at https:// | **So we’ll try to get a handle on some of the issues and get the hard stuff out of the way in this article. The latest version of Page is 4.25.1 and you can get the latest at https:// | ||
Ligne 19: | Ligne 33: | ||
Next, add a TButton widget to act as our Exit button. Put it in the upper right corner of the form. Set the command attribute to " | Next, add a TButton widget to act as our Exit button. Put it in the upper right corner of the form. Set the command attribute to " | ||
+ | |||
+ | Aussi, dans cet article, nous allons essayer de résoudre quelques problèmes et nous débarrasser des difficultés. La dernière version de Page est la 4.25.1 et vous pouvez obtenir la plus récente sur https:// | ||
+ | |||
+ | Commencez par changer le format en 516 pixels de large par 450 de haut. Maintenant réglez le titre avec quelque chose comme « Démo d'un gadget ttk ». | ||
+ | |||
+ | Ensuite, ajoutez un gadget TButton pour qu'il marche comme un bouton Exit (Quitter). Mettez-le dans le coin en haut à droite de la surface. Paramétrez l' | ||
**Now, place a TLabel frame below the Exit button and size it to about 150 by 285. This will hold 7 TRadiobuttons. Set the Text attribute to " | **Now, place a TLabel frame below the Exit button and size it to about 150 by 285. This will hold 7 TRadiobuttons. Set the Text attribute to " | ||
+ | |||
+ | Maintenant, placez un cadre TLabel sous le bouton Exit et dimensionnez-le à 150 par 285. Celui-ci contiendra 7 TRadiobuttons (boutons radios). Réglez l' | ||
**We are over half way done. Now you need to put a TCheckbutton, | **We are over half way done. Now you need to put a TCheckbutton, | ||
Ligne 27: | Ligne 49: | ||
Now for the code. In your favorite text editor or IDE, open the " | Now for the code. In your favorite text editor or IDE, open the " | ||
+ | |||
+ | Nous avons fait la moitié du chemin. Maintenant, nous avons besoin de mettre un TCheckbutton, | ||
+ | |||
+ | Sauvegardez le fichier Page comme « ttkdemo.tcl », puis générez les deux modules Python. Il seront enregistrés automatiquement par Page sous les noms « ttkdemo.py » et « ttkdemo_support.py ». Vous pouvez fermer Page maintenant. | ||
+ | |||
+ | Maintenant, le code. Dans votre éditeur de texte favori ou dans votre environnement de développement (IDE), ouvrez le fichier « ttkdemo_support.py ». C'est là que tout le code sera saisi. | ||
**If everything went correctly during the GUI design process, Page should have created the following functions for you... | **If everything went correctly during the GUI design process, Page should have created the following functions for you... | ||
Ligne 38: | Ligne 66: | ||
Our code will be pretty minimal. We'll start with fleshing out the on_rbClick() function. This is a callback function that fires whenever the user clicks on one of the TRadiobuttons. Code is shown on the next page, top right.** | Our code will be pretty minimal. We'll start with fleshing out the on_rbClick() function. This is a callback function that fires whenever the user clicks on one of the TRadiobuttons. Code is shown on the next page, top right.** | ||
+ | |||
+ | Si tout s'est bien déroulé pendant la création du GUI, Page devrait avoir créé pour vous les fonctions suivantes : | ||
+ | |||
+ | set_Tk_var() | ||
+ | on_rbClick() | ||
+ | on_btnExit() | ||
+ | init() | ||
+ | destroy_window() | ||
+ | et la routine if __name__ | ||
+ | |||
+ | Notre code sera assez réduit. nous commencerons par mettre au point la fonction on_rbClick(). C'est une fonction d' | ||
**Page put the first two statements (print(...) and sys.stdout.flush() in for you. So the first line of our code prints the value of the TRadiobutton that we entered in the ' | **Page put the first two statements (print(...) and sys.stdout.flush() in for you. So the first line of our code prints the value of the TRadiobutton that we entered in the ' | ||
Ligne 44: | Ligne 83: | ||
Next we repeat the operation in the print statement that gets the value of that TRadiobutton and finally, we tell ttk to use the correct style to draw the widgets. This happens immediately.** | Next we repeat the operation in the print statement that gets the value of that TRadiobutton and finally, we tell ttk to use the correct style to draw the widgets. This happens immediately.** | ||
+ | |||
+ | Page a incorporé les deux premières déclarations (print(...) et sys.stdout.flush()) à votre place. Ainsi, la première ligne de notre code imprime la valeur du TRadiobutton que nous avons entrée dans l' | ||
+ | |||
+ | (' | ||
+ | |||
+ | Ensuite, nous répétons l' | ||
**Now we'll put in the code for the on_btnExit() callback... | **Now we'll put in the code for the on_btnExit() callback... | ||
Ligne 57: | Ligne 102: | ||
setup_styles()** | setup_styles()** | ||
+ | |||
+ | Maintenant, nous incorporons l' | ||
+ | def on_btnExit(): | ||
+ | print(' | ||
+ | | ||
+ | sys.stdout.flush() | ||
+ | | ||
+ | destroy_window() | ||
+ | |||
+ | Nous ajouterons juste une ligne en bas de la fonction init, qui, si vous vous en souvenez, est le tout dernier truc qui est lancé avant que le formulaire ne soit présenté à l' | ||
+ | |||
+ | setup_styles() | ||
**This is the longest and some would say the most complicated function in the program. It is also the only one that we have to create fully by hand. I'll break up the function in parts (see above). | **This is the longest and some would say the most complicated function in the program. It is also the only one that we have to create fully by hand. I'll break up the function in parts (see above). | ||
Ligne 72: | Ligne 129: | ||
rblist[i].configure(text='' | rblist[i].configure(text='' | ||
rblist[i].configure(state=' | rblist[i].configure(state=' | ||
+ | | ||
+ | C'est la fonction la plus longue et, certains dirons, la plus compliquée du programme. C'est aussi la seule que nous devons créer à la main. J' | ||
+ | |||
+ | D' | ||
+ | |||
+ | s = ttk.Style() | ||
+ | cntr = 0 | ||
+ | | ||
+ | Les deux lignes suivantes créent un objet nommé « s » qui contient les informations de style venant de ttk. Nous n'en utiliserons qu'une partie, mais une fois que vous serez plus à l'aise avec ttk, cela vous donnera accès à beaucoup de choses. Nous créons aussi une variable compteur et l' | ||
+ | |||
+ | def clear_radio_buttons(): | ||
+ | | ||
+ | for i in range(7): | ||
+ | rblist[i].configure(text='' | ||
+ | rblist[i].configure(state=' | ||
**Now we create a function within our function that clears the text field and disables all 7 of the radio buttons. I'm sure you realize why we clear the text fields, and the reason we disable all the TRadiobutton widgets at this point is so that if we are running under Linux, the user can't click buttons 5 through 7, but if we are under Windows, all 7 will be activated in the next bit of code. Welcome to cross-platform programming! | **Now we create a function within our function that clears the text field and disables all 7 of the radio buttons. I'm sure you realize why we clear the text fields, and the reason we disable all the TRadiobutton widgets at this point is so that if we are running under Linux, the user can't click buttons 5 through 7, but if we are under Windows, all 7 will be activated in the next bit of code. Welcome to cross-platform programming! | ||
Ligne 83: | Ligne 155: | ||
rblist[cntr].configure(state=' | rblist[cntr].configure(state=' | ||
cntr += 1** | cntr += 1** | ||
+ | | ||
+ | Maintenant, nous créons une fonction dans notre fonction qui efface les champs de texte et désactive les 7 boutons radio. Je suis sûr que vous comprenez pourquoi nous effaçons les champs de texte, et la raison pour laquelle nous désactivons tous les boutons gadgets TRadiobuttons à cet endroit vient du fait que, si nous fonctionnons sous Linux, l' | ||
+ | |||
+ | clear_radio_buttons() | ||
+ | | ||
+ | Nous appelons maintenant la fonction dans la fonction qui paramètre les TRadiobuttons et nous sommes prêts pour tous les « charger ». | ||
+ | |||
+ | for i in s.theme_names(): | ||
+ | rblist[cntr].configure(text=i) | ||
+ | rblist[cntr].configure(state=' | ||
+ | cntr += 1 | ||
**We' | **We' | ||
Ligne 91: | Ligne 174: | ||
| | ||
w.TProgressbar1.start()** | w.TProgressbar1.start()** | ||
+ | |||
+ | Nous utiliserons une simple boucle for pour le faire. Nous utiliserons, | ||
+ | |||
+ | Enfin, nous paramétrerons la barre TProgress pour qu' | ||
+ | |||
+ | w.TProgressbar1.config(mode=' | ||
+ | | ||
+ | w.TProgressbar1.start() | ||
**That' | **That' | ||
Ligne 99: | Ligne 190: | ||
That was easy and pretty much painless. As always, I've put all the code up on pastebin at the following links...** | That was easy and pretty much painless. As always, I've put all the code up on pastebin at the following links...** | ||
+ | |||
+ | Ça y est. Sauvegardez votre programme et vous pouvez le lancer dans Python. Maintenant, chaque fois que vous voulez utiliser des gadgets ttk dans votre GUI, vous savez à quoi ils ressembleront quel que soit le style et une fois que vous en avez choisi un à votre goût, vous pouvez mettre les lignes suivantes quelque part dans la fonction init() (après les quatre premières lignes) : | ||
+ | |||
+ | style = ttk.Style() | ||
+ | style.theme_use(‘votre_style_ici’) | ||
+ | | ||
+ | C' | ||
**ttkdemo.tcl - https:// | **ttkdemo.tcl - https:// | ||
Ligne 109: | Ligne 207: | ||
Until next time, have a great month and keep programming!** | Until next time, have a great month and keep programming!** | ||
+ | |||
+ | ttkdemo.tcl - https:// | ||
+ | |||
+ | ttkdemo.py - https:// | ||
+ | |||
+ | ttkdemo_support.py - https:// | ||
+ | |||
+ | Quand vous les téléchargerez, | ||
+ | |||
+ | Jusqu' | ||
issue149/python.1570518655.txt.gz · Dernière modification : 2019/10/08 09:10 de d52fr