# coding=utf-8 # -*- coding: utf-8 -*- from __future__ import unicode_literals import sys import os import random import matplotlib # Make sure that we are using QT5 matplotlib.use('Qt5Agg') from PyQt5 import QtCore, QtWidgets from PyQt5.QtWidgets import * from numpy import arange, sin, pi from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.figure import Figure progname = os.path.basename(sys.argv[0]) progversion = "0.1" class MyMplCanvas(FigureCanvas): """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.).""" def __init__(self, parent=None, width=20, height=10, dpi=100): fig = Figure(figsize=(width, height), dpi=dpi) self.axes = fig.add_subplot(111) self.axes.autoscale(False) self.axes.axis([0, 100, 0, 20]) # self.axes.set_xlim(0, 100) # self.axes.set_ylim(0, 20) self.axes.grid(True) self.compute_initial_figure() FigureCanvas.__init__(self, fig) self.setParent(parent) FigureCanvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) FigureCanvas.updateGeometry(self) def compute_initial_figure(self): self.axes.scatter([0, 1, 2, 3], [1, 2, 0, 4],color='red') class ApplicationWindow(QtWidgets.QMainWindow): def __init__(self): QtWidgets.QMainWindow.__init__(self) self.setAttribute(QtCore.Qt.WA_DeleteOnClose) self.setWindowTitle("application main window") self.main_widget = QtWidgets.QWidget(self) vbox = QtWidgets.QVBoxLayout(self.main_widget) h0box= QtWidgets.QHBoxLayout(self.main_widget) self.lable_x=QLabel("切面:") self.lay_x=QLineEdit() h0box.addWidget(self.lable_x) h0box.addWidget(self.lay_x) vbox.addLayout(h0box) self.canvas = MyMplCanvas( self.main_widget,width=20, height=50, dpi=100) ###attention### vbox.addWidget(self.canvas) hbox = QtWidgets.QHBoxLayout(self.main_widget) self.start_button = QPushButton("start", self) self.stop_button = QPushButton("stop", self) self.exit_button = QPushButton("exit", self) self.start_button.clicked.connect(self.on_start) self.stop_button.clicked.connect(self.on_stop) self.exit_button.clicked.connect(self.on_exit) hbox.addWidget(self.start_button) hbox.addWidget(self.stop_button) hbox.addWidget(self.exit_button) vbox.addLayout(hbox) self.setLayout(vbox) self.main_widget.setFocus() self.setCentralWidget(self.main_widget) self.distance=0 self.timer = QtCore.QTimer(self) self.timer.timeout.connect(self.update_figure) # self.timer.start(200) def on_start(self): #self.distance=50-int(self.lay_x.text()) self.timer.start(200) def update_figure(self): # Build a list of 4 random integers between 0 and 10 (both inclusive) l = [random.randint(0, 10) for i in range(4)] self.canvas.axes.cla() # clear all self.canvas.axes.autoscale(False) #refix coordinates limits self.canvas.axes.grid(True) #refix grid self.canvas.axes.scatter([0, 1, 2, 3], l,color='red') self.canvas.draw() def on_stop(self): self.timer.stop() #close timer def on_exit(self): self.close() #close window qApp = QtWidgets.QApplication(sys.argv) aw = ApplicationWindow() aw.setWindowTitle("%s" % progname) aw.show() sys.exit(qApp.exec_()) #qApp.exec_()
pyqt5+timer+scatter(DO)
最新推荐文章于 2024-06-28 18:39:14 发布