1

I am using pyqt5 to build a basic GUI for a project. The UI has only one button to open a dialog to pick file Load Data Button. When we click on this button, a File Dialog is opened to select a *.csv or *.tsv file.

I am trying to change the color of this QPushButton to blue using setStyleSheet property when data is being loaded. The button color should changes to green when data load is complete.

The button color is not changing to blue. It changes to green at the end when data is fully loaded. But not to blue while pd.read_csv('train_v2.csv') is running. I have even set the setStyleSheet property before running pd.read_csv line. Here is the code i am working on:

import sys
import pandas as pd
from PyQt5.QtWidgets import (QMainWindow, QProgressBar,
    QAction, QPushButton, QFileDialog, QApplication)
from PyQt5.QtGui import QIcon


class Example(QMainWindow):

    def __init__(self):

        super().__init__()

        self.initUI()

    def initUI(self):

        self.btn = QPushButton('Load data', self)
        self.btn.setGeometry(300, 300, 250, 30)
        self.btn.move(200, 40)
        self.btn.clicked.connect(self.showDialog)
        self.statusBar()
        self.progressbar = QProgressBar()

        openFile = QAction(QIcon('open.png'), 'Open', self)
        openFile.setShortcut('Ctrl+O')
        openFile.setStatusTip('Open new File')
        openFile.triggered.connect(self.showDialog)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(openFile)

        self.setGeometry(300, 300, 650, 400)
        self.setWindowTitle('File Dialog')
        self.show()

    def showDialog(self):

        fname = QFileDialog.getOpenFileName(self, "Open File", '/home',
        "Tabular (*csv *.tsv)")

        if fname[0] is not '':

            self.btn.setStyleSheet(" background-color: blue")
            data = pd.read_csv(fname[0])

            self.setStatusTip('!!! Data loaded sucessfully!!! Data contains: %d rows and %d columns.' % (data.shape[0], data.shape[1]))

            self.btn.setStyleSheet(" background-color: green")



if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

I am using python 3.4.5 with pyqt5 installed in it. Any help would be much appreciated.

ML_NN
  • 101
  • 1
  • 7
  • See also: https://stackoverflow.com/questions/1386043/how-to-make-qt-work-when-main-thread-is-busy – Sébastien Aug 11 '17 at 10:37
  • I'd try `background-color: rgb(0,255,0);` because "blue" might not be a supported word, while "green" maybe it's. Check this way and see if it works. – Saelyth Aug 13 '17 at 00:09
  • If you still need it try to make another question not duplicated and I'll answer it to you with a small example, since this one I'm not able to post an answer. Just tag me or send here the link of the other question. – yurisnm Aug 14 '17 at 18:28

0 Answers0