views:

480

answers:

2

[EDIT - Reduced and re-posted code, restated question]

I would like to change the background color of a frame (or panel; whichever makes it work). The problem is that the background color of controls on that frame (or panel) don't have their background color updated until I click on the control (the slider control, specifically).

Any ideas on how to fix this?

Here's some demo code:

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()
A: 

Try putting a panel in each frame and then putting your controls on the panel. In wxPython, frames don't like to have multiple windows (especially for Windows OS), so it usually helps to have the frame own one panel and the panel to own the other controls.

If this doesn't solve the problem, please to to phrase your question more clearly and reduce your code to the smallest program that displays the problem, and then it will be easier for us (or at least me) to understand.

tom10
Tried with a panel, but no difference. I also updated the question and code I posted above.
mark
I still don't get it. For example, when do yo expect the background color to change. You make a slider to change the background color, but want it to change before you click the slider? Also, what OS are you on. On my system (Linux), the slider appear vertical and so small they don't move. Is this what you want?
tom10
A: 

Try generating and processing a 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)
Czeck-o-slovakia