Fonctionnement de pack

Placer des carrés.

On considère le code ci-dessous.


import tkinter
 
application = tkinter.Tk()
application.title("Pack testeur")
 
 
framejaune = tkinter.Frame(application, bg="yellow", width=50, height=50)
framebleu = tkinter.Frame(application, bg="blue",   width=50, height=50)
framevert = tkinter.Frame(application, bg="green",   width=50, height=50)
framemagenta = tkinter.Frame(application, bg="magenta",   width=50, height=50)
framemarron = tkinter.Frame(application, bg="brown",   width=50, height=50)

Écrire des commandes pack() qui conviennent pour obtenir les placements suivants :

pack 6 pack 7 pack 8 pack 9


import tkinter
 
application = tkinter.Tk()
application.title("Pack testeur")
 
 
framejaune = tkinter.Frame(application, bg="yellow", width=50, height=50)
framebleu = tkinter.Frame(application, bg="blue",   width=50, height=50)
framevert = tkinter.Frame(application, bg="green",   width=50, height=50)
framemagenta = tkinter.Frame(application, bg="magenta",   width=50, height=50)
framemarron = tkinter.Frame(application, bg="brown",   width=50, height=50)


framejaune.pack(side='right')
framebleu.pack(side='bottom')
framevert.pack(side='right')
framemagenta.pack(side='top')
framemarron.pack(side='left')
application.mainloop()
pack 6

import tkinter
 
application = tkinter.Tk()
application.title("Pack testeur")
 
 
framejaune = tkinter.Frame(application, bg="yellow", width=50, height=50)
framebleu = tkinter.Frame(application, bg="blue",   width=50, height=50)
framevert = tkinter.Frame(application, bg="green",   width=50, height=50)
framemagenta = tkinter.Frame(application, bg="magenta",   width=50, height=50)
framemarron = tkinter.Frame(application, bg="brown",   width=50, height=50)



framebleu.pack(side='bottom')
framevert.pack(side='right')
framemagenta.pack(side='top')
framemarron.pack(side='left')
framejaune.pack(side='right')
application.mainloop()
pack 7

import tkinter
 
application = tkinter.Tk()
application.title("Pack testeur")
 
 
framejaune = tkinter.Frame(application, bg="yellow", width=50, height=50)
framebleu = tkinter.Frame(application, bg="blue",   width=50, height=50)
framevert = tkinter.Frame(application, bg="green",   width=50, height=50)
framemagenta = tkinter.Frame(application, bg="magenta",   width=50, height=50)
framemarron = tkinter.Frame(application, bg="brown",   width=50, height=50)



framebleu.pack(side='bottom')
framevert.pack(side='right')
framemagenta.pack(side='right')
framemarron.pack(side='left')
framejaune.pack(side='right')
application.mainloop()
pack 8

import tkinter
 
application = tkinter.Tk()
application.title("Pack testeur")
 
 
framejaune = tkinter.Frame(application, bg="yellow", width=50, height=50)
framebleu = tkinter.Frame(application, bg="blue",   width=50, height=50)
framevert = tkinter.Frame(application, bg="green",   width=50, height=50)
framemagenta = tkinter.Frame(application, bg="magenta",   width=50, height=50)
framemarron = tkinter.Frame(application, bg="brown",   width=50, height=50)



framebleu.pack(side='bottom')
framevert.pack(side='right')
framemagenta.pack(side='right')
framemarron.pack(side='bottom')
framejaune.pack(side='right')
application.mainloop() 
pack 9

Dans les carrés.

On reprend le code de l'exercice précédent légèrement modifié.


import tkinter
 
application = tkinter.Tk()
application.title("Pack testeur")
 
largeur = 100
hauteur = 100
framejaune = tkinter.Frame(application, bg="yellow", width=largeur, height=hauteur)
framebleu = tkinter.Frame(application, bg="blue",   width=largeur, height=hauteur)
framevert = tkinter.Frame(application, bg="green",   width=largeur, height=hauteur)
framemagenta = tkinter.Frame(application, bg="magenta",   width=largeur, height=hauteur)
framemarron = tkinter.Frame(application, bg="brown",   width=largeur, height=hauteur)

Écrire des commandes pack() qui conviennent pour obtenir le résultat suivant (on a utilisé un Button et un Label) :

pack 10

C'est dans le bloc jaune que l'on essaie de placer le Button et le Label.

La position 'left' utilisée dans le code pour le bouton est donc relative à ce bloc.

L'étiquette est, dans notre code, placée après le bouton. C'est donc une position en bas de la cavité laissée par le placement du bouton, (d'où le placement sur la droite, positionnement de la cavité restante).


import tkinter
 
application = tkinter.Tk()
application.title("Pack testeur")
 
largeur = 100
hauteur = 100
framejaune = tkinter.Frame(application, bg="yellow", width=largeur, height=hauteur)
framebleu = tkinter.Frame(application, bg="blue",   width=largeur, height=hauteur)
framevert = tkinter.Frame(application, bg="green",   width=largeur, height=hauteur)
framemagenta = tkinter.Frame(application, bg="magenta",   width=largeur, height=hauteur)
framemarron = tkinter.Frame(application, bg="brown",   width=largeur, height=hauteur)

bouton1 = tkinter.Button(framejaune, text = "jaune")
etiquette2 = tkinter.Label(framejaune, text = "jaune2")


framejaune.pack(side='right',fill='y')
framebleu.pack(side='bottom')
framevert.pack(side='right')
framemagenta.pack(side='right')
framemarron.pack(side='bottom')

bouton1.pack(side='left')
etiquette2.pack(side='bottom')

application.mainloop()

Jouer avec les carrés.

Cet exercice n'a pas de corrigé. On vous demande :

  1. de tester le code ci-dessous,
  2. de faire des essais et prévoir avec divers choix où va se placer le carré suivant,
  3. enfin de comprendre le code, de le modifier (par exemple en ajoutant d'autres couleurs).

import tkinter


positionChoisie = 'top'

def efface(couleur) :
	f[couleur].pack_forget()
	 
	
def packons(couleur) :
	f[couleur].pack(side = positionChoisie) 
	
	
def positionne(position) :
	global positionChoisie
	positionChoisie = position
	
def fermer() :
	application.destroy()
	fenetrePack.destroy()
	fenetrePos.destroy()
	fenetreSupprime.destroy()
	
	
 
application = tkinter.Tk()
application.title("Testons pack")
application.geometry('300x300+200+0')
 
 
# dictionnaire de frames :
f=dict()
for couleur in ['yellow', 'brown', 'green', 'blue', 'magenta'] :
	f[couleur] = tkinter.Frame(application, bg = couleur, width=50, height=50)
 


 



fenetreSupprime = tkinter.Tk()
fenetreSupprime.geometry('100x200+0+0')


bdel = dict()

for couleur in ['yellow', 'brown', 'green', 'blue', 'magenta'] :
	bdel[couleur] = tkinter.Button(fenetreSupprime, text = 'Efface ' + couleur, command = lambda x=couleur:efface(x))
	bdel[couleur].pack()
	
 
fenetrePack = tkinter.Tk()
fenetrePack.geometry('100x150+0+500') 

bpack = dict()

for couleur in ['yellow', 'brown', 'green', 'blue', 'magenta'] :
	bpack[couleur] = tkinter.Button(fenetrePack, text = 'Pack ' + couleur, command = lambda x=couleur:packons(x))
	bpack[couleur].pack()
	
	
	
fenetrePos = tkinter.Tk()
fenetrePos.geometry('150x150+200+500') 

bpos = dict()
for position in ['left', 'right', 'top', 'bottom'] :	
	bpos[position] = tkinter.Button(fenetrePos, text = 'Position : ' + position, command = lambda x=position:positionne(x))
	bpos[position].pack()
	


boutonDestroy = tkinter.Button(fenetreSupprime, text = 'Fermer', command = fermer)
boutonDestroy.pack(side='bottom')
application.mainloop()

Avez-vous bien tout saisi ?

N'hésitez pas à nous proposer vos améliorations de ce petit jeu.