Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

Please Help me. I'm running a simple python program that will display the data from mySQL database in a tkinter form...

from Tkinter import * 

import MySQLdb 

def button_click():


root = Tk() 



myContainer = Frame(root) 

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

db = MySQLdb.connect ("localhost","root","","chocoholics") 

s = "Select * from member" cursor = db.cursor() c


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)

That's the whole program...

The error was

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

Can anyone help me??? I'm new to python.

Thank you so much...


1 Answer

0 votes
by (106k points)
edited by
Best answer

It looks like one of the indexes is wrong, either the inner one or the outer one.

I suspect you mean to say [0] where you say [1] and [1] where you say [2]. Indexes are 0-based in Python.

To know more about this you can have a look at the following video tutorial:-

Browse Categories