Chuck Esterbrook wrote:
I want to put a clickable URL in my "About" panel:
http://foobar.com/
I'm guessing that wx.StaticText(), an EVT_LEFT_DOWN, and Python's webbrowser will do the trick, but I wanted to see if anyone on the list had already done this?
Hi Chuck,
I do this using a flattened button (so it looks just like a static text or bitmap). The attached probably has a few outside dependencies that I'm forgetting to include, but should give the general idea.
-tim
# In my application.
def ShowAbout(self, event=None):
title = "About " + self.name
modules = '\n '.join(["%s [%s]" % (x.name, x.version) for x in self.modules])
message = "About " + self.name
message += "\nInstalled Modules:\n " + modules
libs = ((tools.LoadBitmap("python"), "http://www.python.org"),
(tools.LoadBitmap("wxPython"), "http://www.wxpython.org"),
(tools.LoadBitmap("wxWindows"), "http://www.wxwindows.org"))
licensedir = "Licenses"
tools.ShowAbout(title, message, libs, licensedir)
# In tools
*def LoadBitmap(name, mask=(255,0,255)): # Pink is default mask color
path = os.path.join(utilities.findHomeDirectory(), "images", name)
if sys.platform == "win32":
bmp = wx.wxBitmap(path + ".bmp", wx.wxBITMAP_TYPE_BMP)
else:
bmp = wx.wxBitmap(path + ".xpm", wx.wxBITMAP_TYPE_XPM)
img = wx.wxImageFromBitmap(bmp)
img.SetMaskColour(*mask)
return img.ConvertToBitmap()*
def ShowAbout(title, message, libs, licensedir, target=wx.NULL):
dlg = wx.wxDialog(target, -1, title, style=wx.wxDEFAULT_DIALOG_STYLE|wx.wxSUNKEN_BORDER) button = wx.wxButton(dlg, -1, "Ok")
wx.EVT_BUTTON(dlg, button.GetId(), lambda e, d=dlg: d.EndModal(0))
vert = wx.wxBoxSizer(wx.wxVERTICAL)
txt = wx.wxStaticText(dlg, -1, message + "\n\nPowered by:")
vert.Add(txt, 0, wx.wxTOP | wx.wxLEFT | wx.wxRIGHT, 10)
horz = wx.wxBoxSizer(wx.wxHORIZONTAL)
for bitmap, url in libs:
bbutton = wx.wxBitmapButton(dlg, -1, bitmap, style=wx.wxNO_3D)
wx.EVT_BUTTON(dlg, bbutton.GetId(), lambda e, url=url, wb=webbrowser: wb.open(url))
horz.Add(bbutton, 0, wx.wxALIGN_CENTRE)
vert.Add(horz, 0, wx.wxALIGN_CENTER | wx.wxALL, 10)
licensepath = "file://" + os.path.join(utilities.findHomeDirectory(),licensedir, "index.html")
horz = wx.wxBoxSizer(wx.wxHORIZONTAL)
for text, url in [("See directory ", None),
('<font color="blue" weight="bold">%s</font>' % licensedir, licensepath),
(' for full text of licenses', None)]:
bmp = wxFancyText.renderToBitmap(text,
background=wx.wxTheBrushList.FindOrCreateBrush(dlg.GetBackgroundColour(),wx.wxSOLID))
bbutton = wx.wxBitmapButton(dlg, -1, bmp, style=wx.wxNO_3D)
if url is not None:
wx.EVT_BUTTON(dlg, bbutton.GetId(), lambda e, url=url, wb=webbrowser: wb.open(url))
horz.Add(bbutton, 0, wx.wxALIGN_LEFT)
vert.Add(horz, 0, wx.wxALIGN_CENTER|wx.wxBOTTOM|wx.wxLEFT|wx.wxRIGHT, 10)
vert.Add(button, 0, wx.wxALIGN_CENTRE|wx.wxBOTTOM, 10)
dlg.SetAutoLayout(wx.true)
dlg.SetSizer(vert)
vert.Fit(dlg)
vert.SetSizeHints(dlg)
dlg.ShowModal()
dlg.Destroy()