将每个进程的输出重定向到 QtabWidget 选项卡
Redirect output of each process to QtabWidget tabs
任何人都可以帮助修复此脚本
我想将每个进程输出分成 tab1 和 tab2。
tab1 输出类似于 enter image description here
tab2 输出类似于 enter image description here
有什么方法可以分离进程输出
"""
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from multiprocessing import Process, Queue
import multiprocessing as mp
import datetime
import time
“我创建了 2 个函数,每个函数都生成日期和数字”
def producer(q):
proc = mp.current_process()
while True:
now = datetime.datetime.now()
data = str(now)
q.put(data)
time.sleep(1)
def counting(q):
proc = mp.current_process()
count = 1
while True:
q.put(str(count))
time.sleep(1)
count += 1
class Consumer(QThread):
poped = pyqtSignal(str)
def __init__(self, q):
super().__init__()
self.q = q
def run(self):
while True:
if not self.q.empty():
data = q.get()
self.poped.emit(data)
“标签在这里”
class MyWindow(QMainWindow):
def __init__(self, q):
super().__init__()
self.setGeometry(200, 200, 300, 200)
self.layout = QVBoxLayout(self)
# Initialize tab screen
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.tabs.resize(300, 200)
# Add tabs
self.tabs.addTab(self.tab1, "Tab1")
self.tabs.addTab(self.tab2, "Tab2")
self.tabs.addTab(self.tab3, "Tab3")
# Create first tab
self.tab1.layout = QVBoxLayout(self)
self.tab2.layout = QVBoxLayout(self)
self.text = QPlainTextEdit()
self.text.setReadOnly(True)
self.text2 = QPlainTextEdit()
self.text.setReadOnly(True)
self.tab1.layout.addWidget(self.text)
self.tab1.setLayout(self.tab1.layout)
self.tab2.layout.addWidget(self.text2)
self.tab2.setLayout(self.tab2.layout)
# Add tabs to widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
self.setCentralWidget(self.tabs)
# thread for data consumer
self.consumer = Consumer(q)
self.consumer.poped.connect(self.print_data)
self.consumer.start()
@pyqtSlot(str)
def print_data(self, data):
self.statusBar().showMessage(data)
self.text.appendPlainText(data)
self.text2.appendPlainText(data)
if __name__ == "__main__":
q = Queue()
# producer process
p = Process(name="producer", target=producer, args=(q, ), daemon=True)
p.start()
p2 = Process(name="producer", target=counting, args=(q, ), daemon=True)
p2.start()
# Main process
app = QApplication(sys.argv)
mywindow = MyWindow(q)
mywindow.show()
app.exec_()
"""
有许多不同的方法可以实现您想要的。
此解决方案使用 header 作为数据标识符。
然后你可以根据你的需要解释你的数据 header.
这是您修改后的代码:
import datetime
import sys
import time
from multiprocessing import Process, Queue
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
def producer(q, header):
while True:
data = str(datetime.datetime.now())
q.put((header, data))
time.sleep(1)
def counting(q, header):
count = 1
while True:
data = str(count)
q.put((header, data))
time.sleep(1)
count += 1
class Consumer(QThread):
poped = pyqtSignal(int, str)
def __init__(self, q):
super().__init__()
self.q = q
def run(self):
while True:
if not self.q.empty():
self.poped.emit(*q.get())
class MyWindow(QMainWindow):
def __init__(self, q):
super().__init__()
self.setGeometry(200, 200, 300, 200)
self.layout = QVBoxLayout(self)
# Initialize tab screen
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.tabs.resize(300, 200)
# Add tabs
self.tabs.addTab(self.tab1, "Tab1")
self.tabs.addTab(self.tab2, "Tab2")
self.tabs.addTab(self.tab3, "Tab3")
# Create first tab
self.tab1.layout = QVBoxLayout(self)
self.tab2.layout = QVBoxLayout(self)
self.text = QPlainTextEdit()
self.text.setReadOnly(True)
self.text2 = QPlainTextEdit()
self.text.setReadOnly(True)
self.tab1.layout.addWidget(self.text)
self.tab1.setLayout(self.tab1.layout)
self.tab2.layout.addWidget(self.text2)
self.tab2.setLayout(self.tab2.layout)
# Add tabs to widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
self.setCentralWidget(self.tabs)
# thread for data consumer
self.consumer = Consumer(q)
self.consumer.poped.connect(self.print_data)
self.consumer.start()
@pyqtSlot(int, str)
def print_data(self, header, data):
self.statusBar().showMessage(data)
if header == 0:
self.text.appendPlainText(data)
elif header == 1:
self.text2.appendPlainText(data)
if __name__ == "__main__":
q = Queue()
# Create producer with header=0
p = Process(name="producer", target=producer, args=(q, 0), daemon=True)
p.start()
# Create producer with header=1
p2 = Process(name="producer", target=counting, args=(q, 1), daemon=True)
p2.start()
# Main process
app = QApplication(sys.argv)
mywindow = MyWindow(q)
mywindow.show()
app.exec_()
任何人都可以帮助修复此脚本 我想将每个进程输出分成 tab1 和 tab2。
tab1 输出类似于 enter image description here
tab2 输出类似于 enter image description here
有什么方法可以分离进程输出
"""
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from multiprocessing import Process, Queue
import multiprocessing as mp
import datetime
import time
“我创建了 2 个函数,每个函数都生成日期和数字”
def producer(q):
proc = mp.current_process()
while True:
now = datetime.datetime.now()
data = str(now)
q.put(data)
time.sleep(1)
def counting(q):
proc = mp.current_process()
count = 1
while True:
q.put(str(count))
time.sleep(1)
count += 1
class Consumer(QThread):
poped = pyqtSignal(str)
def __init__(self, q):
super().__init__()
self.q = q
def run(self):
while True:
if not self.q.empty():
data = q.get()
self.poped.emit(data)
“标签在这里”
class MyWindow(QMainWindow):
def __init__(self, q):
super().__init__()
self.setGeometry(200, 200, 300, 200)
self.layout = QVBoxLayout(self)
# Initialize tab screen
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.tabs.resize(300, 200)
# Add tabs
self.tabs.addTab(self.tab1, "Tab1")
self.tabs.addTab(self.tab2, "Tab2")
self.tabs.addTab(self.tab3, "Tab3")
# Create first tab
self.tab1.layout = QVBoxLayout(self)
self.tab2.layout = QVBoxLayout(self)
self.text = QPlainTextEdit()
self.text.setReadOnly(True)
self.text2 = QPlainTextEdit()
self.text.setReadOnly(True)
self.tab1.layout.addWidget(self.text)
self.tab1.setLayout(self.tab1.layout)
self.tab2.layout.addWidget(self.text2)
self.tab2.setLayout(self.tab2.layout)
# Add tabs to widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
self.setCentralWidget(self.tabs)
# thread for data consumer
self.consumer = Consumer(q)
self.consumer.poped.connect(self.print_data)
self.consumer.start()
@pyqtSlot(str)
def print_data(self, data):
self.statusBar().showMessage(data)
self.text.appendPlainText(data)
self.text2.appendPlainText(data)
if __name__ == "__main__":
q = Queue()
# producer process
p = Process(name="producer", target=producer, args=(q, ), daemon=True)
p.start()
p2 = Process(name="producer", target=counting, args=(q, ), daemon=True)
p2.start()
# Main process
app = QApplication(sys.argv)
mywindow = MyWindow(q)
mywindow.show()
app.exec_()
"""
有许多不同的方法可以实现您想要的。
此解决方案使用 header 作为数据标识符。
然后你可以根据你的需要解释你的数据 header.
这是您修改后的代码:
import datetime
import sys
import time
from multiprocessing import Process, Queue
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
def producer(q, header):
while True:
data = str(datetime.datetime.now())
q.put((header, data))
time.sleep(1)
def counting(q, header):
count = 1
while True:
data = str(count)
q.put((header, data))
time.sleep(1)
count += 1
class Consumer(QThread):
poped = pyqtSignal(int, str)
def __init__(self, q):
super().__init__()
self.q = q
def run(self):
while True:
if not self.q.empty():
self.poped.emit(*q.get())
class MyWindow(QMainWindow):
def __init__(self, q):
super().__init__()
self.setGeometry(200, 200, 300, 200)
self.layout = QVBoxLayout(self)
# Initialize tab screen
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.tabs.resize(300, 200)
# Add tabs
self.tabs.addTab(self.tab1, "Tab1")
self.tabs.addTab(self.tab2, "Tab2")
self.tabs.addTab(self.tab3, "Tab3")
# Create first tab
self.tab1.layout = QVBoxLayout(self)
self.tab2.layout = QVBoxLayout(self)
self.text = QPlainTextEdit()
self.text.setReadOnly(True)
self.text2 = QPlainTextEdit()
self.text.setReadOnly(True)
self.tab1.layout.addWidget(self.text)
self.tab1.setLayout(self.tab1.layout)
self.tab2.layout.addWidget(self.text2)
self.tab2.setLayout(self.tab2.layout)
# Add tabs to widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
self.setCentralWidget(self.tabs)
# thread for data consumer
self.consumer = Consumer(q)
self.consumer.poped.connect(self.print_data)
self.consumer.start()
@pyqtSlot(int, str)
def print_data(self, header, data):
self.statusBar().showMessage(data)
if header == 0:
self.text.appendPlainText(data)
elif header == 1:
self.text2.appendPlainText(data)
if __name__ == "__main__":
q = Queue()
# Create producer with header=0
p = Process(name="producer", target=producer, args=(q, 0), daemon=True)
p.start()
# Create producer with header=1
p2 = Process(name="producer", target=counting, args=(q, 1), daemon=True)
p2.start()
# Main process
app = QApplication(sys.argv)
mywindow = MyWindow(q)
mywindow.show()
app.exec_()