关于python:为什么我得到索引超出范围错误

why am i getting a index is out of range error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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 (most recent call last): File"C:\Program Files\JetBrains\PyCharm Community Edition
2017.1.4\helpers\pycharm_jb_unittest_runner.py", line 35, in
main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not
JB_DISABLE_BUFFERING) File"C:\Python36\lib\unittest\main.py", line
93, in init
self.parseArgs(argv) File"C:\Python36\lib\unittest\main.py", line 140, in parseArgs
self.createTests() File"C:\Python36\lib\unittest\main.py", line 147, in createTests
self.module) File"C:\Python36\lib\unittest\loader.py", line 219, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names] File"C:\Python36\lib\unittest\loader.py", line 219, in
suites = [self.loadTestsFromName(name, module) for name in names] File"C:\Python36\lib\unittest\loader.py", line 153, in
loadTestsFromName
module = import(module_name) File"C:\Users\Matthew\PycharmProjects\test\test.py", line 37, in
download_button = Button(text='download', command=test()) File"C:\Users\Matthew\PycharmProjects\test\test.py", line 19, in test
threading.Thread(target=download()).start() File"C:\Users\Matthew\PycharmProjects\test\test.py", line 13, in download
vid = videos[n - 1] IndexError: list index out of range


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

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