2011-06-02 29 views
2
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本身並且它可以工作,那麼爲什麼當我在此方法中使用它時不起作用爲什麼不能這樣工作(wxpython/color dialog)

+0

你得到了什麼錯誤??? – Trufa 2011-06-02 03:48:23

+0

沒有,它只是不改變背景顏色 – Isov5 2011-06-02 03:49:12

回答

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

+0

我得到的一切,但第二部分,爲什麼你需要設置一個RGB的索引,當我打印返回的RGB時,我得到了一個4元組(255,255, 0,255)...我假設阿爾法? – Isov5 2011-06-02 04:05:13

+0

該行'SetCustomColour'只顯示如何在顏色選擇器對話框中填充自定義顏色(我已更新了顯示此答案的答案)。由於選擇器沒有「alpha」滑塊(至少在我的系統上),它總是返回「255」(完全不透明)。 – samplebias 2011-06-02 04:18:16