wxPython - StaticBoxSizer

Un StaticBoxSizer place un calibreur de boîte dans une boîte statique. Il fournit une bordure autour de la boîte avec une étiquette en haut. Les étapes suivantes sont impliquées dans la préparation d'un statcboxsizer -

  • Créez un objet wx.StaticBox.
  • Déclarez un wx.StaticBoxSizer avec la boîte statique ci-dessus comme argument.
  • Créez les contrôles et ajoutez le sizer staticbox.
  • Définissez-le comme calibre pour le cadre.

Exemple

Dans l'exemple suivant, deux calibreurs de boîtes statiques sont créés et ajoutés dans un calibreur de boîtes vertical supérieur, qui contrôle la disposition du panneau à l'intérieur d'un cadre.

Le premier dimensionneur de staticbox est créé autour d'une boîte statique nommée 'Name'.

nm = wx.StaticBox(panel, -1, 'Name:') 
nmSizer = wx.StaticBoxSizer(nm, wx.VERTICAL)

Un calibreur de boîte horizontale, contenant deux étiquettes et deux zones de texte, est ajouté au calibreur de boîte statique nmSizer.

nmbox = wx.BoxSizer(wx.HORIZONTAL)
  
fn = wx.StaticText(panel, -1, "First Name") 
nmbox.Add(fn, 0, wx.ALL|wx.CENTER, 5) 
nm1 = wx.TextCtrl(panel, -1, style = wx.ALIGN_LEFT) 
nm2 = wx.TextCtrl(panel, -1, style = wx.ALIGN_LEFT) 
ln = wx.StaticText(panel, -1, "Last Name") 
         
nmbox.Add(nm1, 0, wx.ALL|wx.CENTER, 5) 
nmbox.Add(ln, 0, wx.ALL|wx.CENTER, 5) 
nmbox.Add(nm2, 0, wx.ALL|wx.CENTER, 5)
  
nmSizer.Add(nmbox, 0, wx.ALL|wx.CENTER, 10)

De même, un autre dimensionneur de staticbox contient une boîte statique nommée «Buttons».

sbox = wx.StaticBox(panel, -1, 'buttons:') 
sboxSizer = wx.StaticBoxSizer(sbox, wx.VERTICAL)

Deux objets de bouton, nommés «ok» et «annuler» sont placés dans un gabarit horizontal, qui à son tour, est placé à l'intérieur du second gabarit statique.

hbox = wx.BoxSizer(wx.HORIZONTAL) 
okButton = wx.Button(panel, -1, 'ok') 

hbox.Add(okButton, 0, wx.ALL|wx.LEFT, 10) 
cancelButton = wx.Button(panel, -1, 'cancel') 

hbox.Add(cancelButton, 0, wx.ALL|wx.LEFT, 10) 
sboxSizer.Add(hbox, 0, wx.ALL|wx.LEFT, 10)

Deux calibreurs de boîtes statiques, «nom» et «boutons» sont ajoutés dans un calibreur de boîtes vertical agissant en tant que gestionnaire de disposition du panneau dans le cadre de niveau supérieur.

panel = wx.Panel(self) 
vbox = wx.BoxSizer(wx.VERTICAL)
  
vbox.Add(nmSizer,0, wx.ALL|wx.CENTER, 5) 
vbox.Add(sboxSizer,0, wx.ALL|wx.CENTER, 5) 
panel.SetSizer(vbox)

Voici le code complet -

import wx 
 
class Mywin(wx.Frame): 
   def __init__(self, parent, title): 
      super(Mywin, self).__init__(parent, title = title)
		
      panel = wx.Panel(self) 
      vbox = wx.BoxSizer(wx.VERTICAL) 
      nm = wx.StaticBox(panel, -1, 'Name:') 
      nmSizer = wx.StaticBoxSizer(nm, wx.VERTICAL) 
       
      nmbox = wx.BoxSizer(wx.HORIZONTAL) 
      fn = wx.StaticText(panel, -1, "First Name") 
		
      nmbox.Add(fn, 0, wx.ALL|wx.CENTER, 5) 
      nm1 = wx.TextCtrl(panel, -1, style = wx.ALIGN_LEFT) 
      nm2 = wx.TextCtrl(panel, -1, style = wx.ALIGN_LEFT) 
      ln = wx.StaticText(panel, -1, "Last Name") 
         
      nmbox.Add(nm1, 0, wx.ALL|wx.CENTER, 5)
      nmbox.Add(ln, 0, wx.ALL|wx.CENTER, 5) 
      nmbox.Add(nm2, 0, wx.ALL|wx.CENTER, 5) 
      nmSizer.Add(nmbox, 0, wx.ALL|wx.CENTER, 10)  
		
      sbox = wx.StaticBox(panel, -1, 'buttons:') 
      sboxSizer = wx.StaticBoxSizer(sbox, wx.VERTICAL) 
		
      hbox = wx.BoxSizer(wx.HORIZONTAL) 
      okButton = wx.Button(panel, -1, 'ok') 
		
      hbox.Add(okButton, 0, wx.ALL|wx.LEFT, 10) 
      cancelButton = wx.Button(panel, -1, 'cancel') 
		
      hbox.Add(cancelButton, 0, wx.ALL|wx.LEFT, 10) 
      sboxSizer.Add(hbox, 0, wx.ALL|wx.LEFT, 10) 
      vbox.Add(nmSizer,0, wx.ALL|wx.CENTER, 5) 
      vbox.Add(sboxSizer,0, wx.ALL|wx.CENTER, 5) 
      panel.SetSizer(vbox) 
      self.Centre() 
         
      panel.Fit() 
      self.Show()  
		
app = wx.App() 
Mywin(None,  'staticboxsizer demo') 
app.MainLoop()

Le code ci-dessus produit la sortie suivante -