Skip to Content
Python GUI Programming with Tkinter
book

Python GUI Programming with Tkinter

by Alan D. Moore
May 2018
Beginner to intermediate content levelBeginner to intermediate
452 pages
11h 26m
English
Packt Publishing
Content preview from Python GUI Programming with Tkinter

Creating and using our selector function

With our classes created, let's add a simple selector function to return the appropriate class for each platform:

  1. . Add the get_main_menu_for_os() method at the end of the mainmenu.py file as follows:
def get_main_menu_for_os(os_name):
    menus = {
        'Linux': LinuxMainMenu,
        'Darwin': MacOsMainMenu,
        'freebsd7': LinuxMainMenu,
        'Windows': WindowsMainMenu
    }
    return menus.get(os_name, GenericMainMenu)
  1. The keys in this dictionary are the output strings from platform.system(), which we point to a platform-appropriate menu class. In the event we've passed some new system, we default to the GenericMainMenu class. The application.py file only needs to import this function:
from .mainmenu import get_main_menu_for_os ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python GUI Programming with Tkinter - Second Edition

Python GUI Programming with Tkinter - Second Edition

Alan D. Moore

Publisher Resources

ISBN: 9781788835886Supplemental Content