tk_menu.py 1.33 KB
Newer Older
1 2 3 4
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
5 6 7 8
    Provides menu structure functionalities.
    
    Name: tk_menu.py
    Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
9 10 11 12 13 14 15 16
"""

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk

17
from tk_itemmenu import *
18 19 20

class Menu(object):
    
21
    parent = None
22
    label = None
23
    _items_menu = None
24
    
25 26
    def __init__(self, parent, label):
        self.parent = parent
27
        self.label = label
28
        self._items_menu = []
29 30 31
        

    def add_command(self, label, action, shortcut):
32
        self._items_menu.append( Command(self, label, action, shortcut) )
33 34
        
    def add_separator(self):
35
        self._items_menu.append( Separator(self) )
36 37
        
    def add_check_button(self, label, action, shortcut):
38
        self._items_menu.append( CheckButton(self, label, action, shortcut) )
39 40 41 42

    def render(self, menubar):
        menu = Tk.Menu(menubar, tearoff=0)
        
43
        for item in self._items_menu:
44
            item.render(menu)
45
            
46 47 48
            if item.shortcut is not None:
                self.parent.bind("<Control-Key-" + item.shortcut + ">", item.action)
                self.parent.bind("<Control-Key-" + item.shortcut.lower() + ">", item.action)
49
            
50
        menubar.add_cascade(label=self.label, menu=menu)