AttributeError Problem

animemaiden animemaiden21 at gmail.com
Tue Apr 23 19:42:46 EDT 2013


On Tuesday, April 23, 2013 7:41:27 PM UTC-4, animemaiden wrote:
> Hi,
> 
> 
> 
> I'm trying to display a graph in Tkinter  that reads a graph from a file and displays it on a panel which the first line in the file contains a number that indicates the number of vertices (n). The vertices are labeled as 0,1,…,n-1. Each subsequent line, with the format u x y v1, v2, …describes that the vertex u is located at position (x,y) with the edges (u,1). (u,v2), and so on.
> 
> 
> 
> 
> 
> I'm using Python 3.2.3 and I keep getting this error:
> 
> 
> 
> numberOfVertices = int(infile.readline().decode()) # Read the first line from the file
> 
> AttributeError: 'str' object has no attribute 'readline'
> 
> 
> 
> 
> 
> Here is my code:
> 
> 
> 
> from tkinter import * # Import tkinter
> 
> from tkinter import filedialog
> 
> 
> 
> 
> 
> 
> 
> def displayGraph(canvas, vertices, edges):
> 
>     radius = 3
> 
>     for vertex, x, y in vertices:
> 
>         canvas.create_text(x - 2 * radius, y - 2 * radius, text = str(vertex), tags = "graph")
> 
>         canvas.create_oval(x - radius, y - radius, x + radius, y + radius, fill = "black", tags = "graph")
> 
> 
> 
>     for v1, v2 in edges:
> 
>         canvas.create_line(vertices[v1][1], vertices[v1][2], vertices[v2][1], vertices[v2][2], tags = "graph")
> 
> 
> 
> def main():
> 
> 
> 
>     infile = filedialog.askopenfilename()
> 
> 
> 
>     
> 
>     numberOfVertices = int(infile.readline().decode()) # Read the first line from the file
> 
>     print(numberOfVertices)
> 
> 
> 
>     vertices = []
> 
>     edges = []
> 
>     for i in range(numberOfVertices):
> 
>         items = infile.readline().strip().split() # Read the info for one vertex
> 
>         vertices.append([int(items[0]), int(items[1]), int(items[2])])
> 
>         for j in range(3, len(items)):
> 
>             edges.append([int(items[0]), int(items[j])])            
> 
>     
> 
>     print(vertices)
> 
>     print(edges)
> 
>     
> 
>     infile.close()  # Close the input file
> 
> 
> 
>     window = Tk() # Create a window
> 
>     window.title("Display a Graph") # Set title
> 
>     
> 
>     frame1 = Frame(window) # Hold four labels for displaying cards
> 
>     frame1.pack()
> 
>     canvas = Canvas(frame1, width = 300, height = 200)
> 
>     canvas.pack()
> 
>     
> 
>     displayGraph(canvas, vertices, edges)
> 
>     
> 
>     window.mainloop() # Create an event loop
> 
> 
> 
> main()

Also, it reads data from a file.



More information about the Python-list mailing list