I have a project I am working on for work. Due to the nature of the project I cant share all of the code.
Below is the code of me trying to implement a thread on button click.
from PyQt5 import QtCore, QtWidgets, QtGui, QtSerialPort
from PyQt5 import uic
import sys, time
class BETA_THREADED(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.ui = uic.loadUi('rocket_3.ui', self)
# self.resize(1920, 1080)
self.thread={}
self.launchButton.clicked.connect(self.start_worker)
self.abortButton.clicked.connect(self.stop_worker)
def start_worker(self):
print('1')
self.thread[1] = ThreadClass(parent = None, index = 1)
self.thread[1].start()
self.thread[1].any_signal.connect(self.launchSequence)
self.launchButton.setEnabled(False)
def stop_worker(self):
self.thread[1].stop()
self.launchButton.setEnabled(True)
def launchSequence(self):
print('Test')
class ThreadClass(QtCore.QThread):
any_signal = QtCore.pyqtSignal(int)
def __init__(self, parent=None,index=0):
super(ThreadClass, self).__init__(parent)
self.index=index
self.is_running = True
def run(self):
print('Starting thread...',self.index)
cnt=0
while (True):
cnt+=1
if cnt==99: cnt=0
time.sleep(0.01)
self.any_signal.emit(cnt)
def stop(self):
self.is_running = False
print('Stopping thread...',self.index)
self.terminate()
app = QtWidgets.QApplication(sys.argv)
mainWindow = BETA_THREADED()
mainWindow.show()
sys.exit(app.exec_())
The UI file is generated by PyQt Designer, and the buttons are regular push buttons.
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Rocket</class>
<widget class="QMainWindow" name="Rocket">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1262</width>
<height>323</height>
</rect>
</property>
<property name="windowTitle">
<string>Rocket</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTextBrowser" name="statusBar">
<property name="geometry">
<rect>
<x>230</x>
<y>220</y>
<width>1011</width>
<height>61</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_6">
<property name="geometry">
<rect>
<x>230</x>
<y>190</y>
<width>111</width>
<height>17</height>
</rect>
</property>
<property name="font">
<font>
<family>Carlito</family>
<pointsize>14</pointsize>
</font>
</property>
<property name="text">
<string>STATUS</string>
</property>
</widget>
<widget class="QGroupBox" name="groupBox">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>-10</x>
<y>-10</y>
<width>221</width>
<height>291</height>
</rect>
</property>
<property name="title">
<string/>
</property>
<property name="flat">
<bool>false</bool>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<widget class="QPushButton" name="launchButton">
<property name="geometry">
<rect>
<x>30</x>
<y>60</y>
<width>161</width>
<height>101</height>
</rect>
</property>
<property name="font">
<font>
<family>Carlito</family>
<pointsize>14</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color: blue;</string>
</property>
<property name="text">
<string>LAUNCH</string>
</property>
</widget>
<widget class="QPushButton" name="abortButton">
<property name="geometry">
<rect>
<x>30</x>
<y>180</y>
<width>161</width>
<height>101</height>
</rect>
</property>
<property name="font">
<font>
<family>Carlito</family>
<pointsize>14</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color: red;</string>
</property>
<property name="text">
<string>ABORT</string>
</property>
</widget>
</widget>
</widget>
</widget>
<tabstops>
<tabstop>launchButton</tabstop>
<tabstop>statusBar</tabstop>
<tabstop>abortButton</tabstop>
</tabstops>
<resources/>
<connections>
<connection>
<sender>launchButton</sender>
<signal>clicked()</signal>
<receiver>launchButton</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>546</x>
<y>720</y>
</hint>
<hint type="destinationlabel">
<x>546</x>
<y>720</y>
</hint>
</hints>
</connection>
<connection>
<sender>abortButton</sender>
<signal>clicked()</signal>
<receiver>abortButton</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>630</x>
<y>910</y>
</hint>
<hint type="destinationlabel">
<x>630</x>
<y>910</y>
</hint>
</hints>
</connection>
</connections>
</ui>
When I run the file my window opens as expected and then closes on any button click. I cannot for the life of me figure out why.
Edit: Corrected the spelling and other error noted, provided the full file for both python and UI. The program still closes on any button click with no error in cmd.
