[tkinter] return value from FileDialog problem

Post here if you need help with creating a Graphical User Interface in Python.

[tkinter] return value from FileDialog problem

Postby maiden129 » Tue Apr 23, 2013 11:24 pm

Hi,

I'm working on this problem for my my homework but I don't understand why it's not reading data from a file ?

13.13 (Tkinter:display a graph) A graph consists of vertices and edges that connect vertices. Write a program that reads a graph from a file and displays it on a panel. 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.

Here is my code:

Code: Select all
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()




When I try to test it out I get this error message:

Code: Select all
line 19, in main
    numberOfVertices = int(infile.readline()) # Read the first line from the file
AttributeError: 'str' object has no attribute 'readline'
Last edited by Yoriz on Tue Apr 23, 2013 11:58 pm, edited 1 time in total.
Reason: Changed to a more descriptive title
maiden129
 
Posts: 3
Joined: Tue Apr 23, 2013 11:11 pm

Re: Code readlines problem

Postby Yoriz » Tue Apr 23, 2013 11:56 pm

I'm not a user of the tkinter module but the error you getting is because infile contains a string not a file object.
the variable infile is set in this line
Code: Select all
infile = filedialog.askopenfilename()

Looking up http://tkinter.unpythonic.net/wiki/tkFileDialog askopenfilename gets a filename to open(returns a filename and the file is opened by your own code)

Did you mean to use askopenfile which actually opens a file.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 571
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: [tkinter] return value from FileDialog problem

Postby maiden129 » Wed Apr 24, 2013 12:05 am

I want it to return the filename and read the data from it.
maiden129
 
Posts: 3
Joined: Tue Apr 23, 2013 11:11 pm

Re: [tkinter] return value from FileDialog problem

Postby Yoriz » Wed Apr 24, 2013 12:10 am

There is a tutorial on reading files on this forum.
http://www.python-forum.org/viewtopic.php?f=25&t=197
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 571
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: [tkinter] return value from FileDialog problem

Postby maiden129 » Wed Apr 24, 2013 12:29 am

Thank You.
maiden129
 
Posts: 3
Joined: Tue Apr 23, 2013 11:11 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 1 guest