The personal website of Scott W Harden
June 28th, 2010

Getting GTK, Glade, and Python to Work in Windows

These screenshots show me running the Py2EXE-compiled script I wrote last weekend on a Windows 7 machine. Additionally there is a screenshot of the "Add/Remove Programs" window demonstrating which versions of which libraries were required.

Markdown source code last modified on January 18th, 2021
---
title: Getting GTK, Glade, and Python to Work in Windows
date: 2010-06-28 09:46:26
tags: python, old
---

# Getting GTK, Glade, and Python to Work in Windows

__These screenshots__ show me running the Py2EXE-compiled script I wrote last weekend on a Windows 7 machine. Additionally there is a screenshot of the "Add/Remove Programs" window demonstrating which versions of which libraries were required.

<div class="text-center img-border">

[![](glade_exe_thumb.jpg)](glade_exe.png)

[![](needToInstall_thumb.jpg)](needToInstall.png)

</div>

June 27th, 2010

Python Script with GTK GUI Compiled with Py2EXE

This is a total hack, but it works. I spent all night jumping through hoops to get this thing to run on Windows. The problem is that I designed my previous UI in a version of GLADE which is newer than that supported by Windows. It looks like it's not backward-compatible, so I have to re-design the GUI from scratch using an earlier version of GLADE. I'll probably stick to GTK version 2.12 and Python version 2.6 because they play nicely on Windows. It's a quick and dirty script, but I was able to make the following run on Windows as a single EXE file!

Scott from the future (10 years later): Where's the source code for this? Weird post.

Markdown source code last modified on January 18th, 2021
---
title: Python Script with GTK GUI Compiled with Py2EXE
date: 2010-06-27 23:16:05
---

# Python Script with GTK GUI Compiled with Py2EXE

__This is a total hack, but it works.__ I spent all night jumping through hoops to get this thing to run on Windows. The problem is that I designed my previous UI in a version of GLADE which is newer than that supported by Windows. It looks like it's not backward-compatible, so I have to re-design the GUI from scratch using an earlier version of GLADE. I'll probably stick to GTK version 2.12 and Python version 2.6 because they play nicely on Windows. It's a quick and dirty script, but I was able to make the following run on Windows as a single EXE file!

<div class="text-center">

[![](glade_windows_python_thumb.jpg)](glade_windows_python.png)

</div>

> Scott from the future (10 years later): Where's the source code for this? Weird post.
June 26th, 2010

Spectrograph UI Made with Glade

While continuing to investigate my options for the new version of QRSS VD, I re-visited Glade, the GTK GUI designer. In short, it lets you draw widgets (combo boxes, scrollbars, labels, images, buttons, etc) onto windows and then makes it easy to add code to the GUI. I *hated* the old QRSS VD development because of the ridiculously large amount of time I had to spend coding the UI. Hopefully by migrating from TKinter to GTK - while it opens a whole new can of worms - will let me add functionality rapidly without hesitation.

Here's a quick screenshot of my running this new version of the software with a GUI I made in less than an hour. The bars for brightness and contrast can be adjusted which modify the spectrograph in real time. The audio is whatever is playing in Pandora. I like the "fantastic plastic machine" radio station!

Markdown source code last modified on January 18th, 2021
---
title: Spectrograph UI Made with Glade
date: 2010-06-26 19:03:05
---

# Spectrograph UI Made with Glade

__While continuing to investigate__ my options for the new version of QRSS VD, I re-visited Glade, the GTK GUI designer. In short, it lets you draw widgets (combo boxes, scrollbars, labels, images, buttons, etc) onto windows and then makes it easy to add code to the GUI. I \*hated\* the old QRSS VD development because of the ridiculously large amount of time I had to spend coding the UI. Hopefully by migrating from TKinter to GTK - while it opens a whole new can of worms - will let me add functionality rapidly without hesitation.

<div class="text-center img-border">

[![](glade_python_improving_thumb.jpg)](glade_python_improving.png)

</div>

__Here's a quick screenshot__ of my running this new version of the software with a GUI I made in less than an hour. The bars for brightness and contrast can be adjusted which modify the spectrograph in real time. The audio is whatever is playing in Pandora. I like the "fantastic plastic machine" radio station!

June 24th, 2010

Fast TK Pixelmap generation from 2D Numpy Arrays in Python

I had TKinter all wrong! While my initial tests with PyGame's rapid ability to render Numpy arrays in the form of pixel maps proved impressive, it was only because I was comparing it to poor TK code. I don't know what I was doing wrong, but when I decided to give TKinter one more shot I was blown away -- it's as smooth or smoother as PyGame. Forget PyGame! I'm rendering everything in raw TK from now on. This utilizes the Python Imaging Library (PIL) so it's EXTREMELY flexible (supports fancy operations, alpha channels, etc).

The screenshot shows me running the script (below) generating random noise and "scrolling" it horizontally (like my spectrograph software does) at a fast rate smoothly (almost 90 FPS!). Basically, it launches a window, creates a canvas widget (which I'm told is faster to update than a label and reduces flickering that's often associated with rapid redraws because it uses double-buffering). Also, it uses threading to handle the calculations/redraws without lagging the GUI. The code speaks for itself.

import Tkinter
from PIL import Image, ImageTk
import numpy
import time


class mainWindow():
    times = 1
    timestart = time.clock()
    data = numpy.array(numpy.random.random((400, 500))*100, dtype=int)

    def __init__(self):
        self.root = Tkinter.Tk()
        self.frame = Tkinter.Frame(self.root, width=500, height=400)
        self.frame.pack()
        self.canvas = Tkinter.Canvas(self.frame, width=500, height=400)
        self.canvas.place(x=-2, y=-2)
        self.root.after(0, self.start)  # INCREASE THE 0 TO SLOW IT DOWN
        self.root.mainloop()

    def start(self):
        global data
        self.im = Image.fromstring('L', (self.data.shape[1],
                                         self.data.shape[0]), self.data.astype('b').tostring())
        self.photo = ImageTk.PhotoImage(image=self.im)
        self.canvas.create_image(0, 0, image=self.photo, anchor=Tkinter.NW)
        self.root.update()
        self.times += 1
        if self.times % 33 == 0:
            print "%.02f FPS" % (self.times/(time.clock()-self.timestart))
        self.root.after(10, self.start)
        self.data = numpy.roll(self.data, -1, 1)


if __name__ == '__main__':
    x = mainWindow()
Markdown source code last modified on January 18th, 2021
---
title: Fast TK Pixelmap generation from 2D Numpy Arrays in Python
date: 2010-06-24 21:29:29
tags: python, old
---

# Fast TK Pixelmap generation from 2D Numpy Arrays in Python

__I had TKinter all wrong!__ While my initial tests with PyGame's rapid ability to render Numpy arrays in the form of pixel maps proved impressive, it was only because I was comparing it to poor TK code. I don't know what I was doing wrong, but when I decided to give TKinter one more shot I was blown away -- it's as smooth or smoother as PyGame. Forget PyGame! I'm rendering everything in raw TK from now on. This utilizes the Python Imaging Library (PIL) so it's EXTREMELY flexible (supports fancy operations, alpha channels, etc).

<div class="text-center img-border">

[![](glade_python_improving_thumb.jpg)](glade_python_improving.png)

</div>

__The screenshot shows__ me running the script (below) generating random noise and "scrolling" it horizontally (like my spectrograph software does) at a fast rate smoothly (almost 90 FPS!). Basically, it launches a window, creates a canvas widget (which I'm told is faster to update than a label and reduces flickering that's often associated with rapid redraws because it uses double-buffering). Also, it uses threading to handle the calculations/redraws without lagging the GUI. The code speaks for itself.

```python
import Tkinter
from PIL import Image, ImageTk
import numpy
import time


class mainWindow():
    times = 1
    timestart = time.clock()
    data = numpy.array(numpy.random.random((400, 500))*100, dtype=int)

    def __init__(self):
        self.root = Tkinter.Tk()
        self.frame = Tkinter.Frame(self.root, width=500, height=400)
        self.frame.pack()
        self.canvas = Tkinter.Canvas(self.frame, width=500, height=400)
        self.canvas.place(x=-2, y=-2)
        self.root.after(0, self.start)  # INCREASE THE 0 TO SLOW IT DOWN
        self.root.mainloop()

    def start(self):
        global data
        self.im = Image.fromstring('L', (self.data.shape[1],
                                         self.data.shape[0]), self.data.astype('b').tostring())
        self.photo = ImageTk.PhotoImage(image=self.im)
        self.canvas.create_image(0, 0, image=self.photo, anchor=Tkinter.NW)
        self.root.update()
        self.times += 1
        if self.times % 33 == 0:
            print "%.02f FPS" % (self.times/(time.clock()-self.timestart))
        self.root.after(10, self.start)
        self.data = numpy.roll(self.data, -1, 1)


if __name__ == '__main__':
    x = mainWindow()
```

June 24th, 2010

Detrending Data in Python with Numpy

⚠️ SEE UPDATED POST: Signal Filtering in Python

While continuing my quest into the world of linear data analysis and signal processing, I came to a point where I wanted to emphasize variations in FFT traces. While I am keeping my original data for scientific reference, visually I want to represent it emphasizing variations rather than concentrating on trends. I wrote a detrending function which I'm sure will be useful for many applications:

def detrend(data,degree=10):
    detrended=[None]*degree
    for i in range(degree,len(data)-degree):
        chunk=data[i-degree:i+degree]
        chunk=sum(chunk)/len(chunk)
        detrended.append(data[i]-chunk)
    return detrended+[None]*degree

However, this method is extremely slow. I need to think of a way to accomplish this same thing much faster. [ponders]

UPDATE: It looks like I've once again re-invented the wheel. All of this has been done already, and FAR more efficiently I might add. For more see scipy.signal.detrend.html

import scipy.signal
ffty=scipy.signal.detrend(ffty)
Markdown source code last modified on January 18th, 2021
---
title: Detrending Data in Python with Numpy
date: 2010-06-24 08:38:52
tags: python, old
---

# Detrending Data in Python with Numpy

> **⚠️ SEE UPDATED POST:** [**Signal Filtering in Python**](https://swharden.com/blog/2020-09-23-signal-filtering-in-python/)

__While continuing my quest__ into the world of linear data analysis and signal processing, I came to a point where I wanted to emphasize variations in FFT traces. While I am keeping my original data for scientific reference, visually I want to represent it emphasizing variations rather than concentrating on trends. I wrote a detrending function which I'm sure will be useful for many applications:

```python
def detrend(data,degree=10):
	detrended=[None]*degree
	for i in range(degree,len(data)-degree):
		chunk=data[i-degree:i+degree]
		chunk=sum(chunk)/len(chunk)
		detrended.append(data[i]-chunk)
	return detrended+[None]*degree
```

<div class="text-center">

[![](detrend_fft_thumb.jpg)](detrend_fft.png)

</div>

However, this method is extremely slow. I need to think of a way to accomplish this same thing much faster. \[ponders\]

__UPDATE:__ It looks like I've once again re-invented the wheel. All of this has been done already, and FAR more efficiently I might add. For more see [scipy.signal.detrend.html](https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.detrend.html)

```python
import scipy.signal
ffty=scipy.signal.detrend(ffty)
```

Pages