Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Pyqt进行多处理_Python_Pyqt_Multiprocessing_Pyqt5 - Fatal编程技术网

Python 如何使用Pyqt进行多处理

Python 如何使用Pyqt进行多处理,python,pyqt,multiprocessing,pyqt5,Python,Pyqt,Multiprocessing,Pyqt5,我在使用pyqt的多处理时遇到了问题,因为它打开了多个窗口,并且没有真正超出目标函数。我在Qt Designer中创建了接口,示例代码如下: from multiprocessing import Pool from PyQt5 import uic, QtWidgets from PyQt5.QtWidgets import * import sys def updater(num): print(num) def main_tracker(): p = Pool(proc

我在使用pyqt的多处理时遇到了问题,因为它打开了多个窗口,并且没有真正超出目标函数。我在Qt Designer中创建了接口,示例代码如下:

from multiprocessing import Pool
from PyQt5 import uic, QtWidgets
from PyQt5.QtWidgets import *
import sys

def updater(num):
    print(num)

def main_tracker():
    p = Pool(processes=4)
    data = p.map(updater, range(0, 100))

app=QtWidgets.QApplication(sys.argv)
window = uic.loadUi("tracker.ui")
window.pushButton.clicked.connect(main_tracker)
window.show()
sys.exit(app.exec_())
在运行时,界面会正常打开,但当我单击gui上的按钮时,它只会打开多个pyqt窗口,而不会按预期运行功能。我如何才能让它工作,使多处理工作,而不打开多个窗口?我在这里看到过类似的问题,但我还没有找到解决我问题的方法

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>365</width>
    <height>134</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>69</x>
      <y>19</y>
      <width>173</width>
      <height>59</height>
     </rect>
    </property>
    <property name="text">
     <string>PushButton</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>365</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
我有两点意见。 始终使用

如果uuuu name uuuuu==\uuuuuuuu main\uuuuuuuu: 代码在这里 主文件的构造。在您的情况下,它将是:

来自多处理导入池 从PyQt5导入uic、QtWidgets 从PyQt5.QtWidgets导入* 导入系统 def updaternum: printnum def主_跟踪器: p=4 data=p.mapupdater,范围0,100 如果uuuu name uuuuu==\uuuuuuuu main\uuuuuuuu: app=qtwidts.QApplicationsys.argv window=uic.loadUitracker.ui window.butdown.clicked.connectmain\u跟踪器 橱窗秀 sys.exitapp.exec_ 如果您想冻结您的应用程序,请在windows上创建exec文件 将这些行添加到您的代码中,或者它可能会修复您的错误

导入多处理 多处理.U支持
否则,您将再次遇到多窗口问题

我创建了自己的tracker.ui,原因很明显,我没有看到任何窗口打开,除了多处理工作正常外,您还可以提供。ui@eyllanesc当然,我已经编辑了我的问题,并为ui文件添加了生成的代码。对于我来说,它工作正常,我建议您在CMD/Terminal中运行它,以排除IDE的问题。@Eylandesc它仍然执行相同的操作,只打开多个窗口而不打印任何内容。我的文件名为testfile.py,我在cmd上以python testfile.py的形式运行它。同样的问题甚至只是双击文件。在我用VS代码运行它之前,检查一下需求。你认为在这种情况下,gui可以作为一个单独的线程运行吗?我对线程非常陌生,我所有的尝试都是徒劳的,因为QtGUI需要在主线程中运行。但您可以将一些计算委托给工作线程。您可以将python线程用于经典案例,或将QThread用于支持信号的计算。