Search code examples
wxpython

how to change the colour of the clock directly from the menubar?


I'm trying to create a program where I can randomly choose any colour from the menubar and the colour of the clock will be changed.What to do??I've tried this..but it's not working.

import wx
from wx.lib import analogclock as ac 
class MyFrame(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id,'AnalogClock',size=(400,350))
        clock = ac.AnalogClockWindow(self)

        status=self.CreateStatusBar()
        menubar=wx.MenuBar()
        first=wx.Menu()
        item=first.Append(wx.NewId(),"Change background colour","background colour" )
        menubar.Append(first,"FILE")
        self.SetMenuBar(menubar)
        self.Bind(wx.EVT_MENU,self.onClick,item)

    def onClick(self,event):
        list=['black','blue','yellow','red']
        count=wx.SingleChoiceDialog(None,"Which colour u want??","Title",list)
        if count.ShowModal()==wx.ID_OK:
           custom=count.GetStringSelection()

        clock = ac.AnalogClockWindow(self)   
        clock.SetBackgroundColour(custom) #change the colour of background
        clock.SetHandColours('black')
        clock.SetTickColours('black')
        clock.SetTickSizes(h=20, m=7)
        clock.SetTickStyles(ac.TICKS_DECIMAL)
        self.SetSize((600,400))

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1)
        frame.Show(True)
        frame.Centre()
        return True   
app = MyApp(0)
app.MainLoop()

Solution

  • You are creating a new clock each time in onClick method. You need to make a clock accessible to any method and then change its background color. Here is your modified code.

    import wx
    from wx.lib import analogclock as ac
    
    class MyFrame(wx.Frame):
        def __init__(self, parent, id):
            wx.Frame.__init__(self, parent, id,'AnalogClock',size=(600,400))
    
            menubar=wx.MenuBar()
            first=wx.Menu()
            item=first.Append(wx.NewId(), "Change background colour","background colour" )
            menubar.Append(first, "FILE")
            self.SetMenuBar(menubar)
    
            self.status=self.CreateStatusBar()
    
            self.clock = ac.AnalogClockWindow(self)
            self.clock.SetTickSizes(h=20, m=7)
            self.clock.SetTickStyles(ac.TICKS_DECIMAL)
            self.clock.SetHandColours('black')
            self.clock.SetTickColours('black')
    
            self.colorList = ['black','blue','yellow','red']
    
            self.Bind(wx.EVT_MENU, self.onClick,item)
    
        def onClick(self,event):
            count = wx.SingleChoiceDialog(None, "Which colour u want??", "Title", self.colorList)
            if count.ShowModal() == wx.ID_OK:
                custom = count.GetStringSelection()
    
                self.clock.SetBackgroundColour(custom) #change the colour of background
    
    class MyApp(wx.App):
        def OnInit(self):
            frame = MyFrame(None, -1)
            frame.Show(True)
            frame.Centre()
            return True
    
    app = MyApp(0)
    app.MainLoop()