44import sys
55import tkinter as Tk
66from tkinter .simpledialog import SimpleDialog
7+ import tkinter .filedialog
8+ import tkinter .messagebox
79from contextlib import contextmanager
810
911import numpy as np
@@ -136,56 +138,56 @@ def _on_timer(self):
136138
137139
138140class 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' ,
189191 }
190192
191193 _keycode_lookup = {
@@ -407,12 +409,7 @@ def scroll_event(self, event):
407409 x = event .x
408410 y = self .figure .bbox .height - event .y
409411 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
416413 FigureCanvasBase .scroll_event (self , x , y , step , guiEvent = event )
417414
418415 def scroll_event_windows (self , event ):
@@ -462,7 +459,7 @@ def _get_key(self, event):
462459 ]
463460
464461 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
466463 for bitmask , prefix , key_name in modifiers :
467464 if event .state & (1 << bitmask ) and key_name not in key :
468465 key = '{0}+{1}' .format (prefix , key )
@@ -479,7 +476,7 @@ def key_release(self, event):
479476
480477 def new_timer (self , * args , ** kwargs ):
481478 """
482- Creates a new backend-specific subclass of :class:` backend_bases.Timer`.
479+ Creates a new backend-specific subclass of `. backend_bases.Timer`.
483480 This is useful for getting periodic events through the backend's native
484481 event loop. Implemented only for backends with GUIs.
485482
@@ -518,9 +515,11 @@ def __init__(self, canvas, num, window):
518515 self .window .withdraw ()
519516 self .set_window_title ("Figure %d" % num )
520517 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
522520 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)
524523 self .toolbar = self ._get_toolbar ()
525524 self .canvas ._tkcanvas .pack (side = Tk .TOP , fill = Tk .BOTH , expand = 1 )
526525 self ._num = num
@@ -582,7 +581,7 @@ def destroy(self, *args):
582581 if self .canvas ._idle_callback :
583582 self .canvas ._tkcanvas .after_cancel (self .canvas ._idle_callback )
584583 self .window .destroy ()
585- if Gcf .get_num_fig_managers ()== 0 :
584+ if Gcf .get_num_fig_managers () == 0 :
586585 if self .window is not None :
587586 self .window .quit ()
588587 self .window = None
@@ -650,7 +649,7 @@ def _Button(self, text, file, command, extension='.gif'):
650649 return b
651650
652651 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.
654653 s = Tk .Frame (
655654 master = self , height = 26 , relief = Tk .RIDGE , pady = 2 , bg = "DarkGray" )
656655 s .pack (side = Tk .LEFT , padx = 5 )
@@ -691,7 +690,6 @@ def configure_subplots(self):
691690 window .grab_set ()
692691
693692 def save_figure (self , * args ):
694- import tkinter .filedialog , tkinter .messagebox
695693 filetypes = self .canvas .get_supported_filetypes ().copy ()
696694 default_filetype = self .canvas .get_default_filetype ()
697695
@@ -817,6 +815,7 @@ def set_cursor(self, cursor):
817815
818816class ToolbarTk (ToolContainerBase , Tk .Frame ):
819817 _icon_extension = '.gif'
818+
820819 def __init__ (self , toolmanager , window ):
821820 ToolContainerBase .__init__ (self , toolmanager )
822821 xmin , xmax = self .toolmanager .canvas .figure .bbox .intervalx
@@ -911,7 +910,6 @@ def set_message(self, s):
911910
912911class SaveFigureTk (backend_tools .SaveFigureBase ):
913912 def trigger (self , * args ):
914- import tkinter .filedialog , tkinter .messagebox
915913 filetypes = self .figure .canvas .get_supported_filetypes ().copy ()
916914 default_filetype = self .figure .canvas .get_default_filetype ()
917915
0 commit comments