Skip to main content

Lightweight navigation wrapper for Tkinter

Project description

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):
    # Your pages
    pages = [PageOne, PageTwo]

    tknav.Wrapper.__init__(self, pages)

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

    # Set inital state, not required
    self.app_state = {'foo': 'bar'}

    # 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().

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tkinter-nav-0.0.2.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

tkinter_nav-0.0.2-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file tkinter-nav-0.0.2.tar.gz.

File metadata

  • Download URL: tkinter-nav-0.0.2.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for tkinter-nav-0.0.2.tar.gz
Algorithm Hash digest
SHA256 4a6e8412dbacd152ba1648b46a374229588550c985c08b4f35d341a6322ca855
MD5 5d2f59719c029a3a201f0f21e3aca791
BLAKE2b-256 1dbd4d23ea74d00eefd43fab807bf4cba21f6d869859361d7773918f601df42c

See more details on using hashes here.

File details

Details for the file tkinter_nav-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: tkinter_nav-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for tkinter_nav-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c645124aac73f044e196247f4ac18477d9b4aea4e4c5d32d6829a4ad40aea7f5
MD5 cd7b5cbf724e34e0ea2174cf9b690075
BLAKE2b-256 0923e43de9d9706468b224c45695516971259068652c79aeb5970eb7d158a974

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page