Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter文件对话框,传递文件路径并运行脚本的其余部分,转换为.app_Python_Python 3.x_Macos_Tkinter_Pyinstaller - Fatal编程技术网

Python Tkinter文件对话框,传递文件路径并运行脚本的其余部分,转换为.app

Python Tkinter文件对话框,传递文件路径并运行脚本的其余部分,转换为.app,python,python-3.x,macos,tkinter,pyinstaller,Python,Python 3.x,Macos,Tkinter,Pyinstaller,我已经构建了一个脚本,它读取一个日志文本文件并执行某些格式化,然后编写一个html文件并自动打开它。当我告诉它具体查找日志文件的路径时,一切正常。我对Python中的GUI不太在行,所以我找到了一些Tkinter的代码,并对其进行了一些编辑,以满足我的需要。现在,用户可以选择文件,然后运行脚本的其余部分 当我在终端中运行脚本时,它会工作。这将启动一个文件对话框,一旦选择文件,它将自动运行脚本并打开带有格式化文本的html文件 我现在已经尝试使用PyInstaller将其制作成一个.app 我按照

我已经构建了一个脚本,它读取一个日志文本文件并执行某些格式化,然后编写一个html文件并自动打开它。当我告诉它具体查找日志文件的路径时,一切正常。我对Python中的GUI不太在行,所以我找到了一些Tkinter的代码,并对其进行了一些编辑,以满足我的需要。现在,用户可以选择文件,然后运行脚本的其余部分

当我在终端中运行脚本时,它会工作。这将启动一个文件对话框,一旦选择文件,它将自动运行脚本并打开带有格式化文本的html文件

我现在已经尝试使用PyInstaller将其制作成一个.app

我按照说明使用了 pyinstaller-onedir-windowed beta1.py

这在dist文件夹中生成了一个.app文件,但当我双击它时,它什么也不做,没有尝试打开该文件

我不确定我做错了什么。我已经在下面发布了我的代码:

import webbrowser 
import os
import re
import tkinter
from tkinter import filedialog
import os

root = tkinter.Tk()
file = filedialog.askopenfile(parent=root,mode='rb',title='Choose a file')
if file != None:
    data = file.read()
    file.close()

if file:
    filepath = file.name


root.quit()
infile = filepath


fixed_div = """
<style>#fixed_div {
    background: #555;
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    min-height: 100px;
    text-align: center;
    line-height: 60px;
    font-family: 'Helvetica', 'Arial', sans-serif;
}

#logtext {
    padding: 100px 0px 0px;
    white-space: pre-wrap;
    line-height: 1.5;

}

a, u {
    text-decoration: none;
}

#fixed_div a:link { color: white; }
#fixed_div a:visited { color: white; }
#fixed_div a:hover { color: #FF715B; }
#fixed_div a:active { color: #F3E476; }


a.anchor {
    display: block;
    position: relative;
    top: -250px;
    visibility: hidden;
}</style><br>"""




with open(infile) as f:
    f = f.read()
    error_occurances = f.count("ERROR:")
    fail_occurances = f.count("FAIL:")

with open(infile, 'r') as IN, open('output.html', 'w') as OUT:
    OUT.write(fixed_div)
    OUT.write('<head><meta charset="UTF-8"></head>')
    OUT.write('<body>')
    OUT.write('<div id="fixed_div">')
    OUT.write('<div id="title"><font color="white"><font size="5"><strong><a href="#">&#xF8FF LOG READER</a></strong></font></font></div>')
    for x in range(error_occurances):
        error_links = "ERROR " + str(x)
        OUT.write('<a href=' + '"' + '#' + error_links + '"' '>' + error_links + "\t" + '&#9702;' + '</a>' + "\t")


    OUT.write('</div>')
    OUT.write("<pre id='logtext'>")
    x = 0
    for line in IN:
        if "ERROR" in line:
            error_links = "ERROR " + str(x)
            x+=1
            f = '<a class="anchor" id=' + '"' + error_links + '"' '></a><span style="background-color: #ff0000; color: #ffffff;">' + line + '</span>'
            OUT.write(f + '\n')
        elif "FAIL:" in line:
            OUT.write(line.replace('FAIL:', '<strong><span style="background-color: #ff0000; color: #ffffff;">FAIL:</span></strong>') + '\n')
        elif "DEBUG" in line:
            OUT.write(line.replace('DEBUG:', '<strong><span style="color: #0000ff;">DEBUG:</span></strong>') + '\n')
        elif "WARNING" in line:
            OUT.write(line.replace('WARNING:', '<strong><span style="color: #F5A422;">WARNING:</span></strong>') + '\n')
        elif "START:" in line:
            f = '<span style="background-color: #FFC9FF; color: #731275;"><b>' + line + '</b></span>'
        elif "INFO:" in line:
            OUT.write(line.replace('INFO:', '<strong><span style="color: grey;">INFO:</span></strong>') + '\n')
        else:
            OUT.write(line + '\n')
    OUT.write("</pre")
    OUT.write('</body>')




path = open('output.html')
path_name = os.path.realpath(path.name)
url = 'file://' + path_name
webbrowser.open(url, new=2)

在控制台/终端中手动运行它以查看错误消息。所以我打开终端并将.app拖动到?上面说deniedI想写文件的完整路径,但我不使用Mac。因此,您必须更改权限才能运行它。当我将其拖动到终端时,它会将完整路径放入.app-我不确定如何更改它的权限,我已登录到管理员帐户并获取信息显示读写是启用的。它可能需要可执行权限。至少它在Linux/Unix上需要它,而Mac目前是类Unix系统。