问题描述
我已经尝试了通常的 pyinstaller 东西,cx_Freeze,带有 idna 的 cx_Freeze,但它仍然无法工作。当我运行到目前为止生成的 exe 文件时,它总是以命令提示符/终端立即打开和关闭结束。它在 python IDLE 中运行良好,但我不明白为什么它不能作为 exe 文件运行。 这是代码:
import pyautogui
import subprocess
from tkinter import *
import tkinter.messageBox as msgBox
import time
from pynput.keyboard import Key,Controller
from PIL import ImageTk,Image
keyboard = Controller()
root = Tk()
root.geometry("550x500")
root.title("Youtube-dl")
global button_on
button_on = False
global button2_on
button2_on = False
global button3_on
button3_on = False
global link
link = StringVar()
def download():
global link
global button_on
global button2_on
global button3_on
pyautogui.hotkey('winleft','d')
time.sleep(0.3)
command = "cmd"
subprocess.Popen(command)
time.sleep(1)
for char in "cd /":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.05)
pyautogui.press('enter')
for char in "cd users":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.05)
pyautogui.press('enter')
for char in "cd admin":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.05)
pyautogui.press('enter')
for char in "cd desktop":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.05)
pyautogui.press('enter')
for char in "cd youtube-dl":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.05)
pyautogui.press('enter')
if (button_on==True) and (button3_on==False):
for char in 'youtube-dl '+str(link.get()):
keyboard.press(char)
keyboard.release(char)
time.sleep(0.05)
elif (button_on==True) and (button3_on==True):
for char in 'youtube-dl -cit '+str(link.get()):
keyboard.press(char)
keyboard.release(char)
time.sleep(0.05)
elif (button2_on==True) and (button3_on==False):
for char in 'youtube-dl --extract-audio --audio-format mp3 '+str(link.get()):
keyboard.press(char)
keyboard.release(char)
time.sleep(0.05)
elif (button2_on==True) and (button3_on==True):
for char in 'youtube-dl -cit --extract-audio --audio-format mp3 '+str(link.get()):
keyboard.press(char)
keyboard.release(char)
time.sleep(0.05)
pyautogui.press('enter')
def switch1():
global button_on
global button2_on
if button_on==True:
button1.config(image=off)
button_on = False
else:
button1.config(image=on)
button2.config(image=off)
button_on = True
button2_on = False
def switch2():
global button_on
global button2_on
if button2_on==True:
button2.config(image=off)
button2_on = False
else:
button1.config(image=off)
button2.config(image=on)
button_on = False
button2_on = True
def switch3():
global button3_on
if button3_on==True:
button3.config(image=off)
button3_on = False
else:
button3.config(image=on)
button3_on = True
onpic = Image.open("images/onbutton.png")
offpic = Image.open("images/offbutton.png")
onresized = onpic.resize((85,45),Image.ANTIALIAS)
offresized = offpic.resize((85,Image.ANTIALIAS)
on = ImageTk.PhotoImage(onresized)
off = ImageTk.PhotoImage(offresized)
f1 = Frame(root,pady=6)
Label(f1,text="Link",font="comicsans 10").pack()
linkentry = Entry(f1,textvariable=link,width=48,font="comicsans 9",relief=SUNKEN)
linkentry.pack()
f1.pack()
button1 = Button(root,image=off,bd=0,command=switch1)
button1.pack(pady=5)
f2 = Frame(root)
Label(f2,text="Video",font="comicsans 10").pack()
f2.pack()
space = Frame(root,pady=1)
Label(space,text=" ",font="comicsans 10").pack()
space.pack()
button2 = Button(root,command=switch2)
button2.pack(pady=5)
f3 = Frame(root)
Label(f3,text="Audio",font="comicsans 10").pack()
f3.pack()
space = Frame(root,font="comicsans 10").pack()
space.pack()
button3 = Button(root,command=switch3)
button3.pack(pady=5)
f4 = Frame(root)
Label(f4,text="Playlist",font="comicsans 10").pack()
f4.pack()
space = Frame(root,pady=1.5)
Label(space,font="comicsans 10").pack()
space.pack()
f5 = Frame(root)
Button(f5,text="Download",command=download).pack()
f5.pack()
解决方法
在 pyinstaller
中使用 pyinstaller --onefile -w nameofyourfile.py
尝试命令 cmd
时(我认为 w
代表窗口应用程序,但问题是它不会打开控制台)