Re: Display file names in Tkinter Listbox

2010-05-20 Thread sit
On Thu, 20 May 2010 15:36:59 +1000, John McMonagle
jmcmona...@no.spam.velseis.com.au wrote:

r...@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, width=50).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, width=50)
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. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Display file names in Tkinter Listbox

2010-05-20 Thread John McMonagle
s...@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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Display file names in Tkinter Listbox

2010-05-20 Thread sit
On Fri, 21 May 2010 08:02:30 +1000, John McMonagle
jmcmona...@no.spam.velseis.com.au wrote:

s...@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 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Display file names in Tkinter Listbox

2010-05-19 Thread John McMonagle
r...@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, width=50).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, width=50)
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
-- 
http://mail.python.org/mailman/listinfo/python-list