gpt4 book ai didi

python - PyQt4 如何通过单击按钮使用多处理

转载 作者:太空宇宙 更新时间:2023-11-04 05:19:23 25 4
gpt4 key购买 nike

我一直在尝试使用多进程运行命令,因为在使用 while 循环时 GUI 会卡住。我需要调用 pyqt4 类中的函数。或者更好的处理多进程的方法 Qthread 会帮助我吗?我已经搜索了很多教程,但我不知道该怎么做。

我试过了,效果不错。问题是我无法将 QeditText 的输入传递给该函数,如果有办法的话,它会为我想做的事情工作。

import sys
import multiprocessing
import time
from PyQt4 import QtCore, QtGui
from form import Ui_Dialog


def worker():
t = MyDialog()
name = multiprocessing.current_process().name
print name, 'Starting', t.self.ui.rtmpIN.toPlainText()
time.sleep(2)
print name, 'Exiting'

class MyDialog(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.startButton.clicked.connect(self.start)
self.ui.stopButton.clicked.connect(self.stop)
self.ui.comboBox.addItem("player 1")
self.ui.comboBox.addItem("player 2")
self.ui.comboBox.addItem("player 3")
#self.ui.comboBox.currentIndexChanged.connect(self.selectionchange)

def selectionchange(self,i):
print self.ui.comboBox.currentText()

def start(self):
worker_2 = multiprocessing.Process(target=worker) # use default name
worker_2.start()
print "in: ", self.ui.rtmpIN.toPlainText()
print "out: ", self.ui.outPUT.toPlainText()
print str(self.ui.comboBox.currentText())

if self.ui.quialityBox.isChecked():
q = "Streaming started" + "\n" + "quality: " + self.ui.Setquality.toPlainText() + "\n" + "player: " + str(self.ui.comboBox.currentText())
self.ui.theLog.append(q)
#print self.ui.Setquality.toPlainText()
else:
p = "Streaming" + "\n" + "player: " + str(self.ui.comboBox.currentText()) + "\n"
self.ui.theLog.append(p)

def stop(self):
print 'stop pressed.'
self.close()




if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyDialog()
myapp.show()
sys.exit(app.exec_())

我需要能够从中获取数据,在辅助函数中,有什么办法吗?

print "in:  ", self.ui.rtmpIN.toPlainText()
print "out: ", self.ui.outPUT.toPlainText()

编辑:忘记了这里的 form.py http://pastebin.com/HksuSjkt

最佳答案

这是我的解决方案:

from PyQt4.QtCore import QThread

class Worker(QThread):
def __init__(self, parent=None):
super(Worker, self).__init__(parent)
self.textin = ""
self.textout = ""
self.okay = True

def setTextIn(self, text):
self.textin = text

def setTextOut(self, text):
self.textout = text

def run(self):
while self.okay:
print('IN:' + self.textin)
print('OUT:' + self.textout)
time.sleep(2)

def stop(self):
self.okay = False


class MyDialog(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.worker = Worker(self)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.rtmpIN.textChanged.connect(self.changeText)
self.ui.outPUT.textChanged.connect(self.changeText)
self.ui.startButton.clicked.connect(self.start)
self.ui.stopButton.clicked.connect(self.stop)
self.ui.comboBox.addItem("player 1")
self.ui.comboBox.addItem("player 2")
self.ui.comboBox.addItem("player 3")

def selectionchange(self,i):
print(self.ui.comboBox.currentText())

def start(self):
self.worker.start()
print("in: "+self.ui.rtmpIN.toPlainText())
print("out: "+self.ui.outPUT.toPlainText())
print(self.ui.comboBox.currentText())

if self.ui.quialityBox.isChecked():
q = "Streaming started" + "\n" + "quality: " + self.ui.Setquality.toPlainText() + "\n" + "player: " + str(self.ui.comboBox.currentText())
self.ui.theLog.append(q)
else:
p = "Streaming" + "\n" + "player: " + str(self.ui.comboBox.currentText()) + "\n"
self.ui.theLog.append(p)

def changeText(self):
self.worker.setTextIn(self.ui.rtmpIN.toPlainText())
self.worker.setTextOut(self.ui.outPUT.toPlainText())

def stop(self):
self.worker.stop()
self.worker.quit()
self.worker.wait()
print('stop pressed.')
self.close()

def closeEvent(self, event):
self.worker.stop()
self.worker.quit()
self.worker.wait()
QtGui.QDialog.closeEvent(self, event)


if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyDialog()
myapp.show()
sys.exit(app.exec_())

输出:

enter image description here

关于python - PyQt4 如何通过单击按钮使用多处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40825685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com