forked from jwdj/EasyABC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wxhelper.py
102 lines (82 loc) · 2.63 KB
/
wxhelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import wx
import sys
PY3 = sys.version_info.major > 2
def wx_cursor(c):
if PY3:
return wx.Cursor(c)
else:
return wx.StockCursor(c)
def wx_colour(c):
if PY3:
return wx.Colour(c)
else:
return wx.NamedColour(c)
def delete_menuitem(menu, item):
if PY3:
menu.Delete(item)
else:
menu.DeleteItem(item)
def append_submenu(menu, label, submenu):
if PY3:
return menu.AppendSubMenu(submenu, label)
else:
return menu.AppendMenu(-1, label, submenu)
def append_menu_item(menu, label, description, handler, kind=wx.ITEM_NORMAL, id=-1, bitmap=None):
menu_item = wx.MenuItem(menu, -1, label, description, kind)
if bitmap is not None:
menu_item.SetBitmap(bitmap)
if PY3:
menu.Append(menu_item)
else:
menu.AppendItem(menu_item)
if handler is not None:
if menu.InvokingWindow is not None:
menu.InvokingWindow.Bind(wx.EVT_MENU, handler, menu_item)
else:
menu.Bind(wx.EVT_MENU, handler, menu_item)
return menu_item
def append_to_menu(menu, items):
for item in items:
if len(item) == 0:
menu.AppendSeparator()
elif len(item) == 2:
label = item[0]
sub_menu = item[1]
if not isinstance(sub_menu, wx.Menu):
sub_menu = create_menu(sub_menu, menu.InvokingWindow)
append_submenu(menu, label, sub_menu)
else:
id = None
after_add = None
if isinstance(item[0], int):
id = item[0]
item = tuple(list(item)[1:]) # strip id from tuple
if len(item) > 3:
after_add = item[-1]
if hasattr(after_add, '__call__'):
item = tuple(list(item)[0:-1]) # strip after-add-function
menu_item = append_menu_item(menu, *item, id=id)
if after_add is not None:
after_add(menu_item)
def create_menu(items, parent=None):
menu = wx.Menu()
if parent is not None:
menu.InvokingWindow = parent
append_to_menu(menu, items)
return menu
def create_menu_bar(items, parent=None):
menuBar = wx.MenuBar()
if parent is not None:
menuBar.InvokingWindow = parent
for item in items:
label = item[0]
items = item[1]
if not isinstance(items, wx.Menu):
items = create_menu(items, parent=parent)
menuBar.Append(items, label)
return menuBar
def wx_bitmap(width, height, depth=-1):
if PY3:
return wx.Bitmap(width, height, depth)
else:
return wx.EmptyBitmap(width, height, depth)