我制作了一个YouTube视频下载程序,它使用cmd在新的控制台窗口中运行youtube-dl。当我单独运行程序时它运行得很好,但是当我冻结它并点击下载按钮时,它会打开两个控制台窗口并仅在其中一个程序中运行程序。另一个只是坐在那里直到程序结束。
这完全是我的代码。温柔我仍然很新。
from tkinter import *
from tkinter import filedialog
import os
class MyApp:
def __init__(self, parent):
# -----------Main window--------------------
self.mainwindow = Frame(parent)
self.mainwindow.pack(expand=YES, fill=BOTH)
# -----------Frames--------------------
self.topFrame = Frame(self.mainwindow, bg="gray20")
self.topFrame.pack(side=TOP, expand=YES, fill=BOTH)
self.midFrame = Frame(self.mainwindow, bg="gray20")
self.midFrame.pack(side=TOP, expand=YES, fill=BOTH)
self.URLframe = Frame(self.midFrame, bg="gray20")
self.URLframe.pack(side=TOP, expand=YES, fill=BOTH)
self.folderframe = Frame(self.midFrame, bg="gray20")
self.folderframe.pack(side=TOP, expand=YES, fill=BOTH)
self.bottomFrame = Frame(self.mainwindow, bg="gray20")
self.bottomFrame.pack(side=BOTTOM, expand=YES, fill=BOTH)
# -----------Top label--------------------
self.Toplabel = Label(self.topFrame,
text="YouTube Hoarder", font=("Courier", 44),
fg="blue2", bg="gray20",
height=3, width=50,
)
self.Toplabel.pack()
# -----------URL entry--------------------
self.URL_entry = Entry(self.URLframe, width=90)
self.URL_entry.focus_force()
self.URL_entry.pack(side=RIGHT, padx=15)
self.URL_text = Label(self.URLframe,
text="Gimme URL",
height=1, width=10,
bg="light blue"
)
self.URL_text.pack(side=LEFT, pady=6)
# -----------folder entry--------------------
self.DirVar = StringVar(self.mainwindow)
self.folder_entry = Entry(self.folderframe, width=90,
textvariable=self.DirVar)
self.folder_entry.pack(side=RIGHT, padx=15)
self.folder_text = Label(self.folderframe,
text="Gimme folder",
height=1, width=10,
bg="light blue"
)
self.folder_text.pack(side=LEFT, pady=6)
# -----------buttons--------------------
self.Download_button = Button(self.bottomFrame,
text="Download",
width=20, height=4,
bg="light green",
command=self.download_process
)
self.Download_button.pack(side=RIGHT)
self.find_folder_button = Button(self.midFrame,
text="Folder Directory",
width=12, height=2,
bg="light green",
command=self.print_message2
)
self.find_folder_button.pack(side=RIGHT, padx=5)
# -----------options menu--------------------
self.Type_list = ["best", "mp3", "mp4"]
self.Variable = StringVar(self.mainwindow)
self.Variable.set(self.Type_list[0])
self.file_option = OptionMenu(self.bottomFrame, self.Variable,
*self.Type_list)
self.file_option.pack(side=LEFT)
def print_message2(self):
self.dir_opt = options = {}
options['initialdir'] = 'Downloads'
self.DirName = filedialog.askdirectory(**self.dir_opt)
self.DirVar.set(self.DirName)
def download_process(self):
self.givenURL = self.URL_entry.get()
self.givenFT = self.Variable.get()
os.system('start /wait cmd /c youtube-dl --output '
'"{0}\%(title)s.%(ext)s" -i -f '
'{1}/best --yes-playlist "{2}"'.format(
self.DirName, self.givenFT, self.givenURL))
root = Tk()
root.resizable(width=False, height=False)
root.geometry("{}x{}".format(700 , 400))
root.title("YouTube Hoarder")
root.iconbitmap(r'C:\Users\tjhall\Downloads\LT3WE.ico')
myapp = MyApp(root)
root.mainloop()