[PyQt] Create a start Button
Fabien Lafont
lafont.fabien at gmail.com
Wed Dec 21 14:36:57 GMT 2011
Hello everyone,
I'm trying to create a Start button on my soft. My software just plot
live datas. When I run the program it display only a pristine graph
and I want to start the first action of my program when I click on
start. But If I create this Start button nothing appears...
Why just creating this button block the display of my soft?
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 19 14:51:39 2011
@author: lafont
"""
#!/usr/bin/env python
from visa import *
from pylab import *
import sys
from PyQt4 import QtGui
import numpy as np
import random
import ImageGrab
from PyQt4 import QtCore, QtGui
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg
as NavigationToolbar
from PyQt4.QtGui import (QApplication, QLabel, QLineEdit, QSpinBox,
QVBoxLayout, QDial, QGridLayout, QComboBox, QPushButton)
from PyQt4.QtCore import (QObject, Qt, SIGNAL, SLOT)
#===============================================================================
#
#===============================================================================
class CPUMonitor(FigureCanvas):
def __init__(self,parent):
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
FigureCanvas.__init__(self, self.fig)
self.ax.set_autoscale_on(True)
# generates first "empty" plots
self.user, self.nice = [], []
self.l_user, = self.ax.plot([], self.user, label='Voltage')
self.l_nice, = self.ax.plot([], self.nice, label='Voltage2')
# force a redraw of the Figure
self.fig.canvas.draw()
# FigureCanvas.updateGeometry(self)
def action1(self):
self.result1 = random.randint(0,4)
QtCore.QTimer.singleShot(100, self.action2)
def action2(self):
self.result2 = random.randint(0,1)
self.user.append(self.result1)
self.nice.append(self.result2)
self.l_user.set_data(range(len(self.user)), self.user)
self.l_nice.set_data(range(len(self.nice)), self.nice)
# force a redraw of the Figure
self.fig.canvas.draw()
FigureCanvas.updateGeometry(self)
#envoie de l'évènement ici
self.action1()
#
#===============================================================================
#
#===============================================================================
class ApplicationWindow(QtGui.QMainWindow):
"""Example main window"""
def __init__(self):
# initialization of Qt MainWindow widget
QtGui.QMainWindow.__init__(self)
# set window title
self.setWindowTitle("QHE manip")
# instantiate a widget, it will be the main one
self.main_widget = QtGui.QWidget(self)
# create a vertical box layout widget
vbl = QtGui.QGridLayout(self.main_widget)
StartButton = QPushButton("START")
self.connect(StartButton, SIGNAL('released()'),
CPUMonitor.action1)
# instantiate our Matplotlib canvas widget
qmc = CPUMonitor(self.main_widget)
# instantiate the navigation toolbar
ntb = NavigationToolbar(qmc, self.main_widget)
# pack these widget into the vertical box
vbl.addWidget(qmc,0,0)
vbl.addWidget(ntb,1,0)
vbl.addWidget(StartButton,4,0)
# set the focus on the main widget
self.main_widget.setFocus()
# set the central widget of MainWindow to main_widget
self.setCentralWidget(self.main_widget)
# def arret(self):
# stop = False
# print stop
# def commence(self):
# stop = True
# create the GUI application
qApp = QtGui.QApplication(sys.argv)
# instantiate the ApplicationWindow widget
aw = ApplicationWindow()
# show the widget
aw.show()
# start the Qt main loop execution, exiting from this script
# with the same return code of Qt application
sys.exit(qApp.exec_())
More information about the PyQt
mailing list