Ejemplo n.º 1
0
def init(window):
    """Set up Porcupine.

    Usually :source:`porcupine/__main__.py` calls this function and you
    don't need to call it yourself. This function can still be useful if
    you want to run Porcupine minimally from another Python program for
    some reason.

    The *window* argument can be a tkinter root window or a ``Toplevel``
    widget.

    Example::

        import tkinter
        import porcupine

        root = tkinter.Tk()
        porcupine.init(root)
        root.protocol('WM_DELETE_WINDOW', porcupine.quit)
        root.mainloop()
    """
    global _main_window
    global _tab_manager

    assert [_main_window, _tab_manager
            ].count(None) != 1, ("porcupine seems to be partially initialized")
    if _main_window is not None:
        raise RuntimeError("porcupine.init() was called twice")
    _main_window = window  # get_main_window() works from now on

    utils._init_images()
    filetypes.init()
    dirs.makedirs()

    _tab_manager = tabs.TabManager(window)
    _tab_manager.pack(fill='both', expand=True)
    for binding, callback in _tab_manager.bindings:
        window.bind(binding, callback, add=True)

    menubar.init()
    window['menu'] = menubar.get_menu(None)
    _setup_actions()
Ejemplo n.º 2
0
        self._save_hash = save_hash
        self._update_title()

        # this seems to work well enough
        self.textwidget.mark_set('insert', cursor_pos)
        self.textwidget.see('insert linestart')
        self.textwidget.see('insert linestart + 5 lines')
        self.textwidget.see('insert linestart - 5 lines')
        return self


if __name__ == '__main__':
    # test/demo
    from porcupine.utils import _init_images
    root = tkinter.Tk()
    _init_images()

    tabmgr = TabManager(root)
    tabmgr.pack(fill='both', expand=True)
    tabmgr.bind('<<NewTab>>',
                lambda event: print("added tab", event.data_widget.i),
                add=True)
    tabmgr.bind('<<NotebookTabChanged>>',
                lambda event: print("selected",
                                    event.widget.select().i),
                add=True)

    def on_ctrl_w(event):
        if tabmgr.tabs():
            tabmgr.close_tab(tabmgr.select())