为什么这个不行(wxpython/颜色对话框)

2 投票
1 回答
1672 浏览
提问于 2025-04-16 18:49
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 个回答

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 的那个颜色:

在这里输入图片描述

撰写回答