wxPython:更新控件的背景颜色时出现问题

时间:2009-11-10 19:53:17

标签: python wxpython

[编辑 - 减少并重新发布代码,重申问题]

我想更改框架(或面板;无论哪个使其工作)的背景颜色。 问题是,在我点击控件(特别是滑块控件)之前,该框架(或面板)上控件的背景颜色没有更新背景颜色。

有关如何解决此问题的任何想法?

这是一些演示代码:

import wx

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.panel_1 = wx.Panel(self, -1)
        self.sliderDarken = wx.Slider(self.panel_1, -1, 206, 0, 255)
        self.label_3 = wx.StaticText(self.panel_1, -1, "This slider should darken the main panel.\n")
        self.btnPopup = wx.Button(self.panel_1, -1, "This button will pop up a dialog to dim the panel.")
        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_COMMAND_SCROLL, self.onDarken, self.sliderDarken)
        self.Bind(wx.EVT_BUTTON, self.onDialogPopup, self.btnPopup)

    def __set_properties(self):
        self.SetTitle("Main Frame")

    def __do_layout(self):
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_3.Add(self.sliderDarken, 0, 0, 0)
        sizer_3.Add(self.label_3, 0, 0, 0)
        sizer_3.Add(self.btnPopup, 0, 0, 0)
        self.panel_1.SetSizer(sizer_3)
        sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()

    def onDarken(self, event): 
        self.panel_1.SetBackgroundColour(wx.Colour(self.sliderDarken.GetValue(),self.sliderDarken.GetValue(),self.sliderDarken.GetValue()))
        self.panel_1.Refresh()

    def onDialogPopup(self, event): 
        dlgPopup=MyDialog1(None)
        dlgPopup.Show()



class MyDialog1(wx.Dialog):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.sliderDarkenPopup = wx.Slider(self, -1, 206, 0, 255, style=wx.SL_HORIZONTAL|wx.SL_AUTOTICKS|wx.SL_LABELS)
        self.label_2 = wx.StaticText(self, -1, "This slider should darken the panel on the main frame.")
        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_COMMAND_SCROLL, self.onDarkenPopup, self.sliderDarkenPopup)

    def __set_properties(self):
        self.SetTitle("Dimmer Pop up")


    def __do_layout(self):
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_2.Add(self.sliderDarkenPopup, 0, 0, 0)
        sizer_2.Add(self.label_2, 0, 0, 0)
        self.SetSizer(sizer_2)
        sizer_2.Fit(self)
        self.Layout()

    def onDarkenPopup(self, event): 
        frame_1.panel_1.SetBackgroundColour(wx.Colour(self.sliderDarkenPopup.GetValue(),self.sliderDarkenPopup.GetValue(),self.sliderDarkenPopup.GetValue()))
        frame_1.panel_1.Refresh()



if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = MyFrame(None, -1, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()

2 个答案:

答案 0 :(得分:1)

尝试在每个框架中放置一个面板,然后将控件放在面板上。在wxPython中,框架不喜欢有多个窗口(特别是对于Windows操作系统),因此通常有框架拥有一个面板和面板以拥有其他控件。

如果这不能解决问题,请更清楚地表达您的问题并将代码缩减到显示问题的最小程序,然后我们(或至少我)更容易理解。

答案 1 :(得分:1)

尝试生成并处理wx.SysColourChangedEvent。

  def onDarken(self, event): 
        self.panel_1.SetBackgroundColour(wx.Colour(self.sliderDarken.GetValue(),self.sliderDarken.GetValue(),self.sliderDarken.GetValue()))
        event = wx.SysColourChangedEvent()
        self.ProcessEvent(event)