2

ねえ、私はこのGUIを作成しました。これは、スクロールされたキャンバスを使用したtkinterインターフェイスでmatplotlibプロットを表示します。ただし、プロットがキャンバスに追加されると、マウスのスクロールイベントは機能しなくなります。tkinterはまったく新しいので、コードをチェックアウトするときに、改善の余地がある場合はこれを考慮してください。だからここにあります:

from Tkinter import *

import matplotlib
#matplotlib.use('TkAgg')

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
import os
import matplotlib.mlab as mlab

class App:

def __init__(self, master, figureList=[]):
    frame = Frame(master, bg='#00BFFF', borderwidth=1, relief=RAISED)
    frame.pack(expand=YES, fill=BOTH)

    master.title("Listed Plots")
    #Fullscreen for windows
    if os.name == 'nt':
        master.wm_state('zoomed')

    #Create a Canvas
    canvas=Canvas(frame,bg='#00BFFF', relief=SUNKEN)       
    canvas.config(highlightthickness=0) 

    #Create a Scrollbar Horisontal
    hbar=Scrollbar(frame,orient=HORIZONTAL)
    hbar.pack(side=BOTTOM,fill=X)
    hbar.config(command=canvas.xview)

    #Create a Scrollbar Vertical
    vbar=Scrollbar(frame,orient=VERTICAL)
    vbar.pack(side=RIGHT,fill=Y)
    vbar.config(command=canvas.yview)

    canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
    canvas.pack(side=TOP,expand=True,fill=BOTH)

    #Create a Frame in the canvas
    canvFrame = Frame(canvas, bg='#00BFFF')
    canvFrame.pack()

    Label(canvFrame, text="Matplots of data", bg='#B4EEB4').pack(side=TOP,fill=X)

    plotNbr = 1
    windowHeight = 10

    for fig in figureList:
        Label(canvFrame, text="#Plotnumber: " + str(plotNbr), bg='#FF7F24').pack(side=TOP,fill=X)

        frm = Frame(canvFrame, bg='#9FB6CD')
        frm.pack(side=TOP, fill=X)

        self.canvasMPL2, self.canvasMPLToolBar2 = getCanvas(frm, fig)
        self.canvasMPL2.pack(side=TOP)
        self.canvasMPLToolBar2.pack(side=TOP)

        #Create space for the plot and tool bar.
        windowHeight = windowHeight + 680
        plotNbr = plotNbr + 1


    canvas.config(width=1200,height= windowHeight)
    canvas.config(scrollregion=(0,0,1200, windowHeight))

    canvas.create_window(0,0, anchor = NW, window = canvFrame, width = 1200, height = windowHeight)

    canvas.focus_set() #Doesnt work with FigureCanvasTkAgg, this steals the focus 
    #canvas.focus_force()
    #canvas.grab_set_global()

    #scrollwheel settings
    canvas.configure(yscrollincrement='25')

    def rollWheel(event):
        #print "Mousescroll"
        direction = 0
        if event.num == 5 or event.delta == -120:
            direction = 1
        if event.num == 4 or event.delta == 120:
            direction = -1
        event.widget.yview_scroll(direction, UNITS)

    canvas.bind('<MouseWheel>', lambda event: rollWheel(event))
    canvas.bind('<Button-4>', lambda event: rollWheel(event))
    canvas.bind('<Button-5>', lambda event: rollWheel(event))


plottedFigures = [] #To store matplotlib figures

def addPlottedFig(figure):
    '''
    Matplotlib figures to be shown in the GUI
    '''
    plottedFigures.append(figure)


def getCanvas(masterWidget, figure=None):
    '''
    Returns canvas of plot and canvas of tool bar
    '''
    if(figure==None):
        f = getHistoGramPlot() #For testing
        canvas = FigureCanvasTkAgg(f, master=masterWidget)
    else:
        canvas = FigureCanvasTkAgg(figure, master=masterWidget)

    canvas.show()
    toolbar = NavigationToolbar2TkAgg( canvas, masterWidget )
    toolbar.update()

    #return the plot and the toolbar
    return canvas.get_tk_widget(), canvas._tkcanvas

def initiate(FigureList=None):
    '''
    Start the GUI Application
    '''
    root = Tk()
    app = App(root, FigureList)
    root.mainloop()


def main():
    '''
    Shows 2 histograms plots in a tkinter GUI
    '''
    fig1 = getHistoGramPlot()
    fig2 = getHistoGramPlot()

    figList = [fig1, fig2]

    initiate(figList)

def getHistoGramPlot():
    '''
    Return a figure of a histogram
    '''
    mu, sigma = 100, 15
    x = mu + sigma * np.random.randn(10000)
    fig = plt.figure(figsize=(12, 6), dpi=100)
    ax = fig.add_subplot(111)
    n, bins, patches = ax.hist(x, 50, normed=1, facecolor='green', alpha=0.6)
    bincenters = 0.5*(bins[1:]+bins[:-1])
    mu = np.median(x)
    sigma = np.std(x)
    y = mlab.normpdf( bincenters, mu, sigma)
    l = ax.plot(bincenters, y, 'r--', linewidth=1)
    ax.set_xlabel('Values')
    ax.set_ylabel('Probability')
    xlimTop = max(x)
    xlimBottom = min(x)
    ax.set_xlim(xlimBottom, xlimTop)
    ax.grid(True)

    return fig

if __name__ == '__main__':
    main()
4

2 に答える 2

3

迅速で汚い解決策の場合:

  • メインキャンバスをAppクラスの属性にします
  • rollWheelをAppクラスのメソッドにします
  • キャンバスではなくウィンドウにマウスホイールバインディングを作成します。これにより、ウィンドウ内のすべてのウィジェットのマウスホイールイベントがキャプチャされます。
  • マウスホイールイベントでキャンバスをスクロールします

from Tkinter import *

import matplotlib
#matplotlib.use('TkAgg')

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
import os
import matplotlib.mlab as mlab

class App:

    def __init__(self, master, figureList=[]):
        frame = Frame(master, bg='#00BFFF', borderwidth=1, relief=RAISED)
        frame.pack(expand=YES, fill=BOTH)

        master.title("Listed Plots")
        #Fullscreen for windows
        if os.name == 'nt':
            master.wm_state('zoomed')

        #Create a Canvas
        self.canvas = canvas = Canvas(frame,bg='#00BFFF', relief=SUNKEN)       
        canvas.config(highlightthickness=0) 

        #Create a Scrollbar Horisontal
        hbar=Scrollbar(frame,orient=HORIZONTAL)
        hbar.pack(side=BOTTOM,fill=X)
        hbar.config(command=canvas.xview)

        #Create a Scrollbar Vertical
        vbar=Scrollbar(frame,orient=VERTICAL)
        vbar.pack(side=RIGHT,fill=Y)
        vbar.config(command=canvas.yview)

        canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
        canvas.pack(side=TOP,expand=True,fill=BOTH)

        #Create a Frame in the canvas
        canvFrame = Frame(canvas, bg='#00BFFF')
        canvFrame.pack()

        Label(canvFrame, text="Matplots of data", bg='#B4EEB4').pack(side=TOP,fill=X)

        plotNbr = 1
        windowHeight = 10

        for fig in figureList:
            Label(canvFrame, text="#Plotnumber: " + str(plotNbr), bg='#FF7F24').pack(side=TOP,fill=X)

            frm = Frame(canvFrame, bg='#9FB6CD')
            frm.pack(side=TOP, fill=X)

            self.canvasMPL2, self.canvasMPLToolBar2 = getCanvas(frm, fig)
            self.canvasMPL2.pack(side=TOP)
            self.canvasMPLToolBar2.pack(side=TOP)

            #Create space for the plot and tool bar.
            windowHeight = windowHeight + 680
            plotNbr = plotNbr + 1


        canvas.config(width=1200,height= windowHeight)
        canvas.config(scrollregion=(0,0,1200, windowHeight))

        canvas.create_window(0,0, anchor = NW, window = canvFrame, width = 1200, height = windowHeight)

        canvas.focus_set() #Doesnt work with FigureCanvasTkAgg, this steals the focus 
        #canvas.focus_force()
        #canvas.grab_set_global()

        #scrollwheel settings
        canvas.configure(yscrollincrement='25')

##        def rollWheel(event):
##            #print "Mousescroll"
##            direction = 0
##            if event.num == 5 or event.delta == -120:
##                direction = 1
##            if event.num == 4 or event.delta == 120:
##                direction = -1
##            event.widget.yview_scroll(direction, UNITS)

        master.bind('<MouseWheel>', lambda event,s=self: self.rollWheel(event))
        canvas.bind('<Button-4>', lambda event: rollWheel(event))
        canvas.bind('<Button-5>', lambda event: rollWheel(event))

    def rollWheel(self, event):
        #print "Mousescroll"
        direction = 0
        if event.num == 5 or event.delta == -120:
            direction = 1
        if event.num == 4 or event.delta == 120:
            direction = -1
##        event.widget.yview_scroll(direction, UNITS)
        self.canvas.yview_scroll(direction, UNITS)    

plottedFigures = [] #To store matplotlib figures

def addPlottedFig(figure):
    '''
    Matplotlib figures to be shown in the GUI
    '''
    plottedFigures.append(figure)


def getCanvas(masterWidget, figure=None):
    '''
    Returns canvas of plot and canvas of tool bar
    '''
    if(figure==None):
        f = getHistoGramPlot() #For testing
        canvas = FigureCanvasTkAgg(f, master=masterWidget)
    else:
        canvas = FigureCanvasTkAgg(figure, master=masterWidget)

    canvas.show()
    toolbar = NavigationToolbar2TkAgg( canvas, masterWidget )
    toolbar.update()

    #return the plot and the toolbar
    return canvas.get_tk_widget(), canvas._tkcanvas

def initiate(FigureList=None):
    '''
    Start the GUI Application
    '''
    root = Tk()
    app = App(root, FigureList)
    root.mainloop()


def main():
    '''
    Shows 2 histograms plots in a tkinter GUI
    '''
    fig1 = getHistoGramPlot()
    fig2 = getHistoGramPlot()

    figList = [fig1, fig2]

    initiate(figList)

def getHistoGramPlot():
    '''
    Return a figure of a histogram
    '''
    mu, sigma = 100, 15
    x = mu + sigma * np.random.randn(10000)
    fig = plt.figure(figsize=(12, 6), dpi=100)
    ax = fig.add_subplot(111)
    n, bins, patches = ax.hist(x, 50, normed=1, facecolor='green', alpha=0.6)
    bincenters = 0.5*(bins[1:]+bins[:-1])
    mu = np.median(x)
    sigma = np.std(x)
    y = mlab.normpdf( bincenters, mu, sigma)
    l = ax.plot(bincenters, y, 'r--', linewidth=1)
    ax.set_xlabel('Values')
    ax.set_ylabel('Probability')
    xlimTop = max(x)
    xlimBottom = min(x)
    ax.set_xlim(xlimBottom, xlimTop)
    ax.grid(True)

    return fig

if __name__ == '__main__':
    main()
于 2012-11-06T22:41:44.857 に答える
1

コードのインデントclass App:が不適切にインデントされました。rollWheelそれは内で定義された関数だと思います__init__

この変更を行います:

    def rollWheel(event):
        #print "Mousescroll"
        direction = 0
        if event.num == 5 or event.delta == -120:
            direction = 1
        if event.num == 4 or event.delta == 120:
            direction = -1
        # Tell the canvas to scroll directly
        canvas.yview_scroll(direction, UNITS)  

    canvas.bind_all('<MouseWheel>', lambda event: rollWheel(event))
    canvas.bind_all('<Button-4>', lambda event: rollWheel(event))
    canvas.bind_all('<Button-5>', lambda event: rollWheel(event))

.bind_all一般的なtkinterバインディングの詳細については、 effbotのチュートリアルをご覧ください。

于 2012-11-08T03:54:50.470 に答える