Outils pour utilisateurs

Outils du site


issue135:mon_histoire

Quick recap so far. I wrote a program to enter sort information in Rhythmbox in an easier way than to do it manually track by track. I did it to solve a need, fix something Rhythmbox wasn’t doing in the way I wanted it, and also to brush up my programming skills, learning Python along the way. Satisfied with that, I decided that the next step was try to enhance it. The program you saw last month, does what is says on the tin, but it’s cumbersome – to be generous. It runs from the not-so-friendly command line, and can handle only one album at a time. I wanted therefore to write a graphical interface on top of it and, while I was at it, start using a better IDE. What is an IDE? It stands for Integrated Development Environment (more info at: https://en.wikipedia.org/wiki/Integrated_development_environment). So far, I used separate tools, well, actually only one: gnome text editor. It was my source editor of choice, simple, efficient, and it has code color coding. See Illustration 1 for how the code of fixrhy looks in gedit; note that it automatically recognises the language!

Court récapitulatif jusqu'à présent. J'ai écrit un programme pour entrer des informations de tri dans Rhythmbox plus facilement que de le faire manuellement, piste après piste. Je l'ai fait pour répondre à un besoin : changer quelque chose que Rhythmbox ne faisait pas comme j'aurais voulu et, aussi, parfaire mes capacités de programmation tout en apprenant Python. Puisque les résultats me satisfont, j'ai décidé que la prochaine étape serait d'essayer de l'améliorer.

Le programme que vous avez vu le mois dernier fait ce qu'il dit, mais il est lourd et c'est le moins qu'on puisse en dire. Il se lance à partir de la ligne de commande, qui n'est pas connue pour sa convivialité, et ne peut traiter qu'un album à la fois. Aussi, je voulais écrire une interface graphique par-dessus et, dans mon élan, commencer à utiliser un meilleur EDI.

De quoi s'agit-il ? EDI est l'abréviation d'Environnement de Développement Intégré (plus de détails à https://fr.wikipedia.org/wiki/Environnement_de_développement). Bab, le lien est en français et n'est donc pas le même ! Jusqu'ici, j'utilisais des outils distincts, grrrr, en fait un seul : gnome text editor. C'était l'éditeur que je préférais pour du code source, car il est simple, efficace et peut afficher le code en couleurs. La première image vous montre l'apparence du code de fixrhy dans gedit ; remarquez qu'il reconnaît le langage automatiquement !

A graphical application was expected to be more complex, so I wanted an environment that also had auto-completion, code folding, and more sophistication. The only IDE I knew was Eclipse, very powerful but a tad too much for my requirements. I headed off to Ubuntu Software Center and searched for IDE. I was overwhelmed by the number of results – open source; it’s a great world! I decided to refine my query: “IDE Python”. I opted to try Geany and installed it right away (you can also install it with the command: sudo apt install geany). If you decide to try this lightweight IDE, I suggest you check the plugins, there are several of them, and some may be useful for you (see Illustration 2). Check Illustration 3 for a glimpse of the Geany editor window. With Geany, I could write my code and test it without leaving the editor. There are several ways of launching the application: pressing F5, or clicking on Execute under Build, or using the relative icon on the toolbar. It’s a simple shortcut, but it speeds up development when you want to immediately check your last source code change. Je m'attendais à ce qu'une application graphique soit plus complexe et je voulais donc un environnement qui me donne l'auto-complétion, le pliage de code et plus de sophistication. Le seul EDI que je connaissais était Eclipse, qui est très puissant, mais un peu trop pour mes besoins. Je me suis dirigé vers le Centre de logiciels d'Ubuntu où j'ai fait une recherche sur EDI. Le nombre de résultats m'a vraiment étonné - l'Open Source, c'est un monde génial ! J'ai décidé de préciser ma requête : « IDE Python ». Ayant décidé d'essayer Geany, je l'ai installé tout de suite (vous pouvez également l'installer avec la commande : sudo apt install geany). Si vous voulez essayer cet EDI léger, je suggère d'en vérifier les greffons, il y en a plusieurs et certains peuvent vous être utiles (voir Image 2). Regardez Image 3 pour un aperçu de la fenêtre d'édition de Geany. Avec Geany, je pouvais écrire mon code et le tester sans quitter l'éditeur. Il y a plusieurs façons de lancer l'application : appuyer sur F5, cliquer sur Exécuter sous Build ou utiliser l'icône appropriée sur la barre d'outils. C'est un simple raccourci, mais cela accélère le développement quand vous voulez vérifier immédiatement la dernière modification de votre code source. When you develop graphical applications in Ubuntu (or Linux for that matter), you can leverage existing libraries and environments: probably the two most common are QT and GTK+. I opted for the latter and consequently I named the new application fixrhygtk. The last bit I had to work on was how to design the user interface of the application. I had a choice between coding it from scratch, or use a tool, and the tool option was the preferable. I had no need for lean code or speed, so why bother wasting time writing many lines just to describe a button in a window, when a tool like Glade could do it for me? You can check it at https://glade.gnome.org/. In a nutshell, Glade is a visual editor where you can design your user interface (windows, dialog boxes, menus, etc…) on the screen using your mouse like a drawing application. When you have finished the process, you can save an XML file that can be loaded into your application, and, from there, with few lines of code, display your objects on the screen and interact with them. Installing Glade is as easy as Geany, you can search for it in the Ubuntu Software Center (soon to be replaced by Gnome Center) or do it via the command-line with sudo apt install glade.

Before getting into the actual code (we’ll examine it in the next issue), I would like to point out two things. First I had to do some research to understand the basic structure of a Python GTK+ application. If you search Google with these keywords, there are loads of results and I encourage you to check some “Hello world” programs to familiarise yourself with a basic backbone. You probably want to understand also how containers play a role in the graphical layout of a GTK+ application. For me, the Python GTK+ 3 Tutorial acted as an introduction to some concepts, and also as a reference guide later on when I was writing code. You can find it here: http://python-gtk-3-tutorial.readthedocs.io/en/ latest/basics.html For me, it was important to read the introductions to chapters 2, 3, 5, 20, 21 and 22. Quand vous développez des applications graphiques sous Ubuntu (ou, d'ailleurs, sous Linux), vous pouvez tirer partie de bibliothèques et d'environnements existants : les deux plus utilisés sont sans doute QT et GTK+. J'ai choisi ce dernier et, ainsi, j'ai appelé la nouvelle application fixrhygtk. La dernière partie sur laquelle je devais travailler était comment concevoir l'interface utilisateur de l'application. J'avais le choix entre écrire le code à partir de zéro ou utiliser un outil, et l'option outil était préférable. Je n'avais besoin ni d'un code léger, ni de vitesse, alors pourquoi gaspiller du temps à écrire beaucoup de lignes pour tout simplement décrire un bouton dans une fenêtre quand un outil comme Glade pouvait le faire à ma place. Vous pouvez en apprendre davantage sur https://glade.gnome.org/ En un mot, Glade est un éditeur visuel avec lequel vous pouvez concevoir votre interface utilisateur (les fenêtres, les boîtes de dialogue, les menus, etc.) sur l'écran en vous servant de votre souris comme dans une application de dessin. Quand vous avez terminé le processus, vous pouvez sauvegarder un fichier XML qui peut être chargé dans votre application et, à partir de là, avec quelques lignes de code, afficher vos objets à l'écran et interagir avec. L'installation de Glade est aussi facile que cette de Geany : vous pouvez le rechercher dans le Centre de logiciels Ubuntu (bientôt remplacé par le Centre de Gnome) ou le faire en ligne de commande avec sudo apt install glade Avant de parler du code même, que nous examinerons dans le prochain numéro, j'aimerais vous signaler deux choses. D'abord, j'ai été obligé de faire des recherches pour comprendre la structure de base d'une application Python GTK+. Si vous faites une recherche dans Google avec ces mots clés, il y a de très nombreux résultats et je vous suggère fortement de regarder quelques programmes « Hello world » pour vous familiariser avec ses principes de base. Vous voudriez sans doute également comprendre le rôle que jouent des conteneurs dans la mise en page graphique d'une application GTK+. Pour ce qui me concerne, le Python GTK+ 3 Tutorial m'a donné une introduction à certains concepts et, plus tard, est devenu un guide de référence quand j'écrivais du code. Il se trouve ici : http://python-gtk-3-tutorial.readthedocs.io/en/latest/basics.html [Ndt : malheureusement, il ne semble pas y avoir de version française]. Il me paraissait important de lire les introduction aux chapitres 2, 3, 5, 20, 21 et 22. While I was doing my prework, I realised that it was easier for me to write a complete standalone program, using the knowledge I got from coding fixrhy, rather than writing a GUI interface that would use the CLI program to execute the changes. If I had to change 7 different albums, with the CLI version I had to run it 7 times and therefore parse the DB 7 times. That is not efficient; I could have amended fixrhy to accept multiple albums, but that would have been too much work. Easier to do all the processing inside the application, as we are going to see next time.**

Pendant mon travail de préparation, je me suis rendu compte qu'il était plus facile pour moi d'écrire un programme autonome, avec les connaissances acquises lors du codage de fixrhy, plutôt que d'écrire une interface graphique qui se servirait du programme en ligne de commande pour exécuter les modifications. Ainsi, s'il fallait changer 7 albums différents, je devais lancer la version CLI 7 fois pour analyser la base de données 7 fois. Cela n'est pas du tout efficace ; j'aurais pu amender fixrhy pour qu'il accepte de multiples albums différents, mais cela aurait fait beaucoup trop de travail. Il était bien plus facile de faire tout le traitement dans l'application, comme nous le verrons la prochaine fois.

issue135/mon_histoire.txt · Dernière modification : 2018/08/07 08:28 de christo.2so