[英]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.