FAQ
Hello,

My first attenpt at a simple python Tkinter application. I wanted to
see how to load file names into a listbox from a menu. This is what I
got until the part of displaying the file names in a listbox, which I
could not figfure out how to do?

Any help would be appreciated. Trying to do this as simple as possible
to understand.

The "print fileNames" does work but how to display it in listbox?

from Tkinter import *
from tkFileDialog import askopenfilenames

class MyCode:
def __init__(self):
root = Tk()

menubar = Menu(root)
filemenu = Menu(menubar)

lbox = Listbox(root, widthP).pack()

menubar.add_cascade(label='File', menu=filemenu)
filemenu.add_command(label='New Project')
filemenu.add_command(label='Load Files...', command=self.OnLoad)


filemenu.add_command(label='Exit', command=root.quit)

root.config(menu=menubar)

root.mainloop()


def OnLoad(self):
fileNames = askopenfilenames(filetypes=[('Split Files', '*')])
print fileNames


myApp = MyCode()

Search Discussions

  • John McMonagle at May 20, 2010 at 5:36 am

    Rick at home.com wrote:
    Hello,

    My first attenpt at a simple python Tkinter application. I wanted to
    see how to load file names into a listbox from a menu. This is what I
    got until the part of displaying the file names in a listbox, which I
    could not figfure out how to do?

    Any help would be appreciated. Trying to do this as simple as possible
    to understand.

    The "print fileNames" does work but how to display it in listbox?

    from Tkinter import *
    from tkFileDialog import askopenfilenames

    class MyCode:
    def __init__(self):
    root = Tk()

    menubar = Menu(root)
    filemenu = Menu(menubar)

    lbox = Listbox(root, widthP).pack()
    Your problem is the above line of code.

    Here you are assigning the return value of the pack (None if I recall).

    Later when you want to put things in the Listbox you don't have the
    widget reference anymore.

    It's probably a good idea to make the Listbox widget an attribute of
    your class so you can use it in class methods without having to pass it
    as an argument.

    So, change the above line of code to:

    self.lbox = Listbox(root, widthP)
    self.lbox.pack()
    menubar.add_cascade(label='File', menu=filemenu)
    filemenu.add_command(label='New Project')
    filemenu.add_command(label='Load Files...', command=self.OnLoad)


    filemenu.add_command(label='Exit', command=root.quit)

    root.config(menu=menubar)

    root.mainloop()


    def OnLoad(self):
    fileNames = askopenfilenames(filetypes=[('Split Files', '*')])
    print fileNames
    Now that you've got a reference to your Listbox widget that is an
    attribute of your class, you should be able to insert the file names
    into the Listbox like so.

    self.lbox.insert(0, fileNames)

    Regards,

    John
  • Sit at May 20, 2010 at 3:58 pm

    On Thu, 20 May 2010 15:36:59 +1000, John McMonagle wrote:

    Rick at home.com wrote:
    Hello,

    My first attenpt at a simple python Tkinter application. I wanted to
    see how to load file names into a listbox from a menu. This is what I
    got until the part of displaying the file names in a listbox, which I
    could not figfure out how to do?

    Any help would be appreciated. Trying to do this as simple as possible
    to understand.

    The "print fileNames" does work but how to display it in listbox?

    from Tkinter import *
    from tkFileDialog import askopenfilenames

    class MyCode:
    def __init__(self):
    root = Tk()

    menubar = Menu(root)
    filemenu = Menu(menubar)

    lbox = Listbox(root, widthP).pack()
    Your problem is the above line of code.

    Here you are assigning the return value of the pack (None if I recall).

    Later when you want to put things in the Listbox you don't have the
    widget reference anymore.

    It's probably a good idea to make the Listbox widget an attribute of
    your class so you can use it in class methods without having to pass it
    as an argument.

    So, change the above line of code to:

    self.lbox = Listbox(root, widthP)
    self.lbox.pack()
    menubar.add_cascade(label='File', menu=filemenu)
    filemenu.add_command(label='New Project')
    filemenu.add_command(label='Load Files...', command=self.OnLoad)


    filemenu.add_command(label='Exit', command=root.quit)

    root.config(menu=menubar)

    root.mainloop()


    def OnLoad(self):
    fileNames = askopenfilenames(filetypes=[('Split Files', '*')])
    print fileNames
    Now that you've got a reference to your Listbox widget that is an
    attribute of your class, you should be able to insert the file names
    into the Listbox like so.

    self.lbox.insert(0, fileNames)

    Regards,

    John
    Thank you John for your reply.

    I have made the modifications and it does print inside the listbox,
    however they are all printed on the same line.

    My files are named Name.0 Name.1 Name.2 Name.3 etc so I would like
    them to be printed sorted on a separate line. When I did a
    type(fileNames) it showed me that it was in unicode and printed
    character per line, so I tried it this way

    listFileNames = str(fileNames).split(' ')
    for fileName in listFileNames:
    self.lbox.insert(0, fileName)

    it works but does not print sorted.

    it prints

    name.2
    name.1
    name.0
    name.3


    Thank you for all your help.
  • John McMonagle at May 20, 2010 at 10:02 pm

    sit at home.com wrote:

    I have made the modifications and it does print inside the listbox,
    however they are all printed on the same line.
    Sorry, I didn't realize askopenfilenames returned the filenames as a
    whitespace separated string, on my system they are returned as a tuple
    of strings.

    My files are named Name.0 Name.1 Name.2 Name.3 etc so I would like
    them to be printed sorted on a separate line. When I did a
    type(fileNames) it showed me that it was in unicode and printed
    character per line, so I tried it this way

    listFileNames = str(fileNames).split(' ')
    for fileName in listFileNames:
    self.lbox.insert(0, fileName)

    it works but does not print sorted.

    it prints

    name.2
    name.1
    name.0
    name.3
    You obviously need to sort the list before you loop over the items and
    add them to your listbox.

    listFileNames = sorted(str(filenames).split()


    Regards,

    John
  • Sit at May 20, 2010 at 11:26 pm

    On Fri, 21 May 2010 08:02:30 +1000, John McMonagle wrote:

    sit at home.com wrote:
    I have made the modifications and it does print inside the listbox,
    however they are all printed on the same line.
    Sorry, I didn't realize askopenfilenames returned the filenames as a
    whitespace separated string, on my system they are returned as a tuple
    of strings.

    My files are named Name.0 Name.1 Name.2 Name.3 etc so I would like
    them to be printed sorted on a separate line. When I did a
    type(fileNames) it showed me that it was in unicode and printed
    character per line, so I tried it this way

    listFileNames = str(fileNames).split(' ')
    for fileName in listFileNames:
    self.lbox.insert(0, fileName)

    it works but does not print sorted.

    it prints

    name.2
    name.1
    name.0
    name.3
    You obviously need to sort the list before you loop over the items and
    add them to your listbox.

    listFileNames = sorted(str(filenames).split()


    Regards,

    John
    Thank you, it worked

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 20, '10 at 1:35a
activeMay 20, '10 at 11:26p
posts5
users3
websitepython.org

3 users in discussion

John McMonagle: 2 posts Sit: 2 posts Rick: 1 post

People

Translate

site design / logo © 2022 Grokbase