본문 바로가기
파이썬

파이썬 IndexError : 튜플 인덱스가 범위를 벗어남 ----- Python

by º기록 2020. 12. 30.
반응형

제발 도와주세요. mySQL 데이터베이스의 데이터를 tkinter 형식으로 표시하는 간단한 파이썬 프로그램을 실행 중입니다.

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)

그게 전체 프로그램입니다 ....

오류는

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

누구든지 나를 도울 수 있습니까 ??? 파이썬의 새로운 메신저.

정말 고맙습니다....

 

해결 방법

 

아마도 인덱스 중 하나가 안쪽이든 바깥 ​​쪽이든 잘못되었을 것입니다.

[1] 에서 [0] , [2] <에서 [1] 이라고 말하려는 것 같습니다. / code>. 인덱스는 Python에서 0부터 시작합니다.

 

참조 페이지 https://stackoverflow.com/questions/20296188

 

 

반응형

댓글