Skip to main content

Tkinter-nav

Lightweight navigation wrapper for Tkinter.

Install

pip install tkinter-nav

Usage

Create Some Pages

import tkinter as tk
import tkinter_nav as tknav


class PageOne(tknav.Page):

    def __init__(self, parent):
        tknav.Page.__init__(self, parent, 'name_of_page')

        # Use as any Frame
        # Page extends tk.Frame
        tk.Button(self, ...).pack()

    def page_did_mount(self):
      ...

    def page_did_unmount(self):
      ...


class PageTwo(tknav.Page):
  ...


class PageThree(tknav.Page):
  ...

Create your App

...

class App(tknav.Wrapper):

  def __init__(self):
    pages = [PageOne, PageTwo]

    tknav.Wrapper.__init__(
        self,
        # Your pages
        pages=pages,
        # Set inital state, not required
        start_state={'foo': 'bar'}
    )

    # Use as any Tk instance
    # tknav.Wrapper extends tk.Tk
    self.geometry('200x200')
    self.title('My Nav App')

    # Show page
    self.show_page('page_one')


# Run
if __main__ == '__main__':
  App().mainloop()

Pages

Navigate Between Pages

  • From the constructor
class PageOne(tknav.Page):

    def __init__(self, parent):
        ...

        tk.Button(
          command=lambda: self.navigate('page_two')
        ).pack()
  • From a handler function
class PageOne(tknav.Page):
    ...

    def handleClick(self):
      self.navigate('page_two')

Mount and Unmount

  • page_did_mount: When the page is shown
  • page_did_unmount: When the page is hidden

Note: You do not have to use them. They will be defined with a pass statement.

class SomePage(tknav.Page):
    ...

    def page_did_mount(self):
      print('Page did mount')

    def page_did_unmount(self):
      print('Page did unmount')

Note: If you are familiar with React, they share the same role as ComponentDidMount and ComponentDidUnmount.

State

You can set a global state for your app which will enable you to share data between pages.

class App(tknav.Nav):

  def __init__(self):
    ...

    self.app_state = {'foo': 'bar'}

class PageOne(tknav.Page):
    ...

    def function(self):
      # get a value
      print(self.app_state['foo']) #bar

      # set a new value
      self.app_state['bar'] = 'foo'

When navigate() is called, the local state is merged with the global state.

class PageTwo(tknav.Page):
    ...

    def function(self):
      print(self.app_state['bar']) #foo

Note: If you are familiar with React, same principle.

Example

Is this example, we navigated from page_one to page_two to page_one again. The state is printed in page_did_mount() and changed in page_did_unmount().

Release files for tkinter-nav 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tkinter-nav 0.0.5
File Size Uploaded
tkinter-nav-0.0.5.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tkinter-nav 0.0.5
File Interpreter ABI Platform
tkinter_nav-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 12.3 kB

Release files / tkinter-nav-0.0.5.tar.gz

Download URL tkinter-nav-0.0.5.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
915aa476b760d5a4523f21f6800e7c83afc742cb05d0102cf7fa572021cf0276
BLAKE2b-256 checksum
How to use checksums
246fb13ae48239c041715fbee1d5b97ecd3503ca54530c795d96c7a0d071b3e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.2

Release files / tkinter_nav-0.0.5-py3-none-any.whl

Download URL tkinter_nav-0.0.5-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
463a7a4e2454d05df82a624df89e55304f4315a26397efafec08120e9bbf8216
BLAKE2b-256 checksum
How to use checksums
5aef187abeadd92305fb9f8a375c4cfe358a206ef7481a7238aba83752a97b70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.2

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page