0

i am having trouble loading one of my text files into my GUI. I have a load function(on the menubar), and have created a listbox.

The code for the loading the menu and the listbox:

class View(Listbox):
    def __init__(self, master):
        Listbox.__init__(self, master)      #makes view class listbox


class Controller(object):
    def __init__(self, master):
        """ Main interface:
        master - the top level window

        """
        self._master = master
        menubar = Menu(self._master)

        frame1 = Frame(self._master)
        frame1.pack(side=TOP, fill=BOTH, padx=5,expand=True)

        self._Listbox=View(frame1)
        self._Listbox.pack(side = TOP,fill=BOTH, expand = True,pady=20)

        menubar = Menu(self._master)
        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label="Open rooms file", command = self.file_open)


    def file_open(self):
        filename = tkFileDialog.askopenfilename() 

The load file works, but where is the text document being currently loaded? How can i display it on my listbox?

1
  • sorry, i didn't type that properly. it was supposed to be display. Since i'm loading a text file into the GUI, i want the text file to be displayed onto the lixtbox. Commented May 16, 2012 at 13:28

1 Answer 1

3

This works:

from Tkinter import *
import tkFileDialog

class View(Listbox):
    def __init__(self, master):
        Listbox.__init__(self, master)      #makes view class listbox


class Controller(object):
    def __init__(self, master):
        """ Main interface:
        master - the top level window
        """
        self._master = master

        frame1 = Frame(self._master)
        frame1.pack(side=TOP, fill=BOTH, padx=5,expand=True)

        self._Listbox=View(frame1)
        self._Listbox.pack(side = TOP,fill=BOTH, expand = True,pady=20)

        menubar = Menu(self._master)
        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label="Open", command = self.file_open)
        menubar.add_cascade(label='File',menu=filemenu)
        self._master.config(menu=menubar)

    def file_open(self):
        filename = tkFileDialog.askopenfilename()

        #insert each line in the file into the listbox
        with open(filename,'r') as f:
            for line in f:
                self._Listbox.insert(END,line)



if __name__ == "__main__":
    root=Tk()
    c=Controller(root)
    root.mainloop()

This is only slightly different than your code... First, I removed the first menubar = Menu(self._master) since it didn't really do anything. Second, I added a "cascade" menubar.add_cascade(label='File',menu=filemenu), third, I actually attached the menu to the root Tk window: self._master.config(menu=menubar)

Sign up to request clarification or add additional context in comments.

5 Comments

? Sorry i didn't post my imports or my full code. But yes, this is what it looks like. Can you possibly make it so that when you load a text file, it will display it in the listbox?
I don't completely understand what you want ... the list box is displayed, there's just nothing in it. What is in the text file? Is each line of the text file supposed to be a new entry in the listbox? Also, this isn't quite the same as your code -- I'll edit to point out the differences.
@AnthonyDo : I've edited to point out the differences between my code and yours -- I've also added a simple loop to pack each line in the file into a different line in the listbox.
Here is what i want, when i load the text document from the GUI. an image will be much better. imageshack.us/photo/my-images/831/97707494.png
thanks for that :). i'll have a play around with the file_open function and see if i can get the format i want. i appreciate your time in helping me, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.