I'm running a console app in Windows with Python 2.7 and Tkinter. I'm using a filebrowser to choose the file. My problem is that between the selection of the file by the user and the script using the path, Windows tries to open it as well. In this case, it's a .pages file, and it tries to open it with Word. I don't want this to happen. How can I stop it>
Just for context, here's the code:
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
sys.exit()
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()
As pointed out by JFSebastian, my problem was that it re-opened the PDF every time the loop ran again (because the file_extension
variable was still set to .pages
. With that in hand, I made some changes to the code that fixed the problem.
Thanks JF!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.