python线程卡死问题解决_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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值