[wxPython-users] How to change background color in dialogs?
Jerry LeVan
jerry.levan at gmail.com
Thu May 3 15:30:43 PDT 2007
On May 3, 2007, at 3:32 PM, Kevin Ollivier wrote:
> Hi Jerry,
>
> On May 3, 2007, at 12:15 PM, Jerry LeVan wrote:
>
>>
>> On May 3, 2007, at 2:29 PM, Werner F. Bruhin wrote:
>>
>>> Hi Jerry,
>>>
>>> Jerry LeVan wrote:
>>>> Hi,
>>>>
>>>> I can't seem to find the magic to change the background color
>>>> in a dialog...
>>>>
>>>> Here is a fragment that does not work:
>>> It worked for me, i.e. I get an empty blue dialog screen, I am on:
>>>
>>> # Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32
>>> bit (Intel)]
>>> # wxPython 2.8.4.0.20070502, Boa Constructor 0.5.2
>>>
>>>
>>> Werner
>>
>> Hmmm,
>>
>> Must be a mac problem, I tested on a Linux Fedora 6 box and get
>> a blue background...(Using the current FC6 Python/wxPython RPMS).
>>
>> Where should (suspected) Mac bugs be reported?
>
> You can always use the SF bug tracker for wxWidgets (at http://
> www.sf.net/projects/wxwindows), but I'm fairly certain that the
> problem you're having is a limitation of the native control on Mac.
> What you can do to make this work everywhere is create a wx.Panel,
> add it as the dialog's child, and then call SetBackgroundColour on
> the panel. This should give you the desired results.
>
> Regards,
>
> Kevin
>
Ugh,
The problem appears to nasty on the mac ...
For a "Simple Colored background" it *appears* that
SetBackgroundColour cannot be called
for a frame. Consider this example from WxIA.
*************************
import wx
class Frame(wx.Frame):
pass
class App(wx.App):
def OnInit(self):
self.frame = Frame(parent=None, title='Spare')
#self.frame.SetBackGroundColour("light blue")
self.panel = wx.Panel(parent=self.frame)
self.panel.SetBackgroundColour("light blue")
self.frame.Show()
self.SetTopWindow(self.frame)
return True
if __name__ == '__main__':
app = App()
app.MainLoop()
*****************
Run as above I get a light blue panel, good!
If I uncomment the SetBackgroundColor call for the frame and
comment out the two "panel" statements, the program shows
a fragment of a window and immediately dies with no error messages.
Ok, it looks like a panel is necessary for colored backgrounds.
I modified the "validator2.py" program in WxIA to use a panel
**************************
import wx
import pprint
about_txt = """\
Enter Parameters For Postgresql Login. """
class DataXferValidator(wx.PyValidator):
def __init__(self, data, key):
wx.PyValidator.__init__(self)
self.data = data
self.key = key
print "key ", key
def Clone(self):
"""
Note that every validator must implement the Clone() method.
"""
return DataXferValidator(self.data, self.key)
def Validate(self, win):
print "In Validate"
return True
def TransferToWindow(self):
print "In Transfer to Window\n"
textCtrl = self.GetWindow()
textCtrl.SetValue(self.data.get(self.key, ""))
return True
def TransferFromWindow(self):
print "In transfer from Window"
textCtrl = self.GetWindow()
self.data[self.key] = textCtrl.GetValue()
return True
class LoginDialog(wx.Dialog):
def __init__(self, data):
wx.Dialog.__init__(self, None, -1, "Postgresql Login
Dialog",style=wx.DEFAULT_DIALOG_STYLE|wx.WS_EX_VALIDATE_RECURSIVELY)
self.panel = wx.Panel(self,-1)
self.panel.SetBackgroundColour("light blue")
# Create the text controls
about = wx.StaticText(self.panel, -1, about_txt)
user_l = wx.StaticText(self.panel, -1, "User:")
password_l = wx.StaticText(self.panel, -1, "Password:")
host_l = wx.StaticText(self.panel, -1, "Host:")
dbname_l = wx.StaticText(self.panel, -1, "Dbname:")
port_l = wx.StaticText(self.panel, -1, "Port:")
user_t = wx.TextCtrl(self.panel, validator=DataXferValidator
(data, "user"))
print user_t.GetId()
password_t = wx.TextCtrl(self.panel,style=wx.TE_PASSWORD,
validator=DataXferValidator(data, "password"))
host_t = wx.TextCtrl(self.panel, validator=DataXferValidator
(data, "host"))
dbname_t = wx.TextCtrl(self.panel,
validator=DataXferValidator(data, "dbname"))
port_t = wx.TextCtrl(self.panel, validator=DataXferValidator
(data, "port"))
# Use standard button IDs
okay = wx.Button(self.panel, wx.ID_OK,"Login")
okay.SetDefault()
cancel = wx.Button(self.panel, wx.ID_CANCEL)
# Layout with sizers
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(about, 0, wx.ALL, 5)
sizer.Add(wx.StaticLine(self.panel), 0, wx.EXPAND|wx.ALL, 5)
fgs = wx.FlexGridSizer(5, 2, 5, 5)
fgs.Add(user_l, 0, wx.ALIGN_RIGHT)
fgs.Add(user_t, 0, wx.EXPAND)
fgs.Add(password_l, 0, wx.ALIGN_RIGHT)
fgs.Add(password_t, 0, wx.EXPAND)
fgs.Add(host_l, 0, wx.ALIGN_RIGHT)
fgs.Add(host_t, 0, wx.EXPAND)
fgs.Add(dbname_l, 0, wx.ALIGN_RIGHT)
fgs.Add(dbname_t, 0, wx.EXPAND)
fgs.Add(port_l, 0, wx.ALIGN_RIGHT)
fgs.Add(port_t, 0, wx.EXPAND)
fgs.AddGrowableCol(1)
sizer.Add(fgs, 0, wx.EXPAND|wx.ALL, 5)
btns = wx.StdDialogButtonSizer()
btns.AddButton(okay)
btns.AddButton(cancel)
btns.Realize()
sizer.Add(btns, 0, wx.EXPAND|wx.ALL, 5)
self.panel.SetSizer(sizer)
sizer.Fit(self.panel)
app = wx.PySimpleApp()
data = { "user" : "levan", "port" : "5432" }
dlg = LoginDialog(data)
dlg.ShowModal()
dlg.Destroy()
wx.MessageBox("You entered these values:\n\n" +
pprint.pformat(data))
app.MainLoop()
********************************
This gives a light blue background *but* the
validation and transfer functions are never called !
(The keys *are* printed in the init routine (twice)).
The user and port fields are never filled in and typing
anything into the fields does not change the "data" variable
when exiting the dialog.
In this case I get identical behavior on my linux box. ie
the fields are not initialized at startup and not copied
when the login button is clicked.
If I use the "frame" to embed the controls on the linux box
and skip the panel stuff everything works as desired on the
linux box.
Any Wx-Mac users have any suggestions?
Thanks Jerry
More information about the wxpython-users
mailing list