I am trying to port some scripts behind the GUI (using Tkinter) and still did this until all open data is displayed on the Tkinter canvas (using matplotlib to draw it).
The only problem I am facing is that the standard scaling / scrolling that are in matplotlib (using the left mouse button to “move” the graph and the right mouse button to “zoom”) are not available on the canvas, basically the functionality "4 pointed cross" in the matplotlib chart window.
I think this will require the creation of my own handlers, but I would suggest that there should be a way to use the default matplotlib handlers? I also looked at the options for “scrolling” the canvas, as mentioned in this question, but it only seems to change the size of the chart area, and not zoom in / out on the data, nor do I want to add additional buttons to manipulate the chart area.
The bare minimum code I have is:
from Tkinter import *
import matplotlib.pyplot as plt
import matplotlib.backends.backend_tkagg as tkagg
import tkFileDialog
class App():
def __init__(self,master):
self.inputFile = ""
self.fig = plt.Figure()
self.canvas = tkagg.FigureCanvasTkAgg(self.fig, master = master)
self.canvas.get_tk_widget().pack()
self.canvas.draw()
frame = Frame(master)
master.title("MassyTools 0.1.1 (Alpha)")
menu = Menu(root)
root.config(menu = menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Open Input File", command = self.openFile)
calibmenu = Menu(menu)
menu.add_cascade(label="Calibrate",menu=calibmenu)
calibmenu.add_command(label="Open Calibration File", command = self.openCalibrationFile)
calibmenu.add_command(label="Calibrate", command = self.calibrateData)
def openFile(self):
file_path = tkFileDialog.askopenfilename()
setattr(self,'inputFile',file_path)
data = self.readData()
self.plotData(data)
def openCalibrationFile(self):
print "Place holder for selection of the calibration file"
def calibrateData(self):
print "Place holder for actual calibration"
def readData(self):
x_array = []
y_array = []
with open(self.inputFile,'r') as fr:
for line in fr:
line = line.rstrip('\n')
values = line.split()
x_array.append(float(values[0]))
y_array.append(float(values[1]))
return zip(x_array,y_array)
def plotData(self,data):
x_array = []
y_array = []
for i in data:
x_array.append(i[0])
y_array.append(i[1])
self.fig.clear()
self.axes = self.fig.add_subplot(111)
self.line, = self.axes.plot(x_array,y_array)
self.canvas.draw()
"""def openFile(self,number):
name = tkFileDialog.askopenfilename()
ops = {
1: 'deglycoData',
2: 'peptideFile',
3: 'mzML'
}
setattr(self,ops[number],name)
"""
root = Tk()
app = App(root)
root.mainloop()
source
share