为什么这不起作用(wxpython / color对话框)

时间:2011-06-02 03:23:50

标签: python wxpython colordialog

class iFrame(wx.Frame):
    def __init__(blah blah blah):  
        wx.Frame.__init.__(blah blah blah)  

        self.panel = wx.Panel(self, -1)  
        self.panel.SetBackgroundColour((I put a random RGB here for test purposes))  

        c_color = wx.Button(self.panel, -1, 'Press To Change Color')  
        c_color.Bind(wx.EVT_BUTTON, self.OnCC)  

    def OnCC(self, evt):
        dlg = wx.ColourDialog().SetChooseFull(1)  
        if dlg.ShowModal() == wx.ID_OK:  
            data = dlg.GetColourData()  
            color = data.Colour  
            print (color) # I did this just to test it was returning a RGB
            self.panel.SetBackgroundColour(color)  
        dlg.Destroy()  

我尝试做的是将按钮链接到颜色对话框,将RGB存储在变量中并使用它来设置面板的背景颜色......我已经测试了几乎所有这些,我插入了返回的RGB直接进入self.panel本身并且它可以工作,所以当我在这个方法中使用它时为什么它不起作用

1 个答案:

答案 0 :(得分:3)

dlg = wx.ColourDialog().SetChooseFull(1)行似乎是一个错误 - SetChooseFull上的方法不是wx.ColourData

我进行了一些更改以使其正常工作,并对代码进行了评论以说明:

def OnCC(self, evt):
    data = wx.ColourData()
    data.SetChooseFull(True)

    # set the first custom color (index 0)
    data.SetCustomColour(0, (255, 170, 128))
    # set indexes 1-N here if you like.

    # set the default color in the chooser
    data.SetColour(wx.Colour(128, 255, 170))

    # construct the chooser
    dlg = wx.ColourDialog(self, data)

    if dlg.ShowModal() == wx.ID_OK:
        # set the panel background color
        color = dlg.GetColourData().Colour
        self.panel.SetBackgroundColour(color)
    dlg.Destroy()

data.SetCustomColor(index, color)填充对话框中的N自定义颜色。我在下面的索引0圈出了一个:

enter image description here

相关问题