Search code examples
pythonpyqtpyqt5python-3.6qthread

When executing thread, the first execution fails in try statement


    from PyQt5.QtWidgets import QMainWindow, QApplication,QLineEdit, QPushButton, QWidget, QAction, QTabWidget,QVBoxLayout
from PyQt5.QtCore import (QCoreApplication, QObject, QRunnable, QThread,
                          QThreadPool, pyqtSignal)
import sys
import os
from shutil import copy2
import _thread
import time


class AThread(QThread):

    def run(self):
        count = 0
        while count < 5:
            time.sleep(1)
            print("A Increasing")
            count += 1

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.setAcceptDrops(True)
        self.setWindowTitle('Learn')
        self.setGeometry(300, 300, 300, 150)
        self.layout = QVBoxLayout(self)

        # Initialize tab screen
        self.tabs = QTabWidget()
        self.tab1 = QWidget()   
        self.tab2 = QWidget()
        self.tabs.resize(300,200) 

        # Add tabs
        self.tabs.addTab(self.tab1,"Tab 1")
        self.tabs.addTab(self.tab2,"Tab 2")

        # Create first tab
        self.tab1.layout = QVBoxLayout(self)
        self.pushButton1 = QPushButton("PyQt5 button")
        self.pushButton1.clicked.connect(self.ON_PRESS)
        self.textbox = QLineEdit(self)
        self.tab1.layout.addWidget(self.textbox )
        self.tab1.layout.addWidget(self.pushButton1)
        self.tab1.setLayout(self.tab1.layout)
        #Create Textbox inputs


        # Add tabs to widget        
        self.layout.addWidget(self.tabs)
        self.setLayout(self.layout)

    def using_q_thread(self):
        app = Example()
        thread = AThread()
        thread.start()
        sys.exit(app.exec_())

    def ON_PRESS(self):
###Here is the Issue
        try:
            self.using_q_thread()    
        except:
            print ("Error: unable to start thread")
###Drag and Drop files to directory
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()

Hoping I am asking this correctly, but whenever using QThread there appears to be a bit of a hiccup. the first attempt to access the threaded function causes the try statement to fail, but then it immediately works. Im just curious if this is part of the functionality or if there is any issue with my code.


Solution

  • Avoid using try-except as you see hidden the error, in my personal case I avoid using it as far as I can for this type of problems.

    I do not see it necessary to create another Example within using_q_thread, another problem is that thread is a local variable that will be eliminated, so thread must be a member of the class for its scope to increase.

    import sys
    import time
    from PyQt5.QtCore import QThread
    from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTabWidget, QPushButton, QLineEdit
    
    
    class AThread(QThread):
        def run(self):
            count = 0
            while count < 5:
                time.sleep(1)
                print("A Increasing")
                count += 1
    
    class Example(QWidget):
        def __init__(self):
            super().__init__()
            self.setAcceptDrops(True)
            self.setWindowTitle('Learn')
            self.setGeometry(300, 300, 300, 150)
            self.layout = QVBoxLayout(self)
    
            # Initialize tab screen
            self.tabs = QTabWidget()
            self.tab1 = QWidget()   
            self.tab2 = QWidget()
            self.tabs.resize(300,200) 
    
            # Add tabs
            self.tabs.addTab(self.tab1,"Tab 1")
            self.tabs.addTab(self.tab2,"Tab 2")
    
            # Create first tab
            self.tab1.layout = QVBoxLayout()
            self.pushButton1 = QPushButton("PyQt5 button")
            self.pushButton1.clicked.connect(self.ON_PRESS)
            self.textbox = QLineEdit(self)
            self.tab1.layout.addWidget(self.textbox )
            self.tab1.layout.addWidget(self.pushButton1)
            self.tab1.setLayout(self.tab1.layout)
            #Create Textbox inputs
    
    
            # Add tabs to widget        
            self.layout.addWidget(self.tabs)
    
        def using_q_thread(self):
            self.thread = AThread()
            self.thread.start()
    
    
        def ON_PRESS(self):
            self.using_q_thread()    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = Example()
        ex.show()
        sys.exit(app.exec_())