Why doesn't this work (wxpython/color dialog)
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()
What I've tried to do was link a button to a color dialog, store the RGB in a variable and use it to set the panel's background color...I've tested almost all of this, I've inserted the returned RGB开发者_运维百科 directly into the self.panel itself and it works, so why doesn't it work when I use it within this method
The line dlg = wx.ColourDialog().SetChooseFull(1)
seems like a bug -- isn't SetChooseFull
a method on wx.ColourData
?
I made a few changes to get it working and commented the code to illustrate:
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()
The data.SetCustomColor(index, color)
populates the N
custom colors in the dialog. I've circled the one at index 0
below:
精彩评论