1. luoboiqingcai
  2. downloadduxiu

Commits

luoboiqingcai  committed 85e5a9b

show message on the gui window

  • Participants
  • Parent commits 5e42604
  • Branches default

Comments (0)

Files changed (1)

File gui.py

View file
 import tkFileDialog
 from downloadduxiu import (Downloadduxiu,Downloadchaoxing)
 import threading
+import logging
 import os
+from StringIO import StringIO
 
 class Application(Frame):
     """main class"""
         #self.CANCEL['command'] = self.quit
         self.CANCEL['command'] = q
         self.CANCEL.grid(row=4,column=2)
-
         self.OK = Button(self,text='OK')
         self.OK['text'] = 'OK'
         self.OK['command'] = self.doit
         self.OK.grid(row = 4,column=1)
+        self.statusarea = Text(self,width=50,height=10,takefocus=0)
+        self.statusarea.insert(CURRENT,"欢迎使用读秀文献传递下载器\n 访问 https://bitbucket.org/luoboiqingcai/downloadduxiu 下载最新版本。")
+        self.statusarea.grid(row=5,column=1,columnspan=2)
 
 
     def doit(self):
             prex = 'xiazai'
         prex += os.sep
         logfile = 'logfile'
+        class StatusVar(StringIO):
+            def __init__(self,statusarea):
+                StringIO.__init__(self)
+                self.statusarea = statusarea
+            def write(self,some):
+                self.statusarea.insert("1.0",some)
+            def getvalue(self):
+                return self.statusarea.get(START)
+            def close(self):
+                self.closed = True
         if self.type_.get() == 0: # download duxiu
             def work():
                 count = threading.active_count()
                 if count > 2:
                     return
-                duxiu = Downloadduxiu(lnp=lnp,pattern=pattern_for_duxiu_r,img_url_prex=img_url_prex,prex=prex,logfile=logfile)
-                duxiu.downloadduxiu(True,self.url.get())
-                self.url.set('Finished.')
+                try:
+                    duxiu = Downloadduxiu(lnp=lnp,pattern=pattern_for_duxiu_r,img_url_prex=img_url_prex,prex=prex,logfile=logfile)
+                    duxiu.log.addHandler(logging.StreamHandler(StatusVar(self.statusarea)))
+                    duxiu.downloadduxiu(True,self.url.get())
+                except Exception,e:
+                    self.statusarea.insert(END,"%s"%e)
+                else:
+                    self.url.set('Finished.')
+                    self.statusarea.insert(END,"下载完成")
         else: # download chaoxing
             print "download chaoxing.."
             def work():
                 count = threading.active_count()
                 if count > 2: # run in another thread to avoid interrupt the guid thread,and prevent user clicking download multiple times.
                     return
-                chaoxing = Downloadchaoxing(lnp=lnp,pattern=pattern_for_chaoxing,img_url_prex=chaoxing_img_url_prex,prex=prex,logfile=logfile)
-                chaoxing.downloadchaoxing(False,self.url.get())
-                self.url.set('Finished.')
+                try:
+                    chaoxing = Downloadchaoxing(lnp=lnp,pattern=pattern_for_chaoxing,img_url_prex=chaoxing_img_url_prex,prex=prex,logfile=logfile)
+                    duxiu.log.addHandler(logging.StreamHandler(StatusVar(self.statusarea)))
+                    chaoxing.downloadchaoxing(False,self.url.get())
+                except Exception,e:
+                    self.statusarea.insert(END,"%s"%e)
+                finally:
+                    self.url.set('Finished.')
+                    self.statusarea.insert(END,"下载完成")
         th = threading.Thread(target=work)
         th.start()