Esempio n. 1
0
        def _makeButtons(words):
            sizer = wx.WrapSizer()
            for word in words:
                btn = GenButton(pnl, -1, word, style=wx.BU_EXACTFIT)
                sizer.Add(btn, 0, wx.ALL, 4)

                # Add a little color to make it interesting
                r = random.randint(128, 255)
                g = random.randint(128, 255)
                b = random.randint(128, 255)
                btn.SetBackgroundColour(wx.Colour(r, g, b))
                btn.Refresh()

            return sizer
Esempio n. 2
0
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        sizer = wx.WrapSizer()
        for letter in text:
            btn = GenButton(self, label=letter)
            r = random.randint(128, 255)
            g = random.randint(128, 255)
            b = random.randint(128, 255)
            btn.SetBackgroundColour(wx.Colour(r, g, b))
            btn.Refresh()
            sizer.Add(btn, 0, wx.ALL, 5)

        self.SetSizer(sizer)
class ServerControl(wx.Panel):
    name = "ServersControl"
    attributes = "label","running","OK","test_code_OK","formatted_value",
    refresh_period = 1.0

    def __init__(self,parent,n,shown=False):
        self.values = {
            "label":"",
            "running":False,
            "formatted_value":"",
            "OK":True,
            "test_code_OK":False
        }
        self.old_values = {}

        wx.Panel.__init__(self,parent)
        self.Shown = shown
        self.Title = "Test %d" % n
        self.n = n
        self.myEnabled = wx.CheckBox(self,size=(470,-1))
        from wx.lib.buttons import GenButton
        self.State = GenButton(self,size=(25,20))
        self.Setup = wx.Button(self,size=(60,-1),label="More...")
        self.Setup.Shown = False
        self.Log = wx.Button(self,size=(50,-1),label="Log...")

        self.Bind(wx.EVT_CHECKBOX,self.OnEnable,self.myEnabled)
        self.Bind(wx.EVT_BUTTON,self.OnState,self.State)
        self.Bind(wx.EVT_BUTTON,self.OnSetup,self.Setup)
        self.Bind(wx.EVT_BUTTON,self.OnLog,self.Log)

        # Layout
        self.layout = wx.BoxSizer(wx.HORIZONTAL)
        flag = wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND
        self.layout.Add(self.myEnabled,flag=flag,proportion=1)
        self.layout.Add(self.State,flag=flag)
        self.layout.Add(self.Setup,flag=flag)
        self.layout.Add(self.Log,flag=flag)

        # Leave a 10 pixel wide border.
        self.box = wx.BoxSizer(wx.VERTICAL)
        self.box.Add(self.layout,flag=wx.ALL,border=5)
        self.SetSizer(self.box)
        self.Fit()

        self.refresh_label()

        # Periodically refresh the displayed settings.
        self.Bind(wx.EVT_TIMER,self.OnUpdate)
        from threading import Thread
        self.thread = Thread(target=self.keep_updated,name=self.name)
        self.thread.daemon = True
        self.thread.start()

    @property
    def server(self): return servers[self.n]

    def keep_updated(self):
        """Periodically refresh the displayed settings."""
        from time import time,sleep
        while True:
            try:
                t0 = time()
                if self.Shown:
                    ##debug("ServerControl %s: Shown: %r" % (self.n,self.Shown))
                    self.update_data()
                    if self.data_changed:
                        event = wx.PyCommandEvent(wx.EVT_TIMER.typeId,self.Id)
                        # call OnUpdate in GUI thread
                        wx.PostEvent(self.EventHandler,event)
                while time() < t0+self.refresh_period: sleep(0.5)
            except wx.PyDeadObjectError: break

    def OnUpdate(self,event):
        """Periodically refresh the displayed settings."""
        self.refresh_status()

    def refresh(self):
        """Force update"""
        from threading import Thread
        self.thread = Thread(target=self.refresh_background,
            name=self.name+".refresh")
        self.thread.daemon = True
        self.thread.start()

    def refresh_background(self):
        """Force update"""
        self.update_data()
        if self.data_changed:
            event = wx.PyCommandEvent(wx.EVT_TIMER.typeId,self.Id)
            wx.PostEvent(self.EventHandler,event) # call OnUpdate in GUI thread

    def update_data(self):
        """Retreive status information"""
        self.old_values = dict(self.values) # make a copy
        for n in self.attributes:
            try: self.values[n] = getattr(self.server,n)
            except Exception,msg: error("%s\n%s" % (msg,traceback.format_exc()))

    @property
    def data_changed(self):
        """Did the last 'update_data' change the data to be plotted?"""
        changed = (self.values != self.old_values)
        return changed

    def refresh_label(self,event=None):
        """Update the controls with current values"""
        self.Title = self.server.label
        self.myEnabled.Value = self.server.enabled
        self.myEnabled.Label = self.server.label

    def refresh_status(self,event=None):
        """Update the controls with current values"""
        label = self.values["label"]
        running = self.values["running"]

        self.Title = label
        self.myEnabled.Value = running

        red = (255,0,0)
        green = (0,255,0)
        gray = (180,180,180)

        self.myEnabled.Label = "%s: %s" % (label,self.values["formatted_value"])
        color = green if self.values["OK"] else red
        if not self.values["test_code_OK"]: color = gray
        self.State.BackgroundColour = color
        self.State.ForegroundColour = color
        self.State.Refresh() # work-around for a GenButton bug in Windows

    def OnEnable(self,event):
        value = event.IsChecked()
        try: self.server.running = value
        except Exception,msg: error("%r\n%r" % (msg,traceback.format_exc()))
        self.refresh()

    def get_setup(self):
        """'Setup' mode enabled? (Allows reconfiguring parameters)"""
        value = self.Setup.Shown
        return value
    def set_setup(self,value):
        self.Setup.Shown = value
        self.Layout()
        self.Fit()
    setup = property(get_setup,set_setup)

    def OnState(self,event):
        """Start/Stop server"""
        try: self.server.running = not self.server.running
        except Exception,msg: error("%r\n%r" % (msg,traceback.format_exc()))
        self.refresh()

    def OnSetup(self,event):
        """Bring up configuration panel"""
        dlg = SetupPanel(self,self.n)
        dlg.CenterOnParent()
        dlg.Show()

    def OnLog(self,event):
        """Bring up configuration panel"""
        dlg = LogPanel(self,self.n)
        dlg.CenterOnParent()
        dlg.Show()