Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e897855

Browse files
committed
Fully rewrite the qt embedding example.
1 parent 0b93410 commit e897855

File tree

1 file changed

+38
-104
lines changed

1 file changed

+38
-104
lines changed
Lines changed: 38 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
===============
3-
Embedding In Qt
3+
Embedding in Qt
44
===============
55
66
Simple Qt application embedding Matplotlib canvases. This program will work
@@ -9,10 +9,8 @@
99
"Qt5Agg", or by first importing the desired version of PyQt.
1010
"""
1111

12-
from __future__ import unicode_literals
13-
14-
import os
1512
import sys
13+
import time
1614

1715
import numpy as np
1816

@@ -26,105 +24,41 @@
2624
from matplotlib.figure import Figure
2725

2826

29-
progname = os.path.basename(sys.argv[0])
30-
31-
32-
class MyMplCanvas(FigureCanvas):
33-
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
34-
35-
def __init__(self, parent=None, width=5, height=4, dpi=100):
36-
fig = Figure(figsize=(width, height), dpi=dpi)
37-
FigureCanvas.__init__(self, fig)
38-
39-
self.axes = fig.subplots()
40-
self.compute_initial_figure()
41-
42-
self.setParent(parent)
43-
self.setSizePolicy(QtWidgets.QSizePolicy.Expanding,
44-
QtWidgets.QSizePolicy.Expanding)
45-
self.updateGeometry()
46-
47-
def compute_initial_figure(self):
48-
pass
49-
50-
51-
class MyStaticMplCanvas(MyMplCanvas):
52-
"""Simple canvas with a sine plot."""
53-
54-
def compute_initial_figure(self):
55-
t = np.arange(0.0, 3.0, 0.01)
56-
s = np.sin(2 * np.pi * t)
57-
self.axes.plot(t, s)
58-
59-
60-
class MyDynamicMplCanvas(MyMplCanvas):
61-
"""A canvas that updates itself every second with a new plot."""
62-
63-
def __init__(self, *args, **kwargs):
64-
MyMplCanvas.__init__(self, *args, **kwargs)
65-
timer = QtCore.QTimer(self)
66-
timer.timeout.connect(self.update_figure)
67-
timer.start(1000)
68-
69-
def compute_initial_figure(self):
70-
self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r')
71-
72-
def update_figure(self):
73-
self.axes.cla()
74-
# Plot 4 random integers between 0 and 9.
75-
self.axes.plot(range(4), np.random.randint(0, 10, 4), 'r')
76-
self.draw()
77-
78-
7927
class ApplicationWindow(QtWidgets.QMainWindow):
8028
def __init__(self):
81-
QtWidgets.QMainWindow.__init__(self)
82-
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
83-
self.setWindowTitle("application main window")
84-
85-
self.file_menu = QtWidgets.QMenu('&File', self)
86-
self.file_menu.addAction('&Quit', self.fileQuit,
87-
QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
88-
self.menuBar().addMenu(self.file_menu)
89-
90-
self.help_menu = QtWidgets.QMenu('&Help', self)
91-
self.menuBar().addSeparator()
92-
self.menuBar().addMenu(self.help_menu)
93-
94-
self.help_menu.addAction('&About', self.about)
95-
96-
self.main_widget = QtWidgets.QWidget(self)
97-
98-
l = QtWidgets.QVBoxLayout(self.main_widget)
99-
sc = MyStaticMplCanvas(self.main_widget, width=5, height=4, dpi=100)
100-
dc = MyDynamicMplCanvas(self.main_widget, width=5, height=4, dpi=100)
101-
l.addWidget(sc)
102-
l.addWidget(dc)
103-
104-
self.setCentralWidget(self.main_widget)
105-
106-
# Add a toolbar for each canvas.
107-
self.addToolBar(NavigationToolbar(sc, self))
108-
self.addToolBar(
109-
QtCore.Qt.BottomToolBarArea, NavigationToolbar(dc, self))
110-
111-
def fileQuit(self):
112-
self.close()
113-
114-
def closeEvent(self, ce):
115-
self.fileQuit()
116-
117-
def about(self):
118-
QtWidgets.QMessageBox.about(self, "About", """\
119-
embedding_in_qt_sgskip.py example
120-
121-
This program is a simple example of a Qt application embedding Matplotlib \
122-
canvases.""")
123-
124-
125-
qApp = QtWidgets.QApplication(sys.argv)
126-
127-
aw = ApplicationWindow()
128-
aw.setWindowTitle(progname)
129-
aw.show()
130-
sys.exit(qApp.exec_())
29+
super(ApplicationWindow, self).__init__()
30+
self._main = QtWidgets.QWidget()
31+
self.setCentralWidget(self._main)
32+
layout = QtWidgets.QVBoxLayout(self._main)
33+
34+
static_canvas = FigureCanvas(Figure(figsize=(5, 3)))
35+
layout.addWidget(static_canvas)
36+
self.addToolBar(NavigationToolbar(static_canvas, self))
37+
38+
dynamic_canvas = FigureCanvas(Figure(figsize=(5, 3)))
39+
layout.addWidget(dynamic_canvas)
40+
self.addToolBar(QtCore.Qt.BottomToolBarArea,
41+
NavigationToolbar(dynamic_canvas, self))
42+
43+
self._static_ax = static_canvas.figure.subplots()
44+
t = np.linspace(0, 10, 501)
45+
self._static_ax.plot(t, np.tan(t), ".")
46+
47+
self._dynamic_ax = dynamic_canvas.figure.subplots()
48+
self._timer = dynamic_canvas.new_timer(
49+
100, [(self._update_canvas, (), {})])
50+
self._timer.start()
51+
52+
def _update_canvas(self):
53+
self._dynamic_ax.clear()
54+
t = np.linspace(0, 10, 101)
55+
# Shift the sinusoid as a function of time.
56+
self._dynamic_ax.plot(t, np.sin(t + time.time()))
57+
self._dynamic_ax.figure.canvas.draw()
58+
59+
60+
if __name__ == "__main__":
61+
qapp = QtWidgets.QApplication(sys.argv)
62+
app = ApplicationWindow()
63+
app.show()
64+
qapp.exec_()

0 commit comments

Comments
 (0)