Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将更改/更新数据传递到另一个弹出窗口?_Python_Pyqt5_Pyqtgraph - Fatal编程技术网

Python 如何将更改/更新数据传递到另一个弹出窗口?

Python 如何将更改/更新数据传递到另一个弹出窗口?,python,pyqt5,pyqtgraph,Python,Pyqt5,Pyqtgraph,我得到了Python GUI的以下代码。我发现无法将主窗口中运行的数据更新为弹出窗口 在本例中,我使用一些随机数据模拟视频帧,并使用计时器更新数据(updateData()函数) 我想看到弹出窗口中的绿色曲线变化。。。但是我找不到一种有效的方法将不断变化的数据传递到弹出窗口 我应该使用pyqtSignal()来定义自定义信号吗?我读书。我是否应该将updateData()插槽函数定义为自定义信号,然后连接到另一个插槽函数plot_data() 我对你的power\u窗口类做了一些更改: clas

我得到了Python GUI的以下代码。我发现无法将主窗口中运行的数据更新为弹出窗口

在本例中,我使用一些随机数据模拟视频帧,并使用计时器更新数据(
updateData()
函数)

我想看到弹出窗口中的绿色曲线变化。。。但是我找不到一种有效的方法将不断变化的数据传递到弹出窗口

我应该使用pyqtSignal()来定义自定义信号吗?我读书。我是否应该将
updateData()
插槽函数定义为自定义信号,然后连接到另一个插槽函数
plot_data()


我对你的
power\u窗口
类做了一些更改:

class power\u窗口(QMainWindow):
#此窗口没有父窗口。它将显示为我们想要的自由浮动窗口。
定义初始化(自身,父级):
super()。\uuuu init\uuuuu()
self.setWindowTitle(“帧中的总功率”)
self.main_widget=QWidget()
self.main_layout=QGridLayout()
self.main\u小部件.setLayout(self.main\u布局)
self.setCentralWidget(self.main_小部件)
self.plot_plt=pg.PlotWidget()#这是我们的绘图画布
self.plot_plt.showGrid(x=True,y=True)#显示网格
self.main\u layout.addWidget(self.plot\u plt,1,0,3,3)
#自绘图设置范围(最大值=100,最小值=0)
self.parent=parent
self.data_list=[]
parent.timer.timeout.connect(self.plot_data)#更新plot
#parent.updateData.change.connect(self.plot_data)#不工作,可能需要对自定义信号使用pyqtSignal
self.plot_data()
def plot_数据(自身):
self.frame\u sum\u data=self.parent.updateData()
self.data\u list.append(self.frame\u sum\u data)
self.plot_plt.plot().setData(self.data_list,pen='g')#更改画笔的颜色
我认为问题是因为您的程序调用了
\uuuu init\uuuu
上的
updateData()
,因此它只获取第一个值,不会再次更新该值

告诉我它是否符合你的要求。


上面是演示的样子。弹出窗口在左视图中显示“实时”汇总值。右视图显示了左侧的FFT。

Hi@Alejandro,是的,它工作正常。但我发现这种方法可能不是最好的。一旦发出
timeout
信号,
CameraGUI
类中的
updateData()
slot函数将运行一次,并且
power\u窗口()
类中的
plot\u data()
slot函数将运行并再次调用
updateData()
以获取其返回值。因此,
updateData()
总共运行了两次!我现在有另一种方法:我将
绘图数据()
的信号/槽移到单击的
操作按钮()
,并获取
更新数据()函数中弹出窗口的更新数据。:)
"""
1/15/2021  Yuanhang Zhang

"""

from PyQt5.QtWidgets import QMainWindow,QApplication,QGridLayout,QWidget, QPushButton,QDockWidget
from PyQt5.QtCore import Qt,QTimer
from PyQt5.QtGui import QPixmap,QFont
import numpy as np
import pyqtgraph as pg
from pyqtgraph import GradientWidget

class power_window(QMainWindow):
    # this window has no parent. It will appear as a free-floating window as we want.
    def __init__(self,parent):
        super().__init__()
        self.setWindowTitle("Total power in the frame")
        self.main_widget = QWidget()  
        self.main_layout = QGridLayout()  
        self.main_widget.setLayout(self.main_layout)  
        self.setCentralWidget(self.main_widget)  
 
        self.plot_plt = pg.PlotWidget()   # this is our plot canvas
        self.plot_plt.showGrid(x=True,y=True) # show grid
        self.main_layout.addWidget(self.plot_plt, 1, 0, 3, 3)
 
        # self.plot_plt.setYRange(max=100,min=0)

        self.data_list = []
        parent.timer.timeout.connect(self.plot_data) # update plot
        # parent.updateData.change.connect(self.plot_data) # not working, may need to use pyqtSignal for custom signals
        self.frame_sum_data = parent.updateData()
        self.plot_data()

    def plot_data(self):
        self.data_list.append(self.frame_sum_data)
        self.plot_plt.plot().setData(self.data_list,pen='g') # change the color of the pen



class CameraGUI(QMainWindow):
    def __init__(self):
        super().__init__()

        ## Create some random data to mimic video data
        # scale: Standard deviation (spread or “width”) of the distribution. 
        # loc: Mean (“centre”) of the distribution.
        self.data = np.random.normal(size=(15, 30, 30), loc=1024, scale=200).astype(np.uint16)
        self.i = 0
        self.power_gui = None  # for the pop up window
        self.initializeUI()
     
    def initializeUI(self):
        """
        Initialize the window and display its contents to the screen
        """
        self.setGeometry(100,100,1000,800) # (x, y, width, height)
        self.setWindowTitle('Camera GUI')
        
        self.main_widget =  pg.GraphicsLayoutWidget()
        self.setCentralWidget(self.main_widget)  # set the default widget of the MainWindow
        
        self.createCameraWidgets()
        
        self.show()  
 
    
   
    def updateLUT(self):      ## change colormap (color look up table)
        lut = self.gradient.getLookupTable(256)
        return lut

    def action_on_button_clicked(self):  # for the pop-up windown
        # https://www.learnpyqt.com/tutorials/creating-multiple-windows/ 
        if self.power_gui is None:
            # self.power_gui = power_window(np.sum(self.data[self.i]))
            self.power_gui = power_window(self)
            self.power_gui.show()


    def createCameraWidgets(self):
        """
        Setup widgets using QGridLayout
        """
        self.gradient = GradientWidget(parent = self.main_widget,orientation='top')
        self.gradient.sigGradientChanged.connect(self.updateLUT)

        self.dock_widget = QDockWidget(self)
        self.dock_widget.setAllowedAreas(Qt.AllDockWidgetAreas)
        
        # set initial location of dock widget in main window
        self.addDockWidget(Qt.TopDockWidgetArea,self.dock_widget)

        self.button = QPushButton('Power',self)   
        self.button.clicked.connect(self.action_on_button_clicked)  ## --> not updating the data ??
        self.dock_widget.setWidget(self.button)

        ## add view box
        self.view1 = self.main_widget.addViewBox(row = 1, col = 0)
        self.view2 = self.main_widget.addViewBox(row = 1, col = 1)

        ## lock the aspect ratio so pixels are always square
        self.view1.setAspectLocked(True)
        self.view2.setAspectLocked(True)
    
        ## Create image item
        self.img1 = pg.ImageItem(border='w')
        self.img2 = pg.ImageItem(border='w')

        self.view1.addItem(self.img1)
        self.view2.addItem(self.img2)

        # timer of the main window
        self.timer = QTimer()
        self.timer.setInterval(200)      # every 200 ms will emit timeout signal
        self.timer.timeout.connect(self.updateData)  # when timeout signal is emit, it will run updatedata() function
        self.timer.start()

        print(np.sum(self.data[self.i]))  # this will only run once
        print(self.i)


    def updateData(self):
        ## Display the data
        self.img1.setImage(self.data[self.i],lut = self.updateLUT())    # when i changes, set to display in img1
        self.data2 = np.log(np.abs(np.fft.fft2(self.data[self.i])))  # calculate data2 beased on data[i]
        self.i = (self.i+1) % self.data.shape[0]   # update i
        self.img2.setImage(self.data2,lut = self.updateLUT())

        # print(self.i)
        print(np.sum(self.data[self.i])) # this will keep running every 200 ms
        return np.sum(self.data[self.i])



## run the program
if __name__ =="__main__":
    import sys
    app = QApplication(sys.argv)               
    window = CameraGUI()
    window.updateData()
    sys.exit(app.exec_())