繁体   English   中英

Python .exe 文件在双击时不会运行,但在通过 cmd 运行时有效

[英]Python .exe file won't run when double clicked, but works when run through cmd

我通过 Anaconda(更确切地说是 Spyder)在 python 中创建了一个程序,并使用 pyinstaller 从中创建了一个 .exe。 简单地说,当我通过 anaconda 提示运行它时,它可以工作,但是当我双击它时,它只是等待几秒钟然后关闭,什么也不做。

编码:

import xlrd
from scipy.fft import fft
import numpy as np
import tkinter as tk

def main():

    root =tk.Tk()
    root.title("Data input window")
    canvas1 = tk.Canvas(root, width = 620, height = 210,  relief = 'raised')
    canvas1.pack()
    inputdata = tk.StringVar(root)
        
    def getvalue():
        loc = inputdata.get()
        run(loc)
            
    label1 = tk.Label(root, text='Copy file and paste here:')
    label1.config(font=('helvetica', 14))
    canvas1.create_window(310, 25, window=label1)
    e1 = tk.Entry(root,textvariable = inputdata, width=100,fg="blue",bd=3,selectbackground='violet')
    canvas1.create_window(310, 65, window=e1)
    label2 = tk.Label(root, text='Only .xls files supported')
    label2.config(font=('helvetica', 8))
    canvas1.create_window(310, 105, window=label2)
    button1 = tk.Button(root, text='Input data', fg='White', bg= 'dark green', height = 1, width = 10,command=getvalue)
    canvas1.create_window(310, 180, window=button1)
    
    root.mainloop()
    s = input('Press X to exit')
    return 0;

if __name__ == '__main__':
    main()

run(loc) 基本上是当我按下开始时出现的 tkinter 小部件上的某个按钮时需要运行的整个程序。 即使我需要关闭程序的输入,它仍然会自动关闭并且不会出现 tkinter 小部件。

我是初学者,如果这个问题很简单,那么抱歉。

当你双击它时,程序仍在运行,只是窗口一完成就关闭,所以它看起来不像。

当您从 cmd 运行时,您可以轻松看到任何输出,因为此后窗口不会关闭。

但是该程序将在两种情况下运行。

当您双击它或从 cmd 运行它时,它会运行该程序
但是当运行程序本身时(例如:双击它):完成后窗口将关闭(这是正常的)

从 cmd 运行它时:程序将完成,但是因为您没有运行该程序,而是运行了运行该程序的命令提示符,因此在逻辑上它不会关闭命令提示符

你可以通过放入你的脚本来解决这个问题(在最后一行):
input()
Wich 将确保程序在等待用户输入时不会自动关闭

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM