Bonjour

J'ai un texte dans un panel.
Je souhaite changer la couleur de fond quand je click sur un bouton. Simple je pensais...
Problème, seul un rectangle de la largeur du texte change. Pas le panel complet (qui a était "expandé" par le sizer)

Nom : pb.jpg
Affichages : 496
Taille : 16,3 Ko

Si quelqu'un peut m'aider, c'est génial !
Merci d'avance !



Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 
import wx 
 
class PanelText(wx.Panel):
    def __init__(self, parent, id, label,color = (255,255,255),fontsize = 12,**karg):
        wx.Panel.__init__(self, parent, id, **karg)
        self.SetBackgroundColour(color)
        self.txt = wx.StaticText(self,-1, label=label,style=wx.ALIGN_CENTER | wx.ST_NO_AUTORESIZE)
        self.txt.SetFont(wx.Font(wx.FontInfo(fontsize)))
 
        self.box = wx.BoxSizer(wx.HORIZONTAL) 
        self.box.Add(self.txt,1,wx.ALIGN_CENTER)
 
        self.SetSizer(self.box)
 
    def SetLabel(self,label):
        self.txt.SetLabel(label)        
 
class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title,size=(200,200))
 
        panel = wx.Panel(self)
        hbox = wx.BoxSizer(wx.VERTICAL) 
        self.text = PanelText(panel,-1,"Test",color=(128,128,255),fontsize=16)
 
        but_on = wx.Button(panel, label="Allumage")
        but_off=wx.Button(panel, label="Arret")
 
 
        hbox.Add(self.text,1,wx.ALIGN_CENTER | wx.EXPAND)
        hbox.Add(but_on,1)
        hbox.Add(but_off,1)
 
        self.Bind(wx.EVT_BUTTON,self.on,but_on)
        self.Bind(wx.EVT_BUTTON,self.off,but_off)
 
        panel.SetSizer(hbox)
 
 
    def on(self,event):
        self.text.SetBackgroundColour((120,255,120))
        self.text.SetLabel("ON")
 
    def off(self,event):
        self.text.SetBackgroundColour((255,120,120))
        self.text.SetLabel("OFF")
 
 
 
class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'wxBoxSizer.py')
        frame.Show(True)
        frame.Center()
        return True
 
app = MyApp(0)
app.MainLoop()