【问题标题】:Why doesn't this work (wxpython/color dialog)为什么这不起作用(wxpython/color 对话框)
【发布时间】:2011-06-02 12:19:58
【问题描述】:
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 本身就可以了,为什么我在这个方法中使用它时它不起作用

【问题讨论】:

  • 你遇到什么错误???
  • 无,只是不改变背景颜色

标签: python wxpython colordialog


【解决方案1】:

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 处圈出了一个:

【讨论】:

  • 我得到了除了第二部分之外的所有内容,为什么你需要为 RGB 设置索引,当我打印返回的 RGB 时,我得到了一个 4 元组 (255,255,0,255)...我假设是阿尔法???
  • 那行SetCustomColour 只是显示了如何在颜色选择器对话框中填充自定义颜色(我已经更新了答案以显示这一点)。由于选择器没有alpha 的滑块(至少在我的系统上)它总是返回255(完全不透明)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多