AttributeError Problem

A

animemaiden

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 numberthat 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 onevertex
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()
 
A

animemaiden

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 firstline 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.
 
S

Skip Montanaro

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
 
A

animemaiden

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'
 
M

MRAB

Thanks, but now I have this error AttributeError: 'str' object has no attribute 'decode'
You already have a string (the line) that you've read from the file, so
what are you trying to 'decode' anyway? Just remove that erroneous
method call.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top