IndexError:元组索引超出范围----- Python

Jer*_*hez 34 python mysql-python python-2.7

请帮我.我正在运行一个简单的python程序,它将以tkinter形式显示mySQL数据库中的数据...

from Tkinter import *
import MySQLdb

def button_click():
    root.destroy()

root = Tk()
root.geometry("600x500+10+10")
root.title("Ariba")

myContainer = Frame(root)
myContainer.pack(side=TOP, expand=YES, fill=BOTH)

db = MySQLdb.connect ("localhost","root","","chocoholics")
s = "Select * from member"
cursor = db.cursor()
cursor.execute(s)
rows = cursor.fetchall()

x = rows[1][1] + " " + rows[1][2]
myLabel1 = Label(myContainer, text = x)
y = rows[2][1] + " " + rows[2][2]
myLabel2 = Label(myContainer, text = y)
btn = Button(myContainer, text = "Quit", command=button_click, height=1, width=6)

myLabel1.pack(side=TOP, expand=NO, fill=BOTH)
myLabel2.pack(side=TOP, expand=NO, fill=BOTH)
btn.pack(side=TOP, expand=YES, fill=NONE)
Run Code Online (Sandbox Code Playgroud)

这就是整个计划......

错误是

x = rows[1][1] + " " + rows[1][2]
IndexError: tuple index out of range

y = rows[2][1] + " " + rows[2][2]
IndexError: tuple index out of range
Run Code Online (Sandbox Code Playgroud)

谁能帮我???我是python的新手.

非常感谢....

glg*_*lgl 38

可能其中一个索引是错误的,无论是内部索引还是外部索引.

我怀疑你的意思是说[0]你说的地方[1][1]你说的地方[2].索引在Python中基于0.

  • 我收到此消息是因为我将数组传递给期望变量序列参数的函数(例如`'{} {}'.format([1,2])`vs`'{} {}'.格式(*[1,2])` (24认同)
  • 另一种情况是意外地在字符串中有两个'{}',而.format()中只有一个变量/值 (11认同)

Sai*_*eek 5

元组由多个用逗号分隔的值组成。喜欢

>>> t = 12345, 54321, 'hello!'
>>> t[0]
12345
Run Code Online (Sandbox Code Playgroud)

Python 中的元组是基于索引的(并且也是不可变的)。

在这种情况下,x = rows[1][1] + " " + rows[1][2]只有两个索引 0、1 可用,但您正在尝试访问第三个索引。