0

Basically, when the Switch Button is pressed, which is connected to the reconfigure method, I want everything in the reconfigure method to run as a separate thread/process so the main GUI is still accessible and not being blocked. Below is a watered down version of my code.

import sys, time
from PyQt4 import QtGui, QtCore
from PyQt4.Qt import *

#//Popup Class - Will appear when the Switch Button is pressed
class Popup(QWidget):
    def __init__(self):
        QWidget.__init__(self)
    #//nothing here now, it will have a message telling user to wait while program is run

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        grid = QtGui.QGridLayout()
        label_header = QtGui.QLabel("TEST RECONFIGURE")
        font = label_header.font()
        font.setPointSize(24)
        label_header.setFont(font)
        #//Creating Static Labels that will be placed in the GUI
        label_1 = QtGui.QLabel("Menu 1:")
        label_2 = QtGui.QLabel("Menu 2:")
        label_spacer = QtGui.QLabel("")
        label_cfg = QtGui.QLabel("Current Configuration: '/tmp/directory_here' ")

        global comboBox1
        comboBox1 = QtGui.QComboBox()
        comboBox1.addItem("1")
        comboBox1.addItem("2")
        global comboBox2
        comboBox2 = QtGui.QComboBox()
        comboBox2.addItem("3")
        comboBox2.addItem("4")

        #//Switch Button!!!
        global switchButton
        switchButton = QPushButton("Switch")
        switchButton.clicked.connect(self.reconfigure)

        quitButton = QtGui.QPushButton('Quit')
        quitButton.clicked.connect(QtCore.QCoreApplication.instance().quit)

        #//Configure the grid layout
        grid.addWidget(label_spacer,    0,0,9,9)
        grid.addWidget(label_header,    0,0,1,6)
        grid.addWidget(label_1,         1,0,1,1)
        grid.addWidget(comboBox1,       2,0,1,1)
        grid.addWidget(label_2,         3,0,1,1)
        grid.addWidget(comboBox2,       4,0,1,1)
        grid.addWidget(switchButton,    5,0,1,2)
        grid.addWidget(label_cfg,       6,0,1,9)
        grid.addWidget(quitButton,      9,9,1,1)
        self.setLayout(grid)
        self.setGeometry(640,300,400,600)
        self.show()

    #//open up the popup window for switch button, and reconfigure  
    def reconfigure(self):
        print "Opening New Window"
        self.w = Popup()
        self.w.setGeometry(QRect(self.x()+100,self.y()+100,400,200))
        self.w.show()
        txt1 = str(comboBox1.currentText())
        txt2 = str(comboBox2.currentText())
        print " reconfiguring to option %s and option %s" %(txt1, txt2) 

        #
        # This is where most of the work is done, and takes about 1/2 an hour for everything to run
        # Want to make this method a separate thread/process so the rest of the main GUI is still accessible
        # while the program is running as the whole class will be a separate tab in a larger GUI
        #
        print "all done!"
#//runner
def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

I feel like I have to use threading and signals to accomplish this, but I am not having much luck. Any help would be greatly appreciated. Thank you!

user5494969
  • 171
  • 3
  • 14
  • Possible duplicate of [Background thread with QThread in PyQt](http://stackoverflow.com/questions/6783194/background-thread-with-qthread-in-pyqt) – Mel Nov 05 '15 at 08:17

1 Answers1

0
import sys, time
from PyQt4 import QtGui, QtCore
from PyQt4.Qt import *


class ConfigureThread(QtCore.QThread):
    def run(self):
        pass
        #
        # This is where most of the work is done, and takes about 1/2 an hour for everything to run
        # Want to make this method a separate thread/process so the rest of the main GUI is still accessible
        # while the program is running as the whole class will be a separate tab in a larger GUI
        #


#//Popup Class - Will appear when the Switch Button is pressed
class Popup(QWidget):
    def __init__(self):
        QWidget.__init__(self)
    #//nothing here now, it will have a message telling user to wait while program is run

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        grid = QtGui.QGridLayout()
        label_header = QtGui.QLabel("TEST RECONFIGURE")
        font = label_header.font()
        font.setPointSize(24)
        label_header.setFont(font)
        #//Creating Static Labels that will be placed in the GUI
        label_1 = QtGui.QLabel("Menu 1:")
        label_2 = QtGui.QLabel("Menu 2:")
        label_spacer = QtGui.QLabel("")
        label_cfg = QtGui.QLabel("Current Configuration: '/tmp/directory_here' ")

        global comboBox1
        comboBox1 = QtGui.QComboBox()
        comboBox1.addItem("1")
        comboBox1.addItem("2")
        global comboBox2
        comboBox2 = QtGui.QComboBox()
        comboBox2.addItem("3")
        comboBox2.addItem("4")

        #//Switch Button!!!
        global switchButton
        switchButton = QPushButton("Switch")
        switchButton.clicked.connect(self.reconfigure)

        quitButton = QtGui.QPushButton('Quit')
        quitButton.clicked.connect(QtCore.QCoreApplication.instance().quit)

        #//Configure the grid layout
        grid.addWidget(label_spacer,    0,0,9,9)
        grid.addWidget(label_header,    0,0,1,6)
        grid.addWidget(label_1,         1,0,1,1)
        grid.addWidget(comboBox1,       2,0,1,1)
        grid.addWidget(label_2,         3,0,1,1)
        grid.addWidget(comboBox2,       4,0,1,1)
        grid.addWidget(switchButton,    5,0,1,2)
        grid.addWidget(label_cfg,       6,0,1,9)
        grid.addWidget(quitButton,      9,9,1,1)
        self.setLayout(grid)
        self.setGeometry(640,300,400,600)
        self.show()

    #//open up the popup window for switch button, and reconfigure  
    def reconfigure(self):
        print("Opening New Window")
        self.w = Popup()
        self.w.setGeometry(QRect(self.x()+100,self.y()+100,400,200))
        self.w.show()
        txt1 = str(comboBox1.currentText())
        txt2 = str(comboBox2.currentText())
        print(" reconfiguring to option %s and option %s" %(txt1, txt2))

        self.th = ConfigureThread()
        self.th.finished.connect(self.configuring_done)
        self.th.start()

    def configuring_done(self):
        print("all done!")

#//runner
def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
Alexander Lutsenko
  • 2,130
  • 8
  • 14