简体   繁体   English

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

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

I created a program in python through Anaconda (Spyder, more exactly) and made an .exe out of it with pyinstaller.我通过 Anaconda(更确切地说是 Spyder)在 python 中创建了一个程序,并使用 pyinstaller 从中创建了一个 .exe。 Simply put, when I run it through the anaconda prompt it works, but when I double click it, it simply waits for a few seconds and then closes, without doing anything.简单地说,当我通过 anaconda 提示运行它时,它可以工作,但是当我双击它时,它只是等待几秒钟然后关闭,什么也不做。

The code:编码:

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) is basically the entire program that needs to run when I press a certain button on the tkinter widget that appears at start. run(loc) 基本上是当我按下开始时出现的 tkinter 小部件上的某个按钮时需要运行的整个程序。 Even if I require an input for the program to close, it still closes automatically and no tkinter widget appears.即使我需要关闭程序的输入,它仍然会自动关闭并且不会出现 tkinter 小部件。

I am a beginner, so sorry if this issue is a simple one.我是初学者,如果这个问题很简单,那么抱歉。

When you are double clicking it, the program is still being run, it's just that the window is closing as soon as it is finished so it doesn't look like it.当你双击它时,程序仍在运行,只是窗口一完成就关闭,所以它看起来不像。

When you run from cmd you are able to see any output easily as the window won't close afterwards.当您从 cmd 运行时,您可以轻松看到任何输出,因为此后窗口不会关闭。

But the program will be getting run in both scenarios.但是该程序将在两种情况下运行。

When you're double-clicking it or running it from cmd it runs the program当您双击它或从 cmd 运行它时,它会运行该程序
But when running the program itself (by as an example: double-clicking it): The window will close when it's done (which is normal)但是当运行程序本身时(例如:双击它):完成后窗口将关闭(这是正常的)

When running it from cmd: the program will finish but because you didn't run the program but you ran the command prompt which ran the program so logically it will not close the command prompt从 cmd 运行它时:程序将完成,但是因为您没有运行该程序,而是运行了运行该程序的命令提示符,因此在逻辑上它不会关闭命令提示符

You can fix this by putting in you're script (on the last line):你可以通过放入你的脚本来解决这个问题(在最后一行):
input()
Wich will make sure the program doesn't get automatically closed as its waiting for input from a user Wich 将确保程序在等待用户输入时不会自动关闭

暂无
暂无

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

相关问题 双击将运行Python脚本,但不会在IDLE中运行 - Python script runs when double-clicked, but won't run in IDLE 为什么我的 pyinstaller python 程序在双击时崩溃,而不是在 cmd 运行时崩溃 - Why my pyinstaller python program crashes when double-clicked and not when cmd run python .exe 文件无法打开/运行 - python .exe file won't open/run 通过anaconda提示符执行python代码时,导入numpy起作用,但通过cmd运行时产生错误 - Import numpy works when python code executed through anaconda prompt but produces an error when run through cmd 将 python 脚本转换为 exe 后,当我运行 script.exe 并双击它 flash cmd 时,出现错误? - after convert python script to exe when I run script.exe with double click it flash cmd and when run it by cmd the error down appear? 使用文件时,Python脚本无法通过php运行 - Python script won't run through php when using files 从文件夹或桌面打开时,Python程序无法正确运行,但在IDLE中运行时,它可以正常运行 - Python program won't run correctly when opened from folder or desktop, but works fine when run in IDLE 在 IDE 中使用 PIL 的 Python 脚本在通过 cmd 运行时不起作用,并给出“PIL”没有属性“图像” - Python script using PIL works in IDE doesn't when run by cmd and gives 'PIL' has no attribute 'Image' 双击.py时,Python脚本无法正常运行 - Python Script won't run right when double-clicking .py 在批处理文件和cmd行中运行时,Python脚本的行为会有所不同 - Python script is behaving differently when run through batch file and cmd line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM