0

I have a program that I have working successfully in the console, and I wanted to add GUI. When, I tried to add them, however, none of the code after mainloop() ran (unless I closed the Tkinter window). I googled it and found this SO question. But, I honestly have no idea what JAB is talking about in that answer. =)

What do I need to do to the code to make the code after the mainloop() run? Just for context, here's what my code looks like right now:

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.')

The reason I want the GUI to stay open is that I want to eventually display the parts that are terminal output right now in the GUI.

Community
  • 1
  • 1
evamvid
  • 831
  • 6
  • 18
  • 40

1 Answers1

1

What you need to do is basically form a class(its a better practice) & call that class in the 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()

call the other methods in the methods itself.

Alok
  • 2,629
  • 4
  • 28
  • 40
  • Oh No! It's infinetely opening the PDF, even after I added a `sys.exit` after the PDF opener! – evamvid Mar 06 '14 at 04:56
  • its doing just fine in mine...did you make some changes? – Alok Mar 06 '14 at 04:57
  • 1
    its maybe because you are calling `view_file` recursively just above `else`. comment that part & try again – Alok Mar 06 '14 at 04:59
  • That works, but now it doesn't open the PDF at all. How do I get it to open the PDF once? – evamvid Mar 06 '14 at 05:02
  • what is this `.pages` extension? cuz here i'm just opening `.pdf` files with it & its working absolutely fine – Alok Mar 06 '14 at 05:04
  • It's an Apple Pages file. The script converts it into a PDF (because MS Word can't read it on a Windows system) – evamvid Mar 06 '14 at 05:05
  • What happens for me when I plug in an .pages file is that it opens it (the generated PDF) over and over, or when I commented out the `view_file`, it doesn't open it at all. The program takes in a .pages file and puts out a PDF (which should display on the screen). – evamvid Mar 06 '14 at 05:10
  • that means your `file_extension` variable is still `.pages` after the conversion & its entering the `if` loop again & again.you need to change it to `.pdf` after converison – Alok Mar 06 '14 at 05:14
  • do tell me if you encounter any problem – Alok Mar 06 '14 at 05:20