在自己的线程或进程中运行`mainloop()`?

时间:2014-03-06 04:12:36

标签: python python-2.7 tkinter

我有一个程序,我在控制台中成功运行,我想添加GUI。但是,当我尝试添加它们时,mainloop()之后没有任何代码运行(除非我关闭了Tkinter窗口)。我用Google搜索并找到了this SO question。但是,我真的不知道JAB在那个答案中谈到了什么。 =)

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 个答案:

答案 0 :(得分: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()

调用方法本身的其他方法。