因此,我创建了一个程序,该程序旨在使用Python 3.4.4和标准库模块Tkinter将“ .DOCX”扩展文件(即zip文件)转换为“ .TXT”扩展文件。
当我在
Python Shell
中运行此文件时,它工作正常
但是,当我通过'.PYW'
运行带有command line
扩展名的文件时,确实出现了我的窗口,但是一旦完成转换,root
就是withdraws
然后消失
转换文件后,我已经检查了任务管理器,程序仍在运行。尽管始终是焦点,但根源已经缩回。
这是我正在使用的代码:
from tkinter.ttk import Progressbar
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
from zipfile import ZipFile
import shutil
import re
import os
class Console(object):
def __init__(self):
self.lines = []
def output(self, text):
self.update()
self.lines.append(text)
def clear(self):
self.lines = []
def update(self):
if len(self.lines) > 13 or len("".join(self.lines)) > 349:
self.clear()
def text(self):
text = ""
for line in self.lines:
text += ">>> " + line + "\n"
return text
ftypes = [("Word Documents", "*.docx")]
console = Console()
main = tk.Tk()
working_dir = os.getcwd()
main.minsize(500, 400)
main.maxsize(500, 400)
main.update_idletasks()
width = main.winfo_width()
height = main.winfo_height()
x = (main.winfo_screenwidth() // 2) - (width // 2)
y = (main.winfo_screenheight() // 2) - (height // 2)
main.geometry('{}x{}+{}+{}'.format(width, height, x, y))
#main.state("zoomed")
main.overrideredirect(True)
main.configure(background="#333333", width=500, height=500)
def leave():
main.withdraw()
quit()
def console_print(text):
console.output(text)
console_text.configure(text=console.text())
try:
main.deiconify()
except:
pass
def get_file():
main.filename = filedialog.askopenfilename(initialdir="/", title="Select file", filetypes=ftypes)
if main.filename:
print("Current file: " + main.filename)
console_print("Current file: " + main.filename)
def convert():
try:
if main.filename:
pass
except AttributeError:
console_print("Please specify a file.")
else:
if not main.filename:
console_print("Please specify a file.")
else:
# 6 processes
console_print("Converting " + main.filename + " to '.txt' format.")
total_operation["value"] = 0
# process 1
console_print("Generating folder for extraction.")
current_operation["value"] = 0
command = "if not exist '" + working_dir + "\\Extracted' md '" + working_dir + "\\Extracted'"
total_operation["value"] += (100/6)/2
current_operation["value"] += 100/2
os.system(command)
total_operation["value"] += (100/6)/2
current_operation["value"] += 100/2
# process 2
console_print("Extracting document.")
current_operation["value"] = 0
with ZipFile(main.filename, "r") as doc:
total_operation["value"] += (100/6)/2
current_operation["value"] += 100/2
doc.extractall(working_dir + "\\Extracted")
output = ""
total_operation["value"] += (100/6)/2
current_operation["value"] += 100/2
# process 3
console_print("Locating XML.")
current_operation["value"] = 0
file_name = "document.xml"
total_operation["value"] += (100/6)/4
current_operation["value"] += 100/4
full_file = os.path.abspath(os.path.join("Extracted\\word", file_name))
total_operation["value"] += (100/6)/4
current_operation["value"] += 100/4
with open(full_file, "r") as f:
total_operation["value"] += (100/6)/4
current_operation["value"] += 100/4
contents = f.read()
total_operation["value"] += (100/6)/4
current_operation["value"] += 100/4
f.close()
# process 4
console_print("Scanning for text.")
current_operation["value"] = 0
text_list = re.findall("<w:r.*?\</w:r>", contents)
for section in text_list:
total_operation["value"] += (100/6)/len(text_list)
current_operation["value"] += 100/len(text_list)
console_print("Parsing section " + str(text_list.index(section) + 1) + ".")
for line in re.findall("<w:t.*?</w:t", section):
if 'xml:space="preserve"' in line:
line = line[26:-5]
else:
line = line[5:-5]
line = line.strip().replace("’", "'")
if not line:
try:
if lastline:
pass
except NameError:
pass
else:
if lastline:
output += "\n"
elif line in list(",.);:!?") or len(line.split()) == 1 and line != line.title():
output += line
else:
try:
if lastline:
pass
except NameError:
output += line
console_print("Line found!")
else:
output += "\n" + line
console_print("Line found!")
lastline = line
# process 5
console_print("Generating output name.")
current_operation["value"] = 0
output_file_name = working_dir + "\\Converted\\" + "".join(main.filename.replace("/", "\\").split("\\")[-1]).rstrip(".docx") + ".txt"
total_operation["value"] += (100/6)/1
current_operation["value"] += 100/1
# process 6
console_print("Creating file and deleting extraction directory.")
current_operation["value"] = 0
with open(output_file_name, "w") as f:
f.write(output)
total_operation["value"] += (100/6)/2
current_operation["value"] += 100/2
f.close()
shutil.rmtree(working_dir + "\\Extracted")
total_operation["value"] += (100/6)/2
current_operation["value"] += 100/2
console_print("Complete.")
bar = tk.Frame(main, height=25, width=1000, bg="#535353")
bar.place(anchor=tk.N, x=0, y=0)
quit_button = tk.Button(bar, command=leave, fg="white", bg="#e02020", text="X", padx=10, pady=4, highlightthickness=0, bd=0, activebackground="#b81111")
quit_button.place(x=985, y=0, anchor=tk.N)
file_button = tk.Button(main, command=get_file, fg="white", bg="#1b1b1b", text="Select File", padx=55, pady=10, highlightthickness=0, bd=0, activebackground="#535353")
file_button.place(x=30, y=50, anchor=tk.NW)
convert_button = tk.Button(main, command=convert, fg="white", bg="#1b1b1b", text="Convert", padx=60, pady=10, highlightthickness=0, bd=0, activebackground="#535353")
convert_button.place(x=30, y=100, anchor=tk.NW)
console_box = tk.Frame(main, height=250, width=250, bg="#1b1b1b")
console_box.place(x=470, y=300, anchor=tk.SE)
console_text = tk.Label(console_box, text=console.text(), fg="#ffffff", bg="#1b1b1b", font="Courier 10", wraplength = 200, justify=tk.LEFT)
console_text.place(x=10, y=10, anchor=tk.NW)
current_operation_text = tk.Label(main, fg="white", bg="#333333", text="Current Operation", justify=tk.RIGHT)
current_operation_text.place(x=470, y=310, anchor=tk.E)
style = ttk.Style()
style.theme_use("clam")
background_colour = "#1b1b1b"
bar_colour = "#b135ff"
style.configure("operation.Horizontal.TProgressbar", troughcolor=background_colour, bordercolor=background_colour, background=bar_colour, lightcolor=bar_colour, darkcolor=bar_colour)
current_operation = Progressbar(main, orient="horizontal", length=250, mode="determinate", maximum=100, value=0, style="operation.Horizontal.TProgressbar")
current_operation.place(x=470, y=340, anchor=tk.SE)
total_operation_text = tk.Label(main, fg="white", bg="#333333", text="Total Operation", justify=tk.RIGHT)
total_operation_text.place(x=470, y=350, anchor=tk.E)
total_operation = Progressbar(main, orient="horizontal", length=250, mode="determinate", maximum=100, value=0, style="operation.Horizontal.TProgressbar")
total_operation.place(x=470, y=380, anchor=tk.SE)
main.mainloop()
任何建议都非常感谢,如果我问的是不正确的问题,请告诉我! 谢谢。