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.