Pour rendre l'interface d'une application tkinter plus agréable, la plupart des widgets permettent de spécifier des éléments de configurations.
Pour connaître les configurations disponibles, vous pouvez consulter la documentation officielle, ce wiki bien pratique ou encore un des nombreux tutoriels sur tkinter (voir liens au bas de cette page).
Vous avez également à votre disposition une autre source d'information intéressante : le code source de Python. Pour le consulter, faites un Ctrl+Clic sur le nom de la classe du widget dans votre code. PyCharm vous affichera directement le code source de la classe. Ce code débute généralement par un commentaire Docstring qui indique les paramètres attendus par le constructeur (sous Python, le constructeur s'appelle toujours __init__).
En plus des valeurs spécifiées dans les constructeurs, il est possible de fournir des paramètres à la méthode pack() pour ajouter, par exemple, des marges à l'objet ou encore pour spécifier l'emplacement par rapport au parent.
libelle = Label(fenetre, text='Bienvenue !', background='white', foreground='#98B54A')
libelle.pack(ipadx=10, ipady=10)
ou, pour plus de concision :
libelle = Label(fenetre, text='Bienvenue !', bg='white', fg='#98B54A')
libelle.pack(ipadx=10, ipady=10)
Avec ces nouvelles informations, il est possible de rendre l'application Hello World plus jolie comme suit :
#!/usr/bin/env python
from tkinter import *
from tkinter.font import Font
# fenêtre principale
fenetre = Tk()
# libellé
libelle = Label(fenetre, text='Hello World')
libelle.pack(pady=(20, 10))
# police pour le bouton
police = Font(family='Baskerville', size=16, weight='normal', slant='italic')
# bouton pour terminer
bouton_terminer = Button(fenetre, text='Terminer', command=fenetre.destroy, font=police, foreground='#678025', padx=15)
bouton_terminer.pack(padx=10, pady=10)
# la fenêtre s'affiche puis attend les interactions de l'usager
fenetre.mainloop()
« Tkinter 8.5 reference: a GUI for Python ». New Mexico Tech. http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html
« 14.27. Common Widget Properties ». How To Think Lke a Computer Scientist. http://interactivepython.org/courselib/static/thinkcspy/GUIandEventDrivenProgramming/09_modifying_widgets.html
« tkinter — Python interface to Tcl/Tk ». Python. https://docs.python.org/3/library/tkinter.html
« An Introduction To Tkinter ». effbot.org. http://effbot.org/tkinterbook/tkinter-index.htm#class-reference
« Python Tkinter ». Python Course. https://www.python-course.eu/python_tkinter.php
« Créer une interface graphique avec Tkinter et Python 3 ». developpez.com. http://vincent.developpez.com/cours-tutoriels/python/tkinter/apprendre-creer-interface-graphique-tkinter-python-3/
« Interface graphique Tkinter python ». Apprendre Python. http://apprendre-python.com/page-tkinter-interface-graphique-python-tutoriel
« Des interfaces graphiques avec Tkinter ». Open Classrooms. https://openclassrooms.com/courses/apprenez-a-programmer-en-python/des-interfaces-graphiques-avec-tkinter
« Introduction au langage de programmation Python 3 - Chapitre 7 - Interface graphique avec le module Tkinter ». Fabrice Sincère. http://fsincere.free.fr/isn/python/cours_python_tkinter.php?version=3
« Making Widgets Look Nice ». Using Python. http://usingpython.com/making-widgets-look-nice/
▼Publicité