代码之家  ›  专栏  ›  技术社区  ›  West

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

  •  0
  • West  · 技术社区  · 5 年前

    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_())
    

    这个 fetchdata 正在进行多处理的函数有一行 input = window.lineEdit.text() 这会给出一个错误,说明没有定义窗口变量。这是否意味着我必须将window变量传递给 alltask processchain 然后传递给所有多进程的函数?看起来很痛苦,我希望有一个更简单的方法

    如何使GUI对象对每个线程和进程都可用?下面是用于复制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>
    
    1 回复  |  直到 5 年前
        1
  •  1
  •   eyllanesc    5 年前

    不应该从其他线程或其他进程访问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_())