this postのメソッドを使用してスクロール可能なFigureを含むtkinterウィンドウを作成しました。基本的には、私はtk.Scaleを使ってサイズ変更可能な比較的大きなイメージをロードしますが、Figureを含むウィンドウを一定の大きさにしたい。問題は、私の人物が〜16x16インチに達すると、プログラムは堪えがたく遅くなるということです。どんなアイデアでも大歓迎です。matplotlibの大きなスクロール可能図形が極端に遅い
特にスローラインはfigure.set_size_inches([factor * s for s in oldSize])
ここでウィンドウの全体のコードですされています
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import tkinter as tk
from tkinter import ttk
class MacroWindow(tk.Tk):
def __init__(self, controller, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs) #initialize regular Tk stuff
#set properties for main window
tk.Tk.wm_title(self, "Macro View")
tk.Tk.geometry(self, newGeometry = '600x700+200+200')
#define container for what's in the window
self.controller = controller
self.figSize_inches = [8,8]
self.addScrollingFigure()
frame_buttons = ttk.Frame(self)
frame_buttons.grid(row = 1, column = 0, sticky = 'nsew')
button_loadMacroImage = ttk.Button(frame_buttons,text = "Load Test Macro Image", command =
lambda: self.loadMacroImage())
button_loadMacroImage.grid(row = 0, column = 0, padx = 10, pady = 10, sticky = 'nw')
self.scale_zoom = tk.Scale(self, orient = tk.VERTICAL)
self.scale_zoom.grid(row = 2, column = 0, sticky = 'ew')
self.scale_zoom.config(command = self.changeSize, from_=.1, to=5, resolution = .1)
def addScrollingFigure(self):
self.frame_canvas = ttk.Frame(self)
self.frame_canvas.grid(row = 0, column = 0, sticky = 'nsew')
# set up canvas with scrollbars
canvas = tk.Canvas(self.frame_canvas)
canvas.grid(row = 0, column = 0, sticky = 'nsew')
xScrollbar = tk.Scrollbar(self.frame_canvas, orient = tk.HORIZONTAL)
yScrollbar = tk.Scrollbar(self.frame_canvas, orient = tk.VERTICAL)
xScrollbar.grid(row = 1, column = 0, sticky = 'ew')
yScrollbar.grid(row = 0, column = 1, sticky = 'ns')
canvas.config(xscrollcommand = xScrollbar.set)
xScrollbar.config(command = canvas.xview)
canvas.config(yscrollcommand = yScrollbar.set)
yScrollbar.config(command = canvas.yview)
#create figure and axis
f_wholeCellFig = Figure(figsize = self.figSize_inches, dpi = fig_dpi)
a=f_wholeCellFig.add_subplot(1,1,1)
f_wholeCellFig.subplots_adjust(left = 0, right = 1, bottom = 0, top = 1, wspace = 0.02, hspace = 0)
self.wholeCellFig = f_wholeCellFig
self.wholeCellAx = a
#plug in the figure
figAgg = FigureCanvasTkAgg(f_wholeCellFig,canvas)
mplCanvas = figAgg.get_tk_widget()
self.mplCanvas = mplCanvas
self.canvas = canvas
# and connect figure with scrolling region
self.cwid = canvas.create_window(0, 0, window=mplCanvas, anchor='nw')
self.changeSize(1.0)
def changeSize(self,factor):
if not isinstance(factor,float):
factor = self.scale_zoom.get()
figure = self.wholeCellFig
oldSize = self.figSize_inches
figure.set_size_inches([factor * s for s in oldSize])
wi,hi = [i*figure.dpi for i in figure.get_size_inches()]
self.mplCanvas.config(width = wi, height = hi)
self.canvas.itemconfigure(self.cwid, width = wi, height = hi)
self.canvas.config(scrollregion = self.canvas.bbox('all'), width = 500, height = 500)
figure.subplots_adjust(left = 0, bottom = 0, top = 1, right = 1)
figure.canvas.draw()
def loadMacroImage(self):
if simulation:
image = io.imread('../testing/macroImage.tif')
a = self.wholeCellAx
a.clear()
a.axis('equal')
a.axis('off')
self.volume = image
self.multi_slice_viewer()
def multi_slice_viewer(self):
ax = self.wholeCellAx
self.scale_z.config(command = self.scaleCallback, from_=0, to=self.volume.shape[0]-1)
ax.index = self.volume.shape[0] // 2
self.scale_z.set(ax.index)
ax.imshow(self.volume[ax.index])
self.wholeCellFig.canvas.draw()
root = tk.Tk()
window = MacroWindow(root)
root.mainloop()