PyQt5单击按钮:Python已停止运行

时间:2018-08-06 16:04:51

标签: python pandas pyqt

我是PyQt的新手,我尝试为自己的实验室开发GUI,以便其他人可以将我的算法用作黑匣子。 GUI旨在打开一个文本文件,将数据导入pandas数据框,然后运行某些算法并将结果保存到另一个文本文件中。我附加了下面没有算法的代码。

from PyQt5 import QtWidgets
import sys
import pandas as pd

class PrettyWidget(QtWidgets.QWidget):

    def __init__(self):
        super(PrettyWidget, self).__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(600,300, 1000, 600)
        self.setWindowTitle('Hello World')

        # Grid Layout
        grid = QtWidgets.QGridLayout()
        self.setLayout(grid)

        # Import Button
        btn1 = QtWidgets.QPushButton('Import', self)
        btn1.resize(btn1.sizeHint())
        btn1.clicked.connect(self.getCSV)
        grid.addWidget(btn1, 0, 0)

        self.df = pd.DataFrame()

        # Run Button
        btn2 = QtWidgets.QPushButton('Run', self)
        btn2.resize(btn2.sizeHint())
        btn2.clicked.connect(self.RunnSave)
        grid.addWidget(btn2, 0, 1)

        self.show()

    def getCSV(self):
        filePath = QtWidgets.QFileDialog.getOpenFileName(self,'Text File','','*.txt')
        self.df = pd.read_csv(str(filePath))

    def RunnSave(self):
        s = self.df.size()
        with open('hello.txt','w') as f:
            f.write(s)

def main():
    app = QtWidgets.QApplication(sys.argv)
    w = PrettyWidget()
    app.exec_()

我可以成功运行代码,但是在单击“导入”按钮后,程序停止运行。

Image 1: Program interface

Image 2: Python has stopped running

0 个答案:

没有答案