-
Notifications
You must be signed in to change notification settings - Fork 11
/
ui.py
163 lines (141 loc) · 5.23 KB
/
ui.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""
Central place for flags, variables, small utilities etc. that determine
how Tk is used throughout IDLE.
"""
import os
import tkinter
from tkinter import ttk
from tkinter.font import Font
# Should IDLE use themed-Tk widgets (ttk)?
using_ttk = False
# What windowing system are we using?
windowing_system = None # will be set to 'aqua', 'win32', or 'x11'
# Do we need to include a Sizegrip widget?
need_sizegrip = False
# Cursor to use to indicate clickable things, like links - usually a hand
clickable_cursor = 'hand2'
# Tk root window for our application
root = None
# Classes for widgets that have ttk counterparts; since they'll have
# different options, code will need to check which they're using for all
# but the simplest things
Button = tkinter.Button
Frame = tkinter.Frame
Label = tkinter.Label
Scrollbar = tkinter.Scrollbar
Spinbox = tkinter.Spinbox
PanedWindow = tkinter.PanedWindow
Entry = tkinter.Entry
Checkbutton = tkinter.Checkbutton
Radiobutton = tkinter.Radiobutton
# Initialize our common variables; this needs to be called before the
# variables can be used. We require this to avoid the overhead of creating
# a temporary Tk instance.
def init(root_, allow_ttk=True):
global _initialized, root, using_ttk, windowing_system, need_sizegrip,\
clickable_cursor, Button, Frame, Label, Scrollbar, PanedWindow,\
Spinbox, Entry, Checkbutton, Radiobutton
if _initialized:
return
root = root_
if allow_ttk:
try:
b = ttk.Button(root)
using_ttk = True
except Exception:
pass
windowing_system = root.call('tk', 'windowingsystem')
try:
_tooltipfont = Font(name='TkTooltipFont', exists=True, root=root)
except tkinter.TclError:
_tooltipfont = Font(family='helvetica', size=10, root=root)
if using_ttk:
Button = ttk.Button
Frame = ttk.Frame
Label = ttk.Label
Scrollbar = ttk.Scrollbar
PanedWindow = ttk.PanedWindow
Entry = ttk.Entry
Checkbutton = ttk.Checkbutton
Radiobutton = ttk.Radiobutton
Spinbox = _Spinbox # see below
if windowing_system == 'aqua':
clickable_cursor = 'pointinghand'
import platform
v, _, _ = platform.mac_ver()
major, minor = v.split('.')[:2]
if (int(major) == 10 and int(minor) < 7):
need_sizegrip = True
# NOTE: Tk 8.6 defines a <<ContextMenu>> event
root.event_add('<<context-menu>>', '<Button-2>', '<Control-Button-1>')
else:
root.event_add('<<context-menu>>', '<Button-3>')
_initialized = True
_initialized = False
_tooltipfont = None
def padframe(frame, padding):
"Convenience procedure to add padding to a frame, ttk or otherwise"
try:
frame['padding'] = padding
except tkinter.TclError:
frame['padx'] = padding
frame['pady'] = padding
return frame
def image(filename):
"Return an image object for a file in our 'Icons' directory"
dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'Icons')
return tkinter.PhotoImage(master=root, file=os.path.join(dir, filename))
class _Spinbox(tkinter.Spinbox):
"""
A ttk::spinbox was added in Tk 8.5.9; use it if present, otherwise
use a spinbox. Note the two have different options and methods, so this
only works for the basics.
"""
def __init__(self, master=None, cnf={}, **kw):
hasTtkSpinbox = master and master.tk.call('info', 'commands',
'ttk::spinbox')
base = 'ttk::spinbox' if hasTtkSpinbox else 'spinbox'
tkinter.Widget.__init__(self, master, base, cnf, kw)
# TODO - duplication from uitabs.py
_tooltip = None
def tooltip_clear():
global _tooltip
if _tooltip is not None:
if _tooltip['window'] is not None:
_tooltip['window'].destroy()
if _tooltip['afterid'] is not None:
_tooltip['event'].widget.after_cancel(_tooltip['afterid'])
_tooltip = None
def tooltip_schedule(event, callback):
global _tooltip
tooltip_clear()
_tooltip = {'window': None, 'event': event, 'callback': callback,
'afterid': event.widget.after(1500, _tooltip_display)}
def _tooltip_display():
global _tooltip
_tooltip['afterid'] = None
event = _tooltip['event']
callback = _tooltip['callback']
_tooltip['event'] = None
_tooltip['callback'] = None
ret = callback(event)
if ret is not None:
txt, x, y = ret
tw = _tooltip['window'] = tkinter.Toplevel(event.widget)
tw.wm_withdraw()
tw.wm_geometry("+%d+%d" % (x, y))
tw.wm_overrideredirect(1)
try:
tw.tk.call("::tk::unsupported::MacWindowStyle", "style",
tw._w, "help", "noActivates")
except tkinter.TclError:
pass
lbl = tkinter.Label(tw, text=txt, justify='left',
background="#ffffe0", borderwidth=0, font=_tooltipfont)
if windowing_system != 'aqua':
lbl['borderwidth'] = 1
lbl['relief'] = 'solid'
lbl.pack()
tw.update_idletasks() # calculate window size to avoid resize flicker
tw.deiconify()
tw.lift() # needed to work around bug in Tk 8.5.18+ (issue #24570)