13.07.2015 Views

The wxPython tutorial

The wxPython tutorial

The wxPython tutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Creating custom widgetshttp://www.zetcode.com/wxpython/customwidgets/self.font1 = wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD, True, 'Verdana')self.font2 = wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD, False, 'Verdana')self.SetFont(self.font2)self.SetForegroundColour('#0000ff')self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvent)self.Bind(wx.EVT_MOTION, self.OnMouseEvent)def OnMouseEvent(self, event):if event.Moving():self.SetCursor(wx.StockCursor(wx.CURSOR_HAND))self.SetFont(self.font1)elif event.LeftUp():webbrowser.open_new(self.url)else:self.SetCursor(wx.NullCursor)self.SetFont(self.font2)event.Skip()class HyperLink(wx.Frame):def __init__(self, parent, id, title):wx.Frame.__init__(self, parent, id, title, size=(220, 150))panel = wx.Panel(self, -1)Link(panel, -1, 'ZetCode', pos=(10, 60), URL='http://www.zetcode.com')motto = GenStaticText(panel, -1, 'Knowledge only matters', pos=(10, 30))motto.SetFont(wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD, False, 'Verdana'))self.Centre()self.Show(True)app = wx.App()HyperLink(None, -1, 'A Hyperlink')app.MainLoop()This hyperlink widget is based on an existing widget. In this example wedon't draw anything, we just use an existing widget, which we modify abit.from wx.lib.stattext import GenStaticTextimport webbrowserHere we import the base widget from which we derive our hyperlinkwidget and the webbrowser module. webbrowser module is a standardpython module. We will use it to open links in a default browser.self.SetFont(self.font2)self.SetForegroundColour('#0000ff')<strong>The</strong> idea behind creating a hyperlink widget is simple. We inherit from abase wx.lib.stattext.GenStaticText widget class. So we have a text2 de 9 27/04/2008 1:07

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!