У меня странная ошибка в моем коде, и я действительно изо всех сил пытаюсь найти причину. У меня есть небольшое gui -приложение, которое выполняет некоторые вычисления и должно выводить результаты процесса в gui.
Чтобы избежать зависаний приложения, я хотел использовать QThread, поэтому, когда моя кнопка по щелчку запускается функция:
def play_simulation(self):
self.thread = ProductionProcess()
print('Aufruf')
self.thread.start()
Мой поток выглядит следующим образом:
class ProductionProcess(QThread):
def __init__(self):
QThread.__init__(self)
self.iteration = 0
def __del__(self):
self.wait()
def run(self):
while self.iteration < 5:
print(self.iteration)
self.iteration = self.iteration + 1
#self.machine_one_no_variation_status.emit(random.randint(0,100))
self.sleep(2)
К сожалению, программа все еще зависает, первый раз поток запускается, но из-за отсутствия активности пользователя в gui он запускает поток во второй раз, и тогда я могу использовать gui, как предсказано с QThread.
Любые идеи, в чем моя проблема? Вся программа такова:
from PyQt5 import QtWidgets, uic, QtGui
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import Qt, QThread, pyqtSignal
import time
import random
class PlayEOVE(QtWidgets.QWidget):
def __init__(self, main_window, parent=None):
super().__init__(parent)
self.ui = uic.loadUi('LeanPlayground/gui/EffectOfVariationExample.ui', self)
self.show()
self.main_window = main_window
self.loadAssets()
self.reset_stats()
# Stockhandler
self.ui.imgStock2.setPixmap(QPixmap('LeanPlayground/assets/stock_empty.png'))
self.ui.imgStock3.setPixmap(QPixmap('LeanPlayground/assets/stock_empty.png'))
self.ui.imgFGStock.setPixmap(QPixmap('LeanPlayground/assets/stock_empty.png'))
self.actions()
def actions(self):
# actions
self.ui.btnPlaySimulation.clicked.connect(self.play_simulation)
self.ui.btnResetSimulation.clicked.connect(self.reset_stats)
self.ui.timeSlider.valueChanged.connect(self.set_simulation_speed)
self.ui.btnPlaySimulation.clicked.connect(self.play_simulation)
self.ui.btnPauseSimulation.released.connect(self.pause_simulation)
def pause_simulation(self):
self.simulation_active = False
def closeEvent(self, event):
self.main_window.show()
def loadAssets(self):
# machines
self.ui.imgMachine1.setPixmap(QPixmap('LeanPlayground/assets/machine.png'))
self.ui.imgMachine2.setPixmap(QPixmap('LeanPlayground/assets/machine.png'))
self.ui.imgMachine3.setPixmap(QPixmap('LeanPlayground/assets/machine.png'))
#Stocks
self.ui.imgStock2.setPixmap(QPixmap('LeanPlayground/assets/stock_filled_full.png'))
self.ui.imgStock3.setPixmap(QPixmap('LeanPlayground/assets/stock_filled_half.png'))
self.ui.imgFGStock.setPixmap(QPixmap('LeanPlayground/assets/stock_empty.png'))
def play_simulation(self):
#self.simulation_active = True
#self.simulation_thread = threading.Thread(target=self.actions(), args=(1,))
#self.simulation_thread.start()
#self.simulate()
self.thread = ProductionProcess()
print('Aufruf')
#self.thread.machine_one_no_variation_status.connect(self.test)
self.thread.start()
def test(self, val):
self.ui.pgBarMachineOneNoVariation.setValue(val)
def set_simulation_speed(self):
self.simulation_speed = self.ui.timeSlider.value()
self.ui.dataTimeSlider.setText(str(float(self.simulation_speed)) + " %")
def reset_stats(self):
#calculation Vars
self.simulation_active = False
self.iteration = 0
self.simulation_speed = self.ui.timeSlider.value()
self.stock_level_2_no_variation = 0
self.stock_level_3_no_variation = 0
self.stock_level_FG_no_variation = 0
self.stock_level_2_with_variation = 0
self.stock_level_3_with_variation = 0
self.stock_level_FG_with_variation = 0
self.machine_one_with_variation_status = 0
self.machine_two_with_variation_status = 0
self.machine_three_with_variation_status = 0
self.machine_one_no_variation_status = 0
self.machine_two_no_variation_status = 0
self.machine_three_no_variation_status = 0
self.write_to_frontend()
def write_to_frontend(self):
self.ui.pgBarMachineOneNoVariation.setValue(self.machine_one_no_variation_status)
self.ui.pgBarMachineOneWithVariation.setValue(self.machine_one_with_variation_status)
self.ui.pgBarMachineThreeWithVariation.setValue(self.machine_three_with_variation_status)
self.ui.pgBarMachineTwoNoVariation.setValue(self.machine_two_no_variation_status)
self.ui.pgBarMachineTwoWithVariation.setValue(self.machine_two_with_variation_status)
self.ui.pgBarmachineThreeNoVariation.setValue(self.machine_three_no_variation_status)
# Fill Textfields
self.ui.dataTime.setText(str(self.iteration) + ' min')
self.set_simulation_speed()
self.ui.dataStockFinishedProductsNoVariation.setText(str(self.stock_level_FG_no_variation))
self.ui.dataStockFinishedProductsWithVariation.setText(str(self.stock_level_FG_with_variation))
class ProductionProcess(QThread):
def __init__(self):
QThread.__init__(self)
self.iteration = 0
def __del__(self):
self.wait()
def run(self):
while self.iteration < 5:
print(self.iteration)
self.iteration = self.iteration + 1
#self.machine_one_no_variation_status.emit(random.randint(0,100))
self.sleep(2)