AttributeError Problem
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() -- http://mail.python.org/mailman/listinfo/python-list
Re: AttributeError Problem
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: AttributeError Problem
On Tuesday, April 23, 2013 8:02:08 PM UTC-4, Skip Montanaro wrote: > > numberOfVertices = int(infile.readline().decode()) # Read the first line > > from the file > > > AttributeError: 'str' object has no attribute 'readline' > > ... > > > infile = filedialog.askopenfilename() > > > > This is just returning a filename. You need to open it to get a file > > object. For example: > > > > infile = filedialog.askopenfilename() > > fd = open(infile) > > ... > > numberOfVertices = int(fd.readline().decode()) > > > > Skip Thanks, but now I have this error AttributeError: 'str' object has no attribute 'decode' -- http://mail.python.org/mailman/listinfo/python-list