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