运行 PyQt5 中的多个函数
Running multiple functions in PyQt5
我正在尝试 运行 打印机功能与 loopRun 功能同时运行,但是一旦我 运行 代码(就像现在一样)它首先 运行 s 打印机功能,然后加载 GUI 并执行 loopRun 功能。我在 syncFunctions 函数中尝试了多处理,但它给了我一个错误。 运行 将它们组合在一起的最佳方式是什么? (这是我实际代码的一小部分代码,所以我希望找到一种几乎总是有效的方法)顺便说一句,多线程也不起作用。
import sys
import time
import multiprocessing
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import QTimer
# ----------------------------------------------------
def syncFunctions(func1, args1, func2, args2, optional = None):
p1 = multiprocessing.Process(target = func1, args = args1)
p2 = multiprocessing.Process(target = func2, args = args2)
if __name__ == "__main__":
p1.start()
p2.start()
p1.join()
p2.join()
def singleRun(pb, timer):
pb.setValue(pb.value() + 1)
if pb.value() >= pb.maximum():
timer.stop()
def loopRun(pb, delay):
timer = QTimer(window)
timer.start(delay)
timer.timeout.connect(lambda: singleRun(pb, timer))
def printer(num):
for i in range(num):
print(i)
# ----------------------------------------------------
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("Window")
window.setFixedWidth(1000)
window.setFixedHeight(600)
window.move(100, 50)
window.setStyleSheet(
"background-color: rgb(208, 208, 208);"
)
ProgressBar1 = QtWidgets.QProgressBar(window)
ProgressBar1.setGeometry(400, 50, 200, 24)
ProgressBar1.setProperty("value", 0)
ProgressBar1.setStyleSheet(
"QProgressBar {"
"background-color: rgb(0, 33, 68);"
"font: 11pt \'Trebuchet MS\';"
"font-weight: bold;"
"color: rgb(255, 75, 20);"
"text-align: center;"
"border: 1px solid;"
"border-color: rgb(195, 195, 195);"
"}"
"\n"
"QProgressBar:chunk {"
"background-color: rgb(245, 219, 15);"
"}"
)
loopRun(ProgressBar1, 25)
printer(100)
# syncFunctions(loopRun, [ProgressBar1, 25], printer, [100])
window.show()
sys.exit(app.exec_())
您不应在 Qt 中使用多处理,因为 QObject 不可拾取,因此它们不允许使用此功能。在这种情况下,最好使用线程。另一方面,打印 100 个数字非常快,所以用户观察到的效果是在显示 GUI 之前完成,所以最好创建一个小延迟。
import sys
import time
import threading
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QProgressBar, QWidget
def singleRun(pb, timer):
pb.setValue(pb.value() + 1)
if pb.value() >= pb.maximum():
timer.stop()
def loopRun(pb, delay):
timer = QTimer(window)
timer.start(delay)
timer.timeout.connect(lambda: singleRun(pb, timer))
def printer(num):
for i in range(num):
print(i)
time.sleep(0.1)
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("Window")
window.setFixedWidth(1000)
window.setFixedHeight(600)
window.move(100, 50)
window.setStyleSheet("background-color: rgb(208, 208, 208);")
ProgressBar1 = QProgressBar(window)
ProgressBar1.setGeometry(400, 50, 200, 24)
ProgressBar1.setValue(0)
ProgressBar1.setStyleSheet(
"QProgressBar {"
"background-color: rgb(0, 33, 68);"
"font: 11pt 'Trebuchet MS';"
"font-weight: bold;"
"color: rgb(255, 75, 20);"
"text-align: center;"
"border: 1px solid;"
"border-color: rgb(195, 195, 195);"
"}"
"\n"
"QProgressBar:chunk {"
"background-color: rgb(245, 219, 15);"
"}"
)
loopRun(ProgressBar1, 25)
threading.Thread(target=printer, args=(100,), daemon=True).start()
window.show()
sys.exit(app.exec_())
我正在尝试 运行 打印机功能与 loopRun 功能同时运行,但是一旦我 运行 代码(就像现在一样)它首先 运行 s 打印机功能,然后加载 GUI 并执行 loopRun 功能。我在 syncFunctions 函数中尝试了多处理,但它给了我一个错误。 运行 将它们组合在一起的最佳方式是什么? (这是我实际代码的一小部分代码,所以我希望找到一种几乎总是有效的方法)顺便说一句,多线程也不起作用。
import sys
import time
import multiprocessing
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import QTimer
# ----------------------------------------------------
def syncFunctions(func1, args1, func2, args2, optional = None):
p1 = multiprocessing.Process(target = func1, args = args1)
p2 = multiprocessing.Process(target = func2, args = args2)
if __name__ == "__main__":
p1.start()
p2.start()
p1.join()
p2.join()
def singleRun(pb, timer):
pb.setValue(pb.value() + 1)
if pb.value() >= pb.maximum():
timer.stop()
def loopRun(pb, delay):
timer = QTimer(window)
timer.start(delay)
timer.timeout.connect(lambda: singleRun(pb, timer))
def printer(num):
for i in range(num):
print(i)
# ----------------------------------------------------
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("Window")
window.setFixedWidth(1000)
window.setFixedHeight(600)
window.move(100, 50)
window.setStyleSheet(
"background-color: rgb(208, 208, 208);"
)
ProgressBar1 = QtWidgets.QProgressBar(window)
ProgressBar1.setGeometry(400, 50, 200, 24)
ProgressBar1.setProperty("value", 0)
ProgressBar1.setStyleSheet(
"QProgressBar {"
"background-color: rgb(0, 33, 68);"
"font: 11pt \'Trebuchet MS\';"
"font-weight: bold;"
"color: rgb(255, 75, 20);"
"text-align: center;"
"border: 1px solid;"
"border-color: rgb(195, 195, 195);"
"}"
"\n"
"QProgressBar:chunk {"
"background-color: rgb(245, 219, 15);"
"}"
)
loopRun(ProgressBar1, 25)
printer(100)
# syncFunctions(loopRun, [ProgressBar1, 25], printer, [100])
window.show()
sys.exit(app.exec_())
您不应在 Qt 中使用多处理,因为 QObject 不可拾取,因此它们不允许使用此功能。在这种情况下,最好使用线程。另一方面,打印 100 个数字非常快,所以用户观察到的效果是在显示 GUI 之前完成,所以最好创建一个小延迟。
import sys
import time
import threading
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QProgressBar, QWidget
def singleRun(pb, timer):
pb.setValue(pb.value() + 1)
if pb.value() >= pb.maximum():
timer.stop()
def loopRun(pb, delay):
timer = QTimer(window)
timer.start(delay)
timer.timeout.connect(lambda: singleRun(pb, timer))
def printer(num):
for i in range(num):
print(i)
time.sleep(0.1)
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("Window")
window.setFixedWidth(1000)
window.setFixedHeight(600)
window.move(100, 50)
window.setStyleSheet("background-color: rgb(208, 208, 208);")
ProgressBar1 = QProgressBar(window)
ProgressBar1.setGeometry(400, 50, 200, 24)
ProgressBar1.setValue(0)
ProgressBar1.setStyleSheet(
"QProgressBar {"
"background-color: rgb(0, 33, 68);"
"font: 11pt 'Trebuchet MS';"
"font-weight: bold;"
"color: rgb(255, 75, 20);"
"text-align: center;"
"border: 1px solid;"
"border-color: rgb(195, 195, 195);"
"}"
"\n"
"QProgressBar:chunk {"
"background-color: rgb(245, 219, 15);"
"}"
)
loopRun(ProgressBar1, 25)
threading.Thread(target=printer, args=(100,), daemon=True).start()
window.show()
sys.exit(app.exec_())