⇨ MENU ⇨ MODULES ⇨ tkinter ⇨ controles standards ⇨ Button
Tutoriel de tkinter.Button ( )
⇨ MENU ⇨ MODULES ⇨ tkinter ⇨ controles standards ⇨ Button
Tutoriel de tkinter.Button ( )
DESCRIPTION.
Créer un nouveau controle de type bouton cliquable (widget : tkinter.Button ( )).
SYNTAXE.
BUT_Bouton = tkinter.Button ( parent , attribut1 = valeur , ... , attributn = valeur )
BUT_Bouton = ⇨ affectation de l'instance du controle [ optionnel ]
tkinter.Button ( ) ⇨ création d'une instance de tkinter.Button ( ) [ OBLIGATOIRE ]
parent ⇨ conteneur de l'instance de tkinter.Button ( ) [ OBLIGATOIRE ]
attribut = valeur ⇨ attribut à modifier avec sa nouvelle valeur [ optionnel ]
REMARQUES.
Les boutons cliquables créés avec la classe tkinter.Button ( ) permettent une simplification de la méthode tkinter.bind ( "<BottonPress>" , fonction ) pour l'appel d'une fonction fonction. fonction est la valeur de l'attribut command du bouton cliquable. Le nom de la fonction fonction à appeler ne doit pas être suivi d'un couple de parenthèses.
Les objets permanents de type tkinter.Button ( ) sont identifiés dans le site par : BUT_.
Voir les conventions sur les variables utilisées dans ce site ...
CREATION DES BOUTONS CLIQUABLES.
Les boutons cliquables sont crées comme tous les autres controles de tkinter, grâce à leur constructeur de classe tkinter.Button ( ) et leur premier attribut doit être l'identification de son conteneur, son parent.
import tkinter
TKI_Principal = tkinter.Tk ( )
tkinter.Button ( TKI_Principal , text = "Quitter" , command = TKI_Principal.destroy ).pack ( )
TKI_Principal.mainloop ( )
UTILISATION D'UN BOUTON CLIQUABLE POUR QUITTER L'APPLICATION.
import tkinter
TKI_Principal = tkinter.Tk ( )
tkinter.Button ( TKI_Principal , text = "Quitter" , command = TKI_Principal.destroy ).pack ( )
TKI_Principal.mainloop ( )
UTILISATION D'UN BOUTON CLIQUABLE POUR APPELER UNE FONCTION SANS ARGUMENT.
import tkinter
def FNC_Ajouter1 ( ) :
LAB_Compteur [ "text" ] = int ( LAB_Compteur [ "text" ] ) + 1
def FNC_Ajouter5 ( ) :
LAB_Compteur [ "text" ] = int ( LAB_Compteur [ "text" ] ) + 5
def FNC_Retirer1 ( ) :
LAB_Compteur [ "text" ] = int ( LAB_Compteur [ "text" ] ) - 1
def FNC_Retirer5 ( ) :
LAB_Compteur [ "text" ] = int ( LAB_Compteur [ "text" ] ) - 5
TKI_Principal = tkinter.Tk ( )
LAB_Compteur = tkinter.Label ( TKI_Principal , text = "0" )
LAB_Compteur.pack ( )
tkinter.Button ( TKI_Principal , text = "-5" , command = FNC_Retirer5 ).pack ( )
tkinter.Button ( TKI_Principal , text = "-1" , command = FNC_Retirer1 ).pack ( )
tkinter.Button ( TKI_Principal , text = "+1" , command = FNC_Ajouter1 ).pack ( )
tkinter.Button ( TKI_Principal , text = "+5" , command = FNC_Ajouter5 ).pack ( )
tkinter.Button ( TKI_Principal , text = "Quitter" , command = TKI_Principal.destroy ).pack ( )
TKI_Principal.mainloop ( )
UTILISATION D'UN BOUTON CLIQUABLE POUR APPELER UNE FONCTION AVEC ARGUMENTS.
import tkinter
def FNC_compteur ( Q ) :
TKV_Compteur.set ( TKV_Compteur.get ( ) + Q )
TKI_Principal = tkinter.Tk ( )
TKV_Compteur = tkinter.IntVar ( )
tkinter.Label ( TKI_Principal , textvariable = TKV_Compteur ).pack ( )
tkinter.Button ( TKI_Principal , text = "-5" , command = lambda : FNC_compteur ( -5 ) ).pack ( )
tkinter.Button ( TKI_Principal , text = "-1" , command = lambda : FNC_compteur ( -1 ) ).pack ( )
tkinter.Button ( TKI_Principal , text = "+1" , command = lambda : FNC_compteur ( 1 ) ).pack ( )
tkinter.Button ( TKI_Principal , text = "+5" , command = lambda : FNC_compteur ( 5 ) ).pack ( )
tkinter.Button ( TKI_Principal , text = "Quitter" , command = TKI_Principal.destroy ).pack ( )
TKI_Principal.mainloop ( )
MODIFIER LA FONCTION D'APPEL D'UN BOUTON CLIQUABLE.
import tkinter
def FNC_Ajouter1 ( ) :
LAB_Compteur [ "text" ] = int ( LAB_Compteur [ "text" ] ) + 1
BUT_Variant [ "command" ] = FNC_Ajouter5
BUT_Variant [ "text" ] = "+5"
def FNC_Ajouter5 ( ) :
LAB_Compteur [ "text" ] = int ( LAB_Compteur [ "text" ] ) + 5
def FNC_Retirer1 ( ) :
LAB_Compteur [ "text" ] = int ( LAB_Compteur [ "text" ] ) - 1
BUT_Variant [ "command" ] = FNC_Retirer5
BUT_Variant [ "text" ] = "-5"
def FNC_Retirer5 ( ) :
LAB_Compteur [ "text" ] = int ( LAB_Compteur [ "text" ] ) - 5
TKI_Principal = tkinter.Tk ( )
LAB_Compteur = tkinter.Label ( TKI_Principal , text = "0" )
BUT_Retirer = tkinter.Button ( TKI_Principal , text = "-1" , command = FNC_Retirer1 )
BUT_Ajouter = tkinter.Button ( TKI_Principal , text = "+1" , command = FNC_Ajouter1 )
BUT_Variant = tkinter.Button ( TKI_Principal , text = "?" )
BUT_Quitter = tkinter.Button ( TKI_Principal , text = "Quitter" , command = TKI_Principal.destroy )
LAB_Compteur.grid ( row = 0 , column = 1 , sticky = "nesw" )
BUT_Retirer.grid ( row = 1 , column = 0 , sticky = "nesw" )
BUT_Variant.grid ( row = 1 , column = 1 , sticky = "nesw" )
BUT_Ajouter.grid ( row = 1 , column = 2 , sticky = "nesw" )
BUT_Quitter.grid ( row = 2 , column = 1 , sticky = "nesw" )
TKI_Principal.mainloop ( )
Votre aide est précieuse pour améliorer ce site, alors n'hésitez pas à faire part de