Skip to content

Commit 416d1fb

Browse files
committed
Fix: make examples/tfvis.py Python 3 compatible.
Uses Python 3 tkinter module name; the future package lets this work in Python 2 as well.
1 parent 0d13a80 commit 416d1fb

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ script:
7979
- if [[ "$SLYCOT" != "" ]]; then
8080
export PYTHONPATH=$PWD
8181
# pmw needed for examples/tfvis.py
82-
conda install -c conda-forge pmw
82+
# future is needed for Python 2, also for examples/tfvis.py
83+
conda install -c conda-forge pmw future
8384
'(cd examples && bash run_examples.sh)'
8485
fi
8586

examples/tfvis.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/usr/bin/python
2+
# needs pmw (in pypi, conda-forge)
3+
# For Python 2, needs future (in conda pypi and "default")
4+
25
from __future__ import print_function
36

47
""" Simple GUI application for visualizing how the poles/zeros of the transfer
@@ -40,7 +43,7 @@
4043
"""
4144

4245
import control.matlab
43-
import Tkinter
46+
import tkinter
4447
import sys
4548
import Pmw
4649
import matplotlib.pyplot as plt
@@ -103,7 +106,7 @@ def __init__(self, parent):
103106
pass
104107

105108
widgets = (self.numerator_widget, self.denominator_widget)
106-
for i in xrange(len(widgets)):
109+
for i in range(len(widgets)):
107110
widgets[i].grid(row=i+1, column=0, padx=20, pady=3)
108111
Pmw.alignlabels(widgets)
109112

@@ -158,24 +161,24 @@ def __init__(self, parent):
158161
self.zeros = []
159162
self.poles = []
160163

161-
self.topframe = Tkinter.Frame(self.master)
164+
self.topframe = tkinter.Frame(self.master)
162165
self.topframe.pack(expand=True, fill='both')
163166

164-
self.entries = Tkinter.Frame(self.topframe)
167+
self.entries = tkinter.Frame(self.topframe)
165168
self.entries.pack(expand=True, fill='both')
166169

167-
self.figure = Tkinter.Frame(self.topframe)
170+
self.figure = tkinter.Frame(self.topframe)
168171
self.figure.pack(expand=True, fill='both')
169172

170-
header = Tkinter.Label(self.entries,
173+
header = tkinter.Label(self.entries,
171174
text='Define the transfer function:')
172175
header.grid(row=0, column=0, padx=20, pady=7)
173176

174177

175178
self.tfi = TFInput(self.entries)
176179
self.sys = self.tfi.get_tf()
177180

178-
Tkinter.Button(self.entries, text='Apply', command=self.apply,
181+
tkinter.Button(self.entries, text='Apply', command=self.apply,
179182
width=9).grid(row=0, column=1, rowspan=3, padx=10, pady=5)
180183

181184
self.f_bode = plt.figure(figsize=(4, 4))
@@ -185,25 +188,25 @@ def __init__(self, parent):
185188

186189
self.canvas_pzmap = FigureCanvasTkAgg(self.f_pzmap,
187190
master=self.figure)
188-
self.canvas_pzmap.show()
191+
self.canvas_pzmap.draw()
189192
self.canvas_pzmap.get_tk_widget().grid(row=0, column=0,
190193
padx=0, pady=0)
191194

192195
self.canvas_bode = FigureCanvasTkAgg(self.f_bode,
193196
master=self.figure)
194-
self.canvas_bode.show()
197+
self.canvas_bode.draw()
195198
self.canvas_bode.get_tk_widget().grid(row=0, column=1,
196199
padx=0, pady=0)
197200

198201
self.canvas_step = FigureCanvasTkAgg(self.f_step,
199202
master=self.figure)
200-
self.canvas_step.show()
203+
self.canvas_step.draw()
201204
self.canvas_step.get_tk_widget().grid(row=1, column=0,
202205
padx=0, pady=0)
203206

204207
self.canvas_nyquist = FigureCanvasTkAgg(self.f_nyquist,
205208
master=self.figure)
206-
self.canvas_nyquist.show()
209+
self.canvas_nyquist.draw()
207210
self.canvas_nyquist.get_tk_widget().grid(row=1, column=1,
208211
padx=0, pady=0)
209212

@@ -302,7 +305,7 @@ def mouse_move(self, event):
302305
tfcn = self.tfi.get_tf()
303306
if (tfcn != None):
304307
self.draw_pz(tfcn)
305-
self.canvas_pzmap.show()
308+
self.canvas_pzmap.draw()
306309

307310
def apply(self):
308311
"""Evaluates the transfer function and produces different plots for
@@ -353,10 +356,10 @@ def redraw(self):
353356
print("Error plotting step response")
354357
plt.suptitle('Step Response')
355358

356-
self.canvas_pzmap.show()
357-
self.canvas_bode.show()
358-
self.canvas_step.show()
359-
self.canvas_nyquist.show()
359+
self.canvas_pzmap.draw()
360+
self.canvas_bode.draw()
361+
self.canvas_step.draw()
362+
self.canvas_nyquist.draw()
360363

361364
def create_analysis():
362365
""" Create main object """
@@ -366,7 +369,7 @@ def handler():
366369
sys.exit()
367370

368371
# Launch a GUI for the Analysis module
369-
root = Tkinter.Tk()
372+
root = tkinter.Tk()
370373
root.protocol("WM_DELETE_WINDOW", handler)
371374
Pmw.initialise(root)
372375
root.title('Analysis of Linear Systems')

0 commit comments

Comments
 (0)