issue149:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue149:python [2019/10/01 08:53] – créée d52fr | issue149:python [2019/10/10 16:14] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Occasionally, | + | **Occasionally, |
- | 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…** |
- | 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’, | + | De temps en temps, j'aide Don Rozenberg, le créateur de Page (un concepteur d' |
- | 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… | + | 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... |
- | I know this graphic is mostly unreadable, but it is illustrative of the number of attributes that you can control for a standard button | + | **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 |
- | While the above might dissuade many users from even trying | + | When I am making a UI, I usually shy away from the ttk widgets |
- | 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/ | + | 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, appelés clam, alt, default |
- | 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:// | + | 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. | ||
+ | |||
+ | 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/ | ||
+ | |||
+ | 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:// | ||
projects/ | projects/ | ||
Start by making your new form about 516 pixels wide by 450 high. Now set the title to something like "ttk Widget Demo". | Start by making your new form about 516 pixels wide by 450 high. Now set the title to something like "ttk Widget Demo". | ||
- | 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 | + | Maintenant, placez un cadre TLabel |
- | 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, |
Save the Page file as " | Save the Page file as " | ||
- | 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 " |
- | If everything went correctly during the GUI design process, Page should have created the following functions for you... | + | 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... | ||
set_Tk_var() | set_Tk_var() | ||
Ligne 37: | Ligne 65: | ||
and the if __name__ routine. | and the if __name__ routine. | ||
- | 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.** |
- | 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 ' | + | 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 ' | ||
+ | |||
+ | (' | ||
+ | |||
+ | 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' | ||
(' | (' | ||
- | Next we repeat the operation in the print statement that gets the value of that TRadiobutton | + | 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... |
def on_btnExit(): | def on_btnExit(): | ||
Ligne 55: | Ligne 100: | ||
We'll add just one line to the bottom of the init function, which if you remember, is the very last thing that gets run before the form is shown to the user and the program actually starts. This will call the routine that will do all of our setup on the form. | We'll add just one line to the bottom of the init function, which if you remember, is the very last thing that gets run before the form is shown to the user and the program actually starts. This will call the routine that will do all of our setup on the form. | ||
+ | |||
+ | 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() | 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). |
So first, we have the function definition and we create a list that consists of the 7 TRadiobuttons that we entered. We use their names directly and preface each with a " | So first, we have the function definition and we create a list that consists of the 7 TRadiobuttons that we entered. We use their names directly and preface each with a " | ||
Ligne 66: | Ligne 123: | ||
The next two lines, create an object named " | The next two lines, create an object named " | ||
+ | |||
+ | def clear_radio_buttons(): | ||
+ | | ||
+ | for i in range(7): | ||
+ | rblist[i].configure(text='' | ||
+ | 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(): | def clear_radio_buttons(): | ||
Ligne 73: | Ligne 145: | ||
rblist[i].configure(state=' | 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! |
clear_radio_buttons() | clear_radio_buttons() | ||
Now we call our in-function function that sets up the TRadiobuttons and we are ready to " | Now we call our in-function function that sets up the TRadiobuttons and we are ready to " | ||
+ | |||
+ | for i in s.theme_names(): | ||
+ | rblist[cntr].configure(text=i) | ||
+ | rblist[cntr].configure(state=' | ||
+ | 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(): | for i in s.theme_names(): | ||
Ligne 84: | Ligne 167: | ||
cntr += 1 | cntr += 1 | ||
- | We'll use a simple for loop to do this. We will again, use the list that we created of all the TRadiobutton widgets to get the actual widget object name, set it's text to the style name for that position in the loop and set the state back to " | + | **We'll use a simple for loop to do this. We will again, use the list that we created of all the TRadiobutton widgets to get the actual widget object name, set it's text to the style name for that position in the loop and set the state back to " |
Finally, we'll set the TProgress bar so it does something. The progress bar has two modes, ' | Finally, we'll set the TProgress bar so it does something. The progress bar has two modes, ' | ||
+ | |||
+ | w.TProgressbar1.config(mode=' | ||
+ | | ||
+ | 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.config(mode=' | ||
Ligne 92: | Ligne 183: | ||
w.TProgressbar1.start() | w.TProgressbar1.start() | ||
- | That's it. Save your program and you can run it in Python. Now, anytime you want to use ttk widgets in your GUI, you know what they will look like under any style and once you have selected one to your liking, you can put somewhere in the init() function (after the first four lines) the following lines… | + | **That's it. Save your program and you can run it in Python. Now, anytime you want to use ttk widgets in your GUI, you know what they will look like under any style and once you have selected one to your liking, you can put somewhere in the init() function (after the first four lines) the following lines… |
style = ttk.Style() | style = ttk.Style() | ||
Ligne 98: | Ligne 189: | ||
- | 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...** |
- | ttkdemo.tcl - https:// | + | Ç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.py - https:// | ttkdemo.py - https:// | ||
Ligne 108: | Ligne 206: | ||
When you download them, they will most likely come down as “ttkdemo.tcl.txt”, | When you download them, they will most likely come down as “ttkdemo.tcl.txt”, | ||
- | 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.1569912781.txt.gz · Dernière modification : 2019/10/01 08:53 de d52fr