Pyqt使用QThread,但GUI仍然没有响应

2024-10-02 10:19:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将文件从FTP下载到网络共享文件夹(文件大小可能是500mb或更大),但每次单击“开始”时,GUI将显示“没有响应”,即使使用QThread

我做错什么了吗?在

在主.py在

# -*- coding: utf-8 -*-
from PyQt4 import QtGui
import ftp100



class main_windows(QtGui.QWidget):
    def __init__(self):
        super(main_windows, self).__init__()
        self._count = 0
        self.Ftpallobject = []

    def init(self):

        #PASS SOME GUI CODE

        button_go = QtGui.QPushButton('GO')
        button_go.clicked.connect(self.Ftpstart)

        self.fp = ftp100.ftpmethod()
        self.fp.requestSignal_getinfo.connect(self.Ftpallobject)



    def SendFtpInfo(self):
        self.fp.update_getinfo.emit(self.allobject)

    def Ftpstart(self):
        self.fp.run()

在ftp.py文件在

^{pr2}$

Tags: 文件pyimportselfgoinitmainwindows
1条回答
网友
1楼 · 发布于 2024-10-02 10:19:07

你确实做错了。在

现在要做的是直接调用run-方法,而不是调用start(),因此正确的实现应该是:

def Ftpstart(self):
    self.fp.start()

当子类化QThread(或普通python线程)时,实现run-方法,它表示线程应该做的工作,如果直接调用它,则在current线程中执行该代码(在本例中是主线程)。这就是为什么您的GUI将变得无响应。在

{{cd2>如果一个线程不存在,那么它将首先调用一个新的。来自PyQT Documentation

QThread.start (self, Priority priority = QThread.InheritPriority)

Begins execution of the thread by calling run(). The operating system will schedule the thread according to the priority parameter. If the thread is already running, this function does nothing.

对于run()

QThread.run (self)

The starting point for the thread. After calling start(), the newly created thread calls this function.

相关问题 更多 >

    热门问题