Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
basic threaded gui for simulation program
from PyQt4 import QtCore
class BaseThread(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
self.exiting = False
def __del__(self):
self.exiting = True
self.wait()
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
matplotlib.rcParams.update({'font.size': 8})
class Canvas(FigureCanvas):
def __init__(self,parent,dpi=100.0):
size = parent.size()
self.dpi = dpi
self.width = size.width() / dpi
self.height = size.height() / dpi
self.figure = Figure(figsize=(self.width, self.height), dpi=self.dpi, facecolor='w', edgecolor='k', frameon=False)
self.figure.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=0.95, wspace=None, hspace=None)
self.axes = self.figure.add_subplot(111)
self.axes.axis((-1,1,-1,1))
FigureCanvas.__init__(self, self.figure)
self.updateGeometry()
self.draw()
self.cc = self.copy_from_bbox(self.axes.bbox)
self.particle_plot = None
self.setParent(parent)
self.blit(self.axes.bbox)
def on_pre_draw(self):
self.restore_region(self.cc)
def on_draw(self):
raise NotImplementedError
def on_post_draw(self):
self.blit(self.axes.bbox)
def redraw(self):
self.on_pre_draw()
self.on_draw()
self.on_post_draw()
from time import sleep
from PyQt4 import QtCore
from basethread import BaseThread
class DrawThread(BaseThread):
def __init__(self):
BaseThread.__init__(self)
def run(self):
while not self.exiting:
self.emit( QtCore.SIGNAL('DrawCanvas()') )
sleep(0.05)
#!/usr/bin/env python
import sys
from PyQt4.QtGui import QApplication
from simulator import MainForm
def main():
app = QApplication(sys.argv)
form = MainForm()
form.show()
app.exec_()
if __name__ == "__main__":
main()
simulator_ui.py: simulator.ui
pyuic4 simulator.ui -o simulator_ui.py
clean:
rm -f *.pyc *~
from numpy import array, where, abs
from numpy.random import random
from canvas import Canvas
class ParticleCanvas(Canvas):
def __init__(self,parent,dpi=100.0):
Canvas.__init__(self,parent,dpi)
self.pos = None
self.vel = None
def reinitialize(self, npart):
#self.pos = 2.0*(random((2,npart))-0.5)
self.pos = (random((2,npart)))
self.vel = 2.0*(random((2,npart))-0.5)
self.particle_plot = None
def move(self):
if self.pos is None or self.vel is None: return
dt = 0.01
# change velocity if they go beyond box
self.vel[where(abs(self.pos + dt*self.vel)>1.0)] *= -1.0
self.pos += dt*self.vel
def on_draw(self):
if self.pos is None or self.vel is None: return
if self.particle_plot is None:
self.particle_plot, = self.axes.plot(self.pos[0],self.pos[1], 'ro', animated=True)
self.particle_plot.set_xdata(self.pos[0])
self.particle_plot.set_ydata(self.pos[1])
self.axes.draw_artist(self.particle_plot)
from PyQt4 import QtGui, QtCore
from simulator_ui import Ui_main
from particlecanvas import ParticleCanvas
from drawthread import DrawThread
from stepthread import StepThread
class MainForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self,parent)
self.ui = Ui_main()
self.ui.setupUi(self)
self.connect(self.ui.btnSetup, QtCore.SIGNAL("clicked()"), self.on_setup_clicked)
self.connect(self.ui.btnRun, QtCore.SIGNAL("clicked()"), self.on_run_clicked)
self.c = ParticleCanvas(self.ui.frame)
self.pp = DrawThread()
self.mm = StepThread()
self.connect( self.pp, QtCore.SIGNAL('DrawCanvas()'), self.c.redraw)
self.connect( self.mm, QtCore.SIGNAL('TakeStep()'), self.c.move)
def on_setup_clicked(self):
self.c.reinitialize(int(self.ui.txtNPart.text()))
self.mm.exiting = True
self.pp.start()
def on_run_clicked(self):
self.mm.simulate(int(self.ui.txtNSteps.text()))
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>main</class>
<widget class="QWidget" name="main">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>412</width>
<height>270</height>
</rect>
</property>
<property name="windowTitle">
<string>Simulator</string>
</property>
<widget class="QLineEdit" name="txtNPart">
<property name="geometry">
<rect>
<x>270</x>
<y>30</y>
<width>131</width>
<height>27</height>
</rect>
</property>
<property name="inputMask">
<string notr="true"/>
</property>
<property name="text">
<string notr="true">100</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>270</x>
<y>10</y>
<width>141</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string notr="true">Number of Particles</string>
</property>
</widget>
<widget class="QFrame" name="frame">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>250</width>
<height>250</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
<widget class="QPushButton" name="btnSetup">
<property name="geometry">
<rect>
<x>340</x>
<y>60</y>
<width>61</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string notr="true">Setup</string>
</property>
</widget>
<widget class="QPushButton" name="btnRun">
<property name="geometry">
<rect>
<x>340</x>
<y>230</y>
<width>61</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string notr="true">Run</string>
</property>
</widget>
<widget class="QLineEdit" name="txtNSteps">
<property name="geometry">
<rect>
<x>270</x>
<y>200</y>
<width>131</width>
<height>27</height>
</rect>
</property>
<property name="inputMask">
<string notr="true"/>
</property>
<property name="text">
<string notr="true">1000</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>270</x>
<y>180</y>
<width>131</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string notr="true">Number of Steps</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
from time import sleep
from PyQt4 import QtCore
from basethread import BaseThread
class StepThread(BaseThread):
def __init__(self):
BaseThread.__init__(self)
def simulate(self, nsteps):
self.nsteps = nsteps
self.exiting = False
self.start()
def run(self):
while not self.exiting and self.nsteps > 0:
self.emit( QtCore.SIGNAL('TakeStep()') )
self.nsteps -= 1
sleep(0.005)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment