如何直接从菜单栏改变时钟的颜色?

时间:2014-10-15 06:03:15

标签: wxpython

我正在尝试创建一个程序,我可以随机选择菜单栏中的任何颜色,时钟的颜色也会改变。怎么办?我试过这个......但是它没有用。

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()

1 个答案:

答案 0 :(得分:0)

您每次使用onClick方法创建一个新时钟。您需要使clock可以访问任何方法,然后更改其背景颜色。这是您修改后的代码。

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()