从函数运行pyqtgraph

我在pyqtgraph中创建了以下图形。它可以正常运行并按应显示的方式显示。


import numpy as np

import pyqtgraph as pg

import pyqtgraph.exporters


from pyqtgraph.Qt import QtCore, QtGui

import csv

import itertools

from datetime import datetime as dt



app = QtGui.QApplication([])



class TimeAxisItem(pg.AxisItem):

    def tickStrings(self, values, scale, spacing):

            return [dt.fromtimestamp(value) for value in values]



x_vals = [dt(2018, 7, 1, 13, 6, 38), dt(2018, 7, 1, 12, 46, 38), dt(2018, 7, 1, 12, 36, 38), dt(2018, 7, 1, 12, 26, 38), dt(2018, 7, 1, 12, 16, 38), dt(2018, 7, 1, 12, 6, 38), dt(2018, 7, 1, 11, 56, 38), dt(2018, 7, 1, 11, 46, 38), dt(2018, 7, 1, 11, 36, 38), dt(2018, 7, 1, 11, 26, 38), dt(2018, 7, 1, 10, 56, 38), dt(2018, 7, 1, 10, 46, 38), dt(2018, 7, 1, 10, 36, 38)]

y_vals =  [23.5, 20.8, 28.0, 28.1, 28.0, 27.8, 27.3, 27.2, 25.7, 24.7, 25.0, 25.0, 24.9]



date_axis = TimeAxisItem(orientation='bottom')

plt = pg.PlotWidget(axisItems = {'bottom': date_axis})


# set properties

plt.setLabel('left', 'Temperature', units='C')

plt.setWindowTitle('Temperature over time')


plt.plot(x=[x.timestamp() for x in x_vals], y=y_vals, symbol=None, pen='r', symbolPen='r')

plt.show()  




if __name__ == '__main__':

    import sys

    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):

        QtGui.QApplication.instance().exec_()

但是,当我尝试从某个函数运行它时,它会闪烁然后消失,但pyqt仍在运行。


import numpy as np

import pyqtgraph as pg

import pyqtgraph.exporters


from pyqtgraph.Qt import QtCore, QtGui

import csv

import itertools

from datetime import datetime as dt



app = QtGui.QApplication([])



class TimeAxisItem(pg.AxisItem):

    def tickStrings(self, values, scale, spacing):

        return [dt.fromtimestamp(value) for value in values]

如何在不崩溃的情况下从函数运行图形?


Helenr
浏览 239回答 2
2回答

慕斯王

在main()内调用Gshow()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python