为什么我得到索引超出范围错误

时间:2017-07-17 19:53:09

标签: python tkinter pytube

from tkinter import *
import pytube
import threading

def download():
    link = test_url.get()

    yt = pytube.YouTube(link)
    videos = yt.get_videos()

    n = quality.get()

    vid = videos[n - 1]

    destination = destination_test.get()
    vid.download(destination)

def test():
    threading.Thread(target=download()).start()

root = Tk()
test_url = StringVar()
quality = IntVar()
destination_test = StringVar()
url_label = Label(text='Enter Url')
quality_label = Label(text='quality')
url_label.grid(row=0, column=0)
quality_label.grid(row=1, column=0)
destination_label = Label(text='Destination')
destination_label.grid(row=2, column=0)
url_entry = Entry(textvariable=test_url)
url_entry.grid(row=0, column=1)

quality_entry = Entry(textvariable=quality)
quality_entry.grid(row=1, column=1)
destination_entry = Entry(textvariable=destination_test)
destination_entry.grid(row=2, column=1)
download_button = Button(text='download', command=test())
download_button.grid(row=3, column=1)
root.mainloop()
  
    

Traceback(最近一次调用最后一次):文件“C:\ Program Files \ JetBrains \ PyCharm Community Edition     2017.1.4 \ helpers \ pycharm_jb_unittest_runner.py“,第35行,in         main(argv = args,module = None,testRunner = unittestpy.TeamcityTestRunner,buffer = not     JB_DISABLE_BUFFERING)文件“C:\ Python36 \ lib \ unittest \ main.py”,行     93,在 init 中         self.parseArgs(argv)文件“C:\ Python36 \ lib \ unittest \ main.py”,第140行,在parseArgs中         self.createTests()文件“C:\ Python36 \ lib \ unittest \ main.py”,第147行,在createTests中         self.module)loadTestsFromNames中的文件“C:\ Python36 \ lib \ unittest \ _ loader.py”,第219行         suites = [self.loadTestsFromName(名称,模块)名称中的名称]文件“C:\ Python36 \ lib \ unittest \ loader.py”,第219行,         suites = [self.loadTestsFromName(名称,模块)名称中的名称]文件“C:\ Python36 \ lib \ unittest \ loader.py”,第153行,in     loadTestsFromName         module = import (module_name)文件“C:\ Users \ Matthew \ PycharmProjects \ test \ test.py”,第37行,in         download_button = Button(text ='download',command = test())文件“C:\ Users \ Matthew \ PycharmProjects \ test \ test.py”,第19行,在测试中         threading.Thread(target = download())。start()文件“C:\ Users \ Matthew \ PycharmProjects \ test \ test.py”,第13行,下载         vid = videos [n - 1] IndexError:列表索引超出范围

  

1 个答案:

答案 0 :(得分:2)

因为变量'n'的值要么等于0,要么它比你的视频数量大2或更多。

无论quality.get()做什么,无论质量变量是什么,n=quality.get()都是最终导致错误的行......