How to use Timeline component in your wxPython application¶
Note: This is work in progress and feedback is welcome.
The core component in Timeline, the canvas where events are drawn, is a reusable component that any wxPython application can use. This page documents how to use that component.
Importing¶
Currently, the canvas component is embedded in the Timeline source code. All
code related to the canvas component is located in timelinelib.canvas
. This
is not 100% true because the canvas component depends on other parts of
timelinelib
. The long term goal though is that it shouldn’t so that the
timelinelib.canvas
package can be extracted to its own project.
In order to use the canvas component, we must obtain the Timeline source code
and make sure that the source/timelinelib
folder is on our Python path.
For the time being, we also need to setup the gettext translation function. The canvas currently depends on gettext, but it should not in the future. We can use this function to setup gettext:
def install_gettext_in_builtin_namespace():
def _(message):
return message
import builtins
if not "_" in builtins.__dict__:
builtins.__dict__["_"] = _
Hint
If we get an error similar to the one bellow, gettext has not been properly setup:
Traceback (most recent call last):
..
NameError: name '_' is not defined
Example¶
Here is a complete example how to use the canvas component:
make_sure_timelinelib_can_be_imported()
install_gettext_in_builtin_namespace()
import wx
from timelinelib.canvas import TimelineCanvas
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, size=(800, 400))
self._create_canvas()
self._display_example_timeline()
def _create_canvas(self):
self.canvas = TimelineCanvas(self)
self.canvas.Bind(wx.EVT_MOUSEWHEEL, self._on_mousewheel)
def _on_mousewheel(self, event):
self.canvas.Scroll(event.GetWheelRotation() / 1200.0)
def _display_example_timeline(self):
# The only way to populate the canvas at the moment is to use a
# database object from Timeline and call its display_in_canvas method.
from timelinelib.db import db_open
db = db_open(":tutorial:")
db.display_in_canvas(self.canvas)
if __name__ == "__main__":
app = wx.App()
frame = MainFrame()
frame.Show()
app.MainLoop()