⇨ MENU ⇨ MODULES ⇨ tkinter ⇨ controles standards ⇨ BitmapImage
Tutoriel de tkinter.BitmapImage ( )
⇨ MENU ⇨ MODULES ⇨ tkinter ⇨ controles standards ⇨ BitmapImage
Tutoriel de tkinter.BitmapImage ( )
DESCRIPTION.
Créer un nouveau controle de type image bitmap (widget : tkinter.BitmapImage ( )).
SYNTAXE.
IMG_Image = tkinter.BitmapImage ( attribut1 = valeur , ... , attributn = valeur )
IMG_Image = ⇨ affectation de l'instance du controle [ optionnel ]
tkinter.BitmapImage ( ) ⇨ création d'une instance de tkinter.BitmapImage ( ) [ OBLIGATOIRE ]
attribut = valeur ⇨ attribut à modifier avec sa nouvelle valeur [ optionnel ]
REMARQUES
Les image BitMap sont des fichier image 2 couleurs. L'utilisation de ce controle permet d'insérer des images au format xbm dans d'autres controles, qui l'autorisent, à la place du texte ou avec lui,
Les objets permanents de type tkinter.BitmapImage ( ) sont identifiés dans le site par : IMG_.
Voir les conventions sur les variables utilisées dans ce site ...
CREATION D'UNE IMAGE BITMAP A INCORPORER.
Les images bitmap à incorporer sont crées comme tous les autres controles de tkinter, grâce à leur constructeur de classe tkinter.BitmapImage ( ) mais ne demandent pas, comme premier attribut, l'identification de son conteneur, son parent, car elles pourront être associées à plusieurs controles de l'interface graphique.
import tkinter
TKI_Principal = tkinter.Tk ( )
IMG_Image = tkinter.BitmapImage ( )
tkinter.Button ( TKI_Principal , text = "Quitter" , command = TKI_Principal.destroy ).pack ( )
TKI_Principal.mainloop ( )
IMPORTER UN FICHIER XBM EXISTANT COMME IMAGE BITMAP A INCORPORER.
import tkinter , tkinter.filedialog
def FNC_Chargemment ( ) :
kfichier = tkinter.filedialog.askopenfilename ( title = "Sélectionnez" , filetypes = [ ( "image bitmap" , ".xbm" ) ] )
if len ( kfichier ) < 1 : return
IMG_Image [ "file" ] = kfichier
TKI_Principal = tkinter.Tk ( )
IMG_Image = tkinter.BitmapImage ( )
BUT_Quitter = tkinter.Button ( TKI_Principal , text = "Quitter" , command = TKI_Principal.destroy )
BUT_Charger = tkinter.Button ( TKI_Principal , text = "Charger une image" , command = FNC_Chargemment )
LAB_Image = tkinter.Label ( TKI_Principal , image = IMG_Image )
LAB_Image.pack ( )
BUT_Charger.pack ( )
BUT_Quitter.pack ( )
TKI_Principal.mainloop ( )
IMPORTER LES DONNEES DE L'IMAGE BITMAP A INCORPORER DANS LE SCRIPT.
import tkinter
G_Donnees = """
#define im_width 32
#define im_height 32
static char im_bits[] = {
0xe1,0x5d,0x73,0x92,0xa6,0x7a,0xf9,0x19,
0x52,0x69,0x28,0xb8,0x3e,0x6e,0xd6,0x5a,
0xf4,0xca,0xde,0xda,0x23,0xf4,0xa4,0x8d,
0x07,0x95,0xb1,0xd7,0x63,0x2d,0x29,0xfb,
0x9b,0xe0,0x40,0x4a,0x00,0xc0,0xe5,0x40,
0xf6,0xa7,0xa6,0x3b,0x5d,0x58,0x27,0x01,
0xa7,0x95,0xe4,0x0c,0xa9,0x64,0x62,0x8f,
0x9c,0xbf,0x49,0x9a,0x1a,0x7e,0x04,0x72,
0xaf,0xa1,0x31,0xc7,0xa9,0xc1,0xbe,0x7d,
0x09,0x24,0x4a,0x4f,0x5b,0xe3,0x46,0x2d,
0xbd,0x50,0x5a,0x45,0x30,0xe8,0xd0,0x4e,
0x43,0x31,0x46,0xde,0x2f,0x7a,0x21,0x5e,
0xb1,0x70,0xcb,0x29,0x45,0xb8,0x32,0x1c,
0x28,0x9a,0xfa,0x7a,0xe9,0x4d,0x49,0x1b,
0xab,0xfd,0x70,0x95,0xe3,0x0d,0x80,0x75,
0xfc,0xb0,0x5d,0x2b,0xf5,0x92,0x2a,0x67
}
"""
TKI_Principal = tkinter.Tk ( )
IMG_Image = tkinter.BitmapImage ( data = G_Donnees )
tkinter.Label ( TKI_Principal , image = IMG_Image ).pack ( )
tkinter.Button ( TKI_Principal , text = "Quitter" , command = TKI_Principal.destroy ).pack ( )
TKI_Principal.mainloop ( )
GENERATEUR D'IMAGES XBM ALEATOIRE DE 32 x 32 PIXELS.
import tkinter , random
TPL_Couleurs = ( "black" , "red" , "lime" , "yellow" , "blue" , "magenta" , "aqua" , "white" )
def FNC_Aleartoire ( ) :
BOX_Matrice.delete ( 0 , "end" )
kprefixe = "#define im_width 32\n#define im_height 32\nstatic char im_bits[] = {\n"
ksufixe = "};"
kmatrice = ""
for koctect in range ( 128 ) :
kpoids = random.randrange ( 255 )
kconversion = hex ( kpoids )
if kpoids < 16 :
kvaleur = kconversion [ -1 ]
kconversion = "0x0" + kvaleur
kmatrice += kconversion
if koctect != 127 : kmatrice += ","
BOX_Matrice.insert ( "end" , kconversion )
kdonnees = kprefixe + kmatrice + ksufixe
IMG_Image [ "data" ] = kdonnees
def FNC_Couleurs ( Q ) :
if Q == "encre" :
kcouleur = TPL_Couleurs.index ( BUT_Encre [ "bg" ] ) + 1
if kcouleur > 7 : kcouleur = 0
BUT_Encre [ "background" ] = TPL_Couleurs [ kcouleur ]
IMG_Image [ "foreground" ] = TPL_Couleurs [ kcouleur ]
else :
kcouleur = TPL_Couleurs.index ( BUT_Fond [ "bg" ] ) + 1
if kcouleur > 7 : kcouleur = 0
BUT_Fond [ "background" ] = TPL_Couleurs [ kcouleur ]
IMG_Image [ "background" ] = TPL_Couleurs [ kcouleur ]
TKI_Principal = tkinter.Tk ( )
IMG_Image = tkinter.BitmapImage ( )
BUT_Quitter = tkinter.Button ( TKI_Principal , text = "Quitter" , command = TKI_Principal.destroy )
BUT_Autre = tkinter.Button ( TKI_Principal , text = "Autre" , command = FNC_Aleartoire )
BUT_Encre = tkinter.Button ( TKI_Principal , text = "Encre" , bg = "black" , command = lambda : FNC_Couleurs ( "encre" ) )
BUT_Fond = tkinter.Button ( TKI_Principal , text = "Fond" , bg = "white" , command = lambda : FNC_Couleurs ( "fond" ) )
LAB_Image = tkinter.Label ( TKI_Principal , image = IMG_Image )
BOX_Matrice = tkinter.Listbox ( TKI_Principal , width = 6 )
BOX_Matrice.pack ( side = "right" , fill = "both" )
LAB_Image.pack ( )
BUT_Autre.pack ( )
BUT_Encre.pack ( )
BUT_Fond.pack ( )
BUT_Quitter.pack ( )
FNC_Aleartoire ( )
TKI_Principal.mainloop ( )
Votre aide est précieuse pour améliorer ce site, alors n'hésitez pas à faire part de