4
4
import sys
5
5
import tkinter as Tk
6
6
from tkinter .simpledialog import SimpleDialog
7
+ import tkinter .filedialog
8
+ import tkinter .messagebox
7
9
from contextlib import contextmanager
8
10
9
11
import numpy as np
@@ -136,56 +138,56 @@ def _on_timer(self):
136
138
137
139
138
140
class FigureCanvasTk (FigureCanvasBase ):
139
- keyvald = {65507 : 'control' ,
140
- 65505 : 'shift' ,
141
- 65513 : 'alt' ,
142
- 65515 : 'super' ,
143
- 65508 : 'control' ,
144
- 65506 : 'shift' ,
145
- 65514 : 'alt' ,
146
- 65361 : 'left' ,
147
- 65362 : 'up' ,
148
- 65363 : 'right' ,
149
- 65364 : 'down' ,
150
- 65307 : 'escape' ,
151
- 65470 : 'f1' ,
152
- 65471 : 'f2' ,
153
- 65472 : 'f3' ,
154
- 65473 : 'f4' ,
155
- 65474 : 'f5' ,
156
- 65475 : 'f6' ,
157
- 65476 : 'f7' ,
158
- 65477 : 'f8' ,
159
- 65478 : 'f9' ,
160
- 65479 : 'f10' ,
161
- 65480 : 'f11' ,
162
- 65481 : 'f12' ,
163
- 65300 : 'scroll_lock' ,
164
- 65299 : 'break' ,
165
- 65288 : 'backspace' ,
166
- 65293 : 'enter' ,
167
- 65379 : 'insert' ,
168
- 65535 : 'delete' ,
169
- 65360 : 'home' ,
170
- 65367 : 'end' ,
171
- 65365 : 'pageup' ,
172
- 65366 : 'pagedown' ,
173
- 65438 : '0' ,
174
- 65436 : '1' ,
175
- 65433 : '2' ,
176
- 65435 : '3' ,
177
- 65430 : '4' ,
178
- 65437 : '5' ,
179
- 65432 : '6' ,
180
- 65429 : '7' ,
181
- 65431 : '8' ,
182
- 65434 : '9' ,
183
- 65451 : '+' ,
184
- 65453 : '-' ,
185
- 65450 : '*' ,
186
- 65455 : '/' ,
187
- 65439 : 'dec' ,
188
- 65421 : 'enter' ,
141
+ keyvald = {65507 : 'control' ,
142
+ 65505 : 'shift' ,
143
+ 65513 : 'alt' ,
144
+ 65515 : 'super' ,
145
+ 65508 : 'control' ,
146
+ 65506 : 'shift' ,
147
+ 65514 : 'alt' ,
148
+ 65361 : 'left' ,
149
+ 65362 : 'up' ,
150
+ 65363 : 'right' ,
151
+ 65364 : 'down' ,
152
+ 65307 : 'escape' ,
153
+ 65470 : 'f1' ,
154
+ 65471 : 'f2' ,
155
+ 65472 : 'f3' ,
156
+ 65473 : 'f4' ,
157
+ 65474 : 'f5' ,
158
+ 65475 : 'f6' ,
159
+ 65476 : 'f7' ,
160
+ 65477 : 'f8' ,
161
+ 65478 : 'f9' ,
162
+ 65479 : 'f10' ,
163
+ 65480 : 'f11' ,
164
+ 65481 : 'f12' ,
165
+ 65300 : 'scroll_lock' ,
166
+ 65299 : 'break' ,
167
+ 65288 : 'backspace' ,
168
+ 65293 : 'enter' ,
169
+ 65379 : 'insert' ,
170
+ 65535 : 'delete' ,
171
+ 65360 : 'home' ,
172
+ 65367 : 'end' ,
173
+ 65365 : 'pageup' ,
174
+ 65366 : 'pagedown' ,
175
+ 65438 : '0' ,
176
+ 65436 : '1' ,
177
+ 65433 : '2' ,
178
+ 65435 : '3' ,
179
+ 65430 : '4' ,
180
+ 65437 : '5' ,
181
+ 65432 : '6' ,
182
+ 65429 : '7' ,
183
+ 65431 : '8' ,
184
+ 65434 : '9' ,
185
+ 65451 : '+' ,
186
+ 65453 : '-' ,
187
+ 65450 : '*' ,
188
+ 65455 : '/' ,
189
+ 65439 : 'dec' ,
190
+ 65421 : 'enter' ,
189
191
}
190
192
191
193
_keycode_lookup = {
@@ -407,12 +409,7 @@ def scroll_event(self, event):
407
409
x = event .x
408
410
y = self .figure .bbox .height - event .y
409
411
num = getattr (event , 'num' , None )
410
- if num == 4 :
411
- step = + 1
412
- elif num == 5 :
413
- step = - 1
414
- else :
415
- step = 0
412
+ step = 1 if num == 4 else - 1 if num == 5 else 0
416
413
FigureCanvasBase .scroll_event (self , x , y , step , guiEvent = event )
417
414
418
415
def scroll_event_windows (self , event ):
@@ -462,7 +459,7 @@ def _get_key(self, event):
462
459
]
463
460
464
461
if key is not None :
465
- # note, shift is not added to the keys as this is already accounted for
462
+ # shift is not added to the keys as this is already accounted for
466
463
for bitmask , prefix , key_name in modifiers :
467
464
if event .state & (1 << bitmask ) and key_name not in key :
468
465
key = '{0}+{1}' .format (prefix , key )
@@ -479,7 +476,7 @@ def key_release(self, event):
479
476
480
477
def new_timer (self , * args , ** kwargs ):
481
478
"""
482
- Creates a new backend-specific subclass of :class:` backend_bases.Timer`.
479
+ Creates a new backend-specific subclass of `. backend_bases.Timer`.
483
480
This is useful for getting periodic events through the backend's native
484
481
event loop. Implemented only for backends with GUIs.
485
482
@@ -518,9 +515,11 @@ def __init__(self, canvas, num, window):
518
515
self .window .withdraw ()
519
516
self .set_window_title ("Figure %d" % num )
520
517
self .canvas = canvas
521
- # If using toolmanager it has to be present when initializing the toolbar
518
+ # If using toolmanager it has to be present when initializing the
519
+ # toolbar
522
520
self .toolmanager = self ._get_toolmanager ()
523
- # packing toolbar first, because if space is getting low, last packed widget is getting shrunk first (-> the canvas)
521
+ # packing toolbar first, because if space is getting low, last packed
522
+ # widget is getting shrunk first (-> the canvas)
524
523
self .toolbar = self ._get_toolbar ()
525
524
self .canvas ._tkcanvas .pack (side = Tk .TOP , fill = Tk .BOTH , expand = 1 )
526
525
self ._num = num
@@ -582,7 +581,7 @@ def destroy(self, *args):
582
581
if self .canvas ._idle_callback :
583
582
self .canvas ._tkcanvas .after_cancel (self .canvas ._idle_callback )
584
583
self .window .destroy ()
585
- if Gcf .get_num_fig_managers ()== 0 :
584
+ if Gcf .get_num_fig_managers () == 0 :
586
585
if self .window is not None :
587
586
self .window .quit ()
588
587
self .window = None
@@ -650,7 +649,7 @@ def _Button(self, text, file, command, extension='.gif'):
650
649
return b
651
650
652
651
def _Spacer (self ):
653
- # Buttons are 30px high, so make this 26px tall with padding to center it
652
+ # Buttons are 30px high. Make this 26px tall +2px padding to center it.
654
653
s = Tk .Frame (
655
654
master = self , height = 26 , relief = Tk .RIDGE , pady = 2 , bg = "DarkGray" )
656
655
s .pack (side = Tk .LEFT , padx = 5 )
@@ -691,7 +690,6 @@ def configure_subplots(self):
691
690
window .grab_set ()
692
691
693
692
def save_figure (self , * args ):
694
- import tkinter .filedialog , tkinter .messagebox
695
693
filetypes = self .canvas .get_supported_filetypes ().copy ()
696
694
default_filetype = self .canvas .get_default_filetype ()
697
695
@@ -817,6 +815,7 @@ def set_cursor(self, cursor):
817
815
818
816
class ToolbarTk (ToolContainerBase , Tk .Frame ):
819
817
_icon_extension = '.gif'
818
+
820
819
def __init__ (self , toolmanager , window ):
821
820
ToolContainerBase .__init__ (self , toolmanager )
822
821
xmin , xmax = self .toolmanager .canvas .figure .bbox .intervalx
@@ -911,7 +910,6 @@ def set_message(self, s):
911
910
912
911
class SaveFigureTk (backend_tools .SaveFigureBase ):
913
912
def trigger (self , * args ):
914
- import tkinter .filedialog , tkinter .messagebox
915
913
filetypes = self .figure .canvas .get_supported_filetypes ().copy ()
916
914
default_filetype = self .figure .canvas .get_default_filetype ()
917
915
0 commit comments