0

I am new to PyQt and I try to develop a GUI for my lab so that others can use my algorithm as a black box. The GUI is designed to open a text file, import the data into a pandas dataframe, then run certain algorithm and save the result into another text file. I attached the code without the algorithm below.

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_()

I can run the code succesfully, but after I click on the 'import' button, the program stops running.

Image 1: Program interface

Image 2: Python has stopped running

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Neyo
  • 21
  • 2
  • 7
  • Besides, I am using Pycharm as IDE. It says **Cannot find reference 'connect' in 'function'**. But according to [link](https://youtrack.jetbrains.com/issue/PY-24183) this is fine. Not sure if this is the cause. – Neyo Aug 06 '18 at 16:09
  • 1
    change `filePath = QtWidgets.QFileDialog....` to `filePath, _ = QtWidgets.QFileDialog...` – eyllanesc Aug 06 '18 at 16:15
  • @eyllanesc That works for importing, however now the 'Run' button starts giving me 'Python has stopped running' when click on it. – Neyo Aug 06 '18 at 16:32
  • I recommend you execute your script from the CMD or terminal, the IDEs do not show the error message and they send you that ugly warning. – eyllanesc Aug 06 '18 at 16:33
  • `s = self.df.size()` or `s = self.df.size`? https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.size.html – eyllanesc Aug 06 '18 at 16:34
  • @eyllanesc You are right. Execute the script in terminal totally helped. Turns out write() argument must be str. Thank you! – Neyo Aug 06 '18 at 17:09

0 Answers0