简体   繁体   English

如何将值从 Pyqt5 GUI 传递给多个进程

[英]How to pass values from Pyqt5 GUI to multiple processes

I have the following code:我有以下代码:

from PyQt5 import uic, QtWidgets
import os
import sys
import threading
from multiprocessing import Pool

def fetchdata(num):
    message = window.lineEdit()
    print(message)

def processchain():
    p=Pool(processes=15)
    data=p.map(fetchdata,range(1,1000))

def alltask():
    x = threading.Thread(target=processchain)
    x.start()

if __name__ == '__main__':

    y = os.getcwd()

    app=QtWidgets.QApplication(sys.argv)
    window = uic.loadUi(os.path.normpath(y)+ "\\" + "estate.ui")
    window.pushButton.clicked.connect(alltask)

    window.show()



    sys.exit(app.exec_())

The fetchdata function being multiprocessed has a line input = window.lineEdit.text() which gives an error saying the window variable isnt defined.被多处理的fetchdata函数有一行input = window.lineEdit.text() ,它给出了一个错误,说没有定义窗口变量。 Does it mean I have to pass the window variable to the thread in the alltask function then pass it from the thread to the processchain function which then passes it on to all the multiprocesses?这是否意味着我必须到窗口变量传递给在该线程alltask功能,那么从线程传递到processchain功能,然后将其传递给所有的multiprocesses? Seems like a pain and am hoping there is an easier way似乎很痛苦,我希望有更简单的方法

How I can have the GUI objects available to every thread and process?如何让 GUI 对象可用于每个线程和进程? Below is the code for reproducing the estate.ui file:下面是用于复制 Estate.ui 文件的代码:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>640</width>
    <height>186</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>370</x>
      <y>30</y>
      <width>151</width>
      <height>71</height>
     </rect>
    </property>
    <property name="text">
     <string>ENTER</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>49</y>
      <width>191</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>60</y>
      <width>47</width>
      <height>13</height>
     </rect>
    </property>
    <property name="text">
     <string>INPUT</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>640</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

The GUI should not be accessed from another thread or other process, what should be done is to send the text information at the time of starting the thread and the processes: GUI不应从另一个线程或其他进程访问,应该做的是在启动线程和进程时发送文本信息:

from multiprocessing import Pool
import os
import sys
import threading

from PyQt5 import uic, QtWidgets


def fetchdata(value):
    num, message = value
    print(num, message)
    return True


def processchain(message):
    p = Pool(processes=15)
    data = p.map(fetchdata, [(i, message) for i in range(1, 1000)])
    print("results:", data)


def alltask(message):
    threading.Thread(target=processchain, args=(message,), daemon=True).start()


if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)
    ui_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "estate.ui")
    window = uic.loadUi(ui_filename)

    def on_clicked():
        message = window.lineEdit.text()
        alltask(message)

    window.pushButton.clicked.connect(on_clicked)
    window.show()

    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM