2014-03-06 30 views
0

我有一個程序,我已經在控制檯中成功地工作,我想添加GUI。但是,當我試圖添加它們時,mainloop()之後的代碼都沒有運行(除非我關閉了Tkinter窗口)。我搜索了它,發現this SO question。但是,我真的不知道JAB在回答這個問題。 =)在其自己的線程或進程中運行`mainloop()`?

我需要做些什麼才能使mainloop()之後的代碼運行?僅適用於上下文,這裏是我的代碼看起來像現在:

from Tkinter import * 
from tkFileDialog import * 
import os.path 
import shutil 
import sys 
import tempfile 
from zipfile import ZipFile 
import subprocess 

root = Tk() 
root.wm_title("Pages to PDF") 
root.wm_iconbitmap('icon.ico') 
w = Label(root, text="Please choose a .pages file to convert.") 
def callback(): 
    print "click!" 
    global y 
    y = askopenfilename(parent=root, defaultextension=".pages") 
    #print(y) 
    #y.pack() 
b = Button(root, text="Browse", command=callback) 
w.pack() 
b.pack() 
root.mainloop() 

def view_file(filepath): 
    subprocess.Popen(filepath, shell=True).wait() 

PREVIEW_PATH = 'QuickLook/Preview.pdf' # archive member path 
#pages_file = raw_input('Enter the path to the .pages file in question: ') 
pages_file = y 
pages_file = os.path.abspath(pages_file) 
filename, file_extension = os.path.splitext(pages_file) 
if file_extension == ".pages": 
    tempdir = tempfile.gettempdir() 
    temp_filename = os.path.join(tempdir, PREVIEW_PATH) 
    with ZipFile(pages_file, 'r') as zipfile: 
     zipfile.extract(PREVIEW_PATH, tempdir) 
    if not os.path.isfile(temp_filename): # extract failure? 
     sys.exit('unable to extract {} from {}'.format(PREVIEW_PATH, pages_file)) 
    final_PDF = filename + '.pdf' 
    shutil.copy2(temp_filename, final_PDF) # copy and rename extracted file 
    # delete the temporary subdirectory created (along with pdf file in it) 
    shutil.rmtree(os.path.join(tempdir, os.path.split(PREVIEW_PATH)[0])) 
    print('Check out the PDF! It\'s located at "{}".'.format(final_PDF)) 
    view_file(final_PDF) # see Bonus below 
else: 
    sys.exit('Sorry, that isn\'t a .pages file.') 

我想要的GUI保持開放的原因是,我想最終顯示現在是終端輸出的GUI部分。

回答

1

你需要做的是基本形成一個類(它是一個更好的做法)&調用這個類在mainloop

from Tkinter import * 
from tkFileDialog import * 
import os.path 
import shutil 
import sys 
import tempfile 
from zipfile import ZipFile 
import subprocess 


class uiclass(): 
    def __init__(self,root): 
     b = Button(root, text="Browse", command=self.callback) 
     w = Label(root, text="Please choose a .pages file to convert.") 
     w.pack() 
     b.pack() 

    def callback(self): 
     print "click!" 
     global y 
     y = askopenfilename(parent=root, defaultextension=".pages") 
     self.view_file(y) 



    def view_file(self,filepath): 
     subprocess.Popen(filepath, shell=True).wait() 

     PREVIEW_PATH = 'QuickLook/Preview.pdf' # archive member path 
     #pages_file = raw_input('Enter the path to the .pages file in question: ') 
     pages_file = y 
     pages_file = os.path.abspath(pages_file) 
     filename, file_extension = os.path.splitext(pages_file) 
     if file_extension == ".pages": 
      tempdir = tempfile.gettempdir() 
      temp_filename = os.path.join(tempdir, PREVIEW_PATH) 
      with ZipFile(pages_file, 'r') as zipfile: 
       zipfile.extract(PREVIEW_PATH, tempdir) 
      if not os.path.isfile(temp_filename): # extract failure? 
       sys.exit('unable to extract {} from {}'.format(PREVIEW_PATH, pages_file)) 
      final_PDF = filename + '.pdf' 
      shutil.copy2(temp_filename, final_PDF) # copy and rename extracted file 
      # delete the temporary subdirectory created (along with pdf file in it) 
      shutil.rmtree(os.path.join(tempdir, os.path.split(PREVIEW_PATH)[0])) 
      print('Check out the PDF! It\'s located at "{}".'.format(final_PDF)) 
      self.view_file(final_PDF) # see Bonus below 
     else: 
      sys.exit('Sorry, that isn\'t a .pages file.') 

if __name__ == '__main__': 
    root = Tk() 
    uiclass(root) 
    root.wm_title("Pages to PDF") 
    root.mainloop() 

呼叫方法本身的其他方法是什麼。

+0

哦不!即使在PDF開啓器後面添加了一個'sys.exit',它無限地打開PDF! – evamvid

+0

它在我的工作很好......你做了一些改變嗎? – Gogo

+1

它可能是因爲你正在'else'上遞歸調用'view_file'。評論該部分&再試一次 – Gogo

相關問題