M
Commentssleep(3)I thought I should remove it, but it is appropriateforLanguagecalc()The same omena occurred when the function is executed. Like me,forI think that the PyQt5のイベント event will be held by running a statement or while loop. WhenQtThen you can find a convenient way to put another calculation on another process.See the following code:Key pointsQtWidgets.QApplication.processEvents()HomeThe program can do everything in a single process and enter a loop to make calculations,In the meantime, the user must wait for the process to end. But with this code,Only the calculation process can be executed in a separate process, and it is more than done in theの processIt is possible to process処理 with overwhelming speed. I want to suck huge data from a fileIt is very important for time. Please check the difference of processing if there is no case.As a result, I can do this, but I didn't have two rules to write.HOME OtherQThreadAlso, I think that it is for multi-thread processing such as this.import sys
from PyQt5 import*
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtGui,QtCore, QtWidgets
import time
from time import sleep
class Second(QtWidgets.QWidget):
def init(self, parent=None):
super(Second, self).init(parent)
#Setting a title, locating and sizing the window
self.title = 'My Second Window'
self.left = 200
self.top = 200
self.width = 500
self.height = 500
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
lbl = QtWidgets.QLabel("計算中")
layout1=QHBoxLayout()
layout1.addWidget(lbl)
self.setLayout(layout1)
class First(QtWidgets.QWidget):
def init(self, parent=None):
super(First, self).init(parent)
self.title = 'My First Window'
self.left = 100
self.top = 100
self.width = 500
self.height = 500
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.pushButton = QtWidgets.QPushButton("Open Me", self)
self.pushButton2 = QtWidgets.QPushButton("close", self)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.pushButton2.clicked.connect(self.on_pushButton2_clicked)
self.newWindow = Second()
self.newWindow.setAttribute(QtCore.Qt.WA_ShowWithoutActivating)
#self.timer = QBasicTimer()
#self.step=0
#self.calctime=1000
layout1=QHBoxLayout()
layout1.addWidget(self.pushButton)
layout1.addWidget(self.pushButton2)
self.setLayout(layout1)
def on_pushButton_clicked(self):
self.newWindow.show()
QtWidgets.QApplication.processEvents()
j = self.calc()
QtWidgets.QApplication.processEvents()
self.newWindow.close()
print("the result of calculation is",j)
def calc(self):
j = 0
for i in range(100000):
j += i
return j
def on_pushButton2_clicked(self):
self.newWindow.close()
if name == 'main':
app = QtWidgets.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
PreviousToma's code is the widget display,calcStop code execution for 3 seconds with a function.andcloseThis means that the widget you want to show your heart is stopped for 3 seconds. Please rewrite the code as below.QtCore.QTimerExamplePySideIt is information from, but I think it works as intended.Reference https://pyside.github.io/docs/pyside/PySide/QtCore/QTimer.html#PySide.QtCore.PySide.QtCore.QTimer.setInterval Homeimport sys
from PyQt5 import*
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtGui,QtCore, QtWidgets
import time
from time import sleep
class Second(QtWidgets.QWidget):
def init(self, parent=None):
super(Second, self).init(parent)
#Setting a title, locating and sizing the window
self.title = 'My Second Window'
self.left = 200
self.top = 200
self.width = 500
self.height = 500
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
lbl = QtWidgets.QLabel("計算中")
layout1=QHBoxLayout()
layout1.addWidget(lbl)
self.setLayout(layout1)
class First(QtWidgets.QWidget):
def init(self, parent=None):
super(First, self).init(parent)
self.title = 'My First Window'
self.left = 100
self.top = 100
self.width = 500
self.height = 500
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.pushButton = QtWidgets.QPushButton("Open Me", self)
self.pushButton2 = QtWidgets.QPushButton("close", self)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.pushButton2.clicked.connect(self.on_pushButton2_clicked)
self.newWindow = Second()
#self.timer = QBasicTimer()
#self.step=0
#self.calctime=1000
layout1=QHBoxLayout()
layout1.addWidget(self.pushButton)
layout1.addWidget(self.pushButton2)
self.setLayout(layout1)
def on_pushButton_clicked(self):
self.newWindow.show()
self.calc()
def calc(self):
QtCore.QTimer.singleShot(3000,self.newWindow.close)
def on_pushButton2_clicked(self):
self.newWindow.close()
if name == 'main':
app = QtWidgets.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())