简体   繁体   中英

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. 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.

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. Even if I require an input for the program to close, it still closes automatically and no tkinter widget appears.

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.

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
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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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