首页 > tkinter调用线程后UI卡死?

tkinter调用线程后UI卡死?

python写了个自动下载ftp文件程序,在tkinter里面加了个按钮,点击执行下载,执行过程中把信息打印到tkinter主界面的text框里,执行过程会卡死,可是我明明用的线程啊,上代码:

class DownloadFtp(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.ftp = FTP()
        self.timeout = 30
        self.port = 21
        self.host = '………………'
        self.user = '……'
        self.password = '……'

    def ftp_connect(self):
        try:
            self.ftp.connect(self.host, self.port, self.timeout)
            self.ftp.login(self.user, self.password)
            text.insert(END, self.ftp.getwelcome() + '\n')
            self.ftp.cwd('……/……')
        except:
            traceback.print_exc()
        download_ftp()

    def start_download(self):
        self.ftp_connect()
        ftp_list = self.ftp.nlst()
        for name in ftp_list:
            text.insert(END, u'获得文件:' + name.decode('gb2312')+'\n')
            if not os.path.exists('py_auto_download/' + name):
                path = 'py_auto_download/' + name
                f = open(path, 'wb')
                filename = 'RETR ' + name
                text.insert(END, u'正在下载:' + name.decode('gb2312')+'\n')
                self.ftp.retrbinary(filename, f.write)
            else:
                text.insert(END, u'文件或者文件夹已存在,忽略\n')
        self.ftp.quit()

def button_ftp():
    if not os.path.exists('py_auto_download'):
        os.makedirs('py_auto_download')
    my_ftp = DownloadFtp()
    my_ftp.start()
    my_ftp.start_download()
    
if __name__ == '__main__':
    root = Tkinter.Tk()
    root.title(u'自助操作端升级')
    root.geometry('300x400')
    root.resizable(width=False, height=False)
    text = Text(root)
    text.pack(side=TOP)

    frameButton = Frame(root)

    frameLButton = Frame(frameButton)
    buttonESQL = Button(frameLButton, text=u'升级本地SQL数据库', command=button_exec_sql)
    buttonESQL.pack(side=LEFT)
    frameLButton.pack(side=LEFT)

    frameRButton = Frame(frameButton)
    buttonFTP = Button(frameRButton, text=u'下载自助操作端升级包', command=button_ftp)
    buttonFTP.pack(side=RIGHT)
    frameRButton.pack(side=RIGHT)

    frameButton.pack()

    root.mainloop()

挂了将近一天,自己解决了
把def start_download(self):方法改成def run(self):设置进程默认执行的方法
然后把my_ftp.start_download()这种调用去掉

继承threading.Thread的话肯定要重写run嘛。。不然你的start()咋运行

【热门文章】
【热门文章】