PyQt5-将变量传递给新窗口中使用的另一个类

时间:2018-10-29 13:33:23

标签: python pyqt5

当我按下按钮时,我正在PyQt5中创建一个新窗口,我有一些要传递给类的数据(新窗口),但是在读取文件后首先知道了该数据,并且另一个类的变量已经在Menu类的init中初始化了。

我希望从Menu中读取的数据可以在PlotCanvas中使用。(现在它只是随机数据)

尚未读取数据,它将是readCsvFile,它将把数据提供给两个Class变量dataSet1和dataSet2。

如何将dataSet1和dataSet2传递给类PlotCanvas

class Menu(QWidget):
    dataSet1 = []
    dataSet2 = []

    def __init__(self):
        super(Menu, self).__init__()
        loadUi('data_visualizing.ui', self)
        self.setWindowTitle('Data Visualizer')

        self.pushButton.setToolTip('Click here to browse for the first data file')
        self.pushButton.clicked.connect(self.on_click)
        self.pushButton2.setToolTip('Click here to browse for the second data file')
        self.pushButton2.clicked.connect(self.on_click)
        self.pushButton3.setToolTip('Click here to get the chosen graph')
        self.pushButton3.clicked.connect(self.on_click_ok_button)

        self.lineEdit.setReadOnly(True)
        self.lineEdit2.setReadOnly(True)

        self.comboBox.addItem('Graph')
        self.comboBox.addItem('2')

        self.dialog = Graph(self)

    def openFileNameDialog(self, which_button):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        filename, _ = QFileDialog.getOpenFileName(self, "Open file", os.getenv('HOME'),
                                                  "All Files (*);;Comma seperated files (*.csv)", options=options)
        if filename:
            print(filename)
            if which_button is self.pushButton:
                self.lineEdit.setText(filename)
            elif which_button is self.pushButton2:
                self.lineEdit2.setText(filename)

    def readCsvFiles(self):
        if self.lineEdit.text() != '':
            spamReader = csv.reader(open(self.lineEdit.text()), delimiter=',', quotechar='|')
            for row in spamReader:
                print(', '.join(row))

    @pyqtSlot()
    def on_click(self):
        self.openFileNameDialog(self.sender())

    @pyqtSlot()
    def on_click_ok_button(self):
        if self.lineEdit.text() != '' and self.lineEdit2.text() != '':
            self.dialog.show()
            self.readCsvFiles()
        else:
            #message box magic
            print("no")


class Graph(QWidget):
    def __init__(self, parent = None):
        super(Graph, self).__init__()
        m = PlotCanvas(self)
        m.move(0, 0)


class PlotCanvas(FigureCanvas):
    def __init__(self, parent=None):
        width = 5
        height = 4
        dpi = 100
        fig = Figure(figsize=(width, height), dpi=dpi)
        #self.axes = fig.add_subplot(111)

        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)
        self.plot()

    def plot(self):
        data = [random.random() for i in range(25)]
        data2 = [random.random() for i in range(25)]
        ax = self.figure.add_subplot(111)
        ax.plot(data, 'r-')
        ax.plot(data2, 'b-')
        ax.set_title('PyQt Matplotlib Example')
        self.draw()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = Menu()
    widget.show()
    sys.exit(app.exec_())

0 个答案:

没有答案