473,394 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

error coding noob program for a beginner

10
ive been learning python since the last couple days and am stuck on this "project"
basically what i am making is a program where a list of picture names appear in a list on one side and when you click them they should appear on the other side in a canvas.
below i included what i have so far. when i run the program it says that "list" does not have attribute "bind". im pretty sure it does, but i cant figure this one out and i lost the source material so i cannot reference it. this is driving me nuts. please help!!!!


-------

#!/usr/bin/python

from Tkinter import *
from os import listdir

def Pushed():
friend_name = ent.get()
friend_info = tex.get(0.0,END)

friend_name = "friends/" + friend_name

fil = file(friend_name,'w')
fil.write(friend_info)
fil.close()

li.delete(0,END)
fil = listdir('friends/')
for item in fil:
li.insert(END,item)



def Press(self):
tex.delete(0.0,END)
ent.delete(0,END)


selec = li.curselection()
str = li.get(selec[0])

ent.insert(END,str)
str = 'friends/' + str

fin = file(str,'r')
outp = fin.read()
fin.close()

tex.insert(END, outp)




root = Tk()

root.geometry('500x350+270+50')


lab = Label(root,text = "Friend directory")
name = Label(root, text = "name")
butto = Button(root, text = "New Friend", command = Pushed)
ent = Entry(root, bg = 'white', width = 40)
li = Listbox(root, bg = 'white', height = 13)
tex = Text(root, bg = 'white', font=("Helvatica",15), width = 30, height = 15)

lab.grid(row = 0, columnspan = 3)
name.grid(row = 1, column = 0)
ent.grid(row = 1, column = 1)
li.grid(rowspan = 2, column = 0 )
tex.grid(row = 2, column = 1)
butto.grid(row = 3, column = 1)


fil = listdir('friends/')
for item in fil:
li.insert(END,item)


li.bind("<Double-Button-1>",Press)



root.mainloop()
Jul 9 '08 #1
10 1607
montana
10
the input box screwed up my code when i submitted it. indents ARE in my program where i believe they are necessary.
Jul 9 '08 #2
Laharl
849 Expert 512MB
This is why we have the [code=python] tags, to prevent exactly that. Unfortunately, until you post your code with proper indentations, it'll be much harder to help you.
Jul 9 '08 #3
jlm699
314 100+
The Tkinter listbox does have a bind but perhaps you have implemented the list box incorrectly.

Refer to the posting guidelines for an explanation of how to use the code tags

Your implementation looks correct so you must be mistaken about the error that you are receiving.
Jul 9 '08 #4
tharden3
916 512MB
you need to format this in the "CODE" selection so that we can copy the code right out of your post. Otherwise, no one will want to help you.
Jul 9 '08 #5
montana
10
sorry guys, its my first time using this forum. here is my code properly displayed, thanks!

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from os import *
  3. from string import *
  4.  
  5. def Press():
  6.     selec=lis.curselection()
  7.     print selec
  8.     str = lis.get(selec[0])
  9.     print str
  10.  
  11.     tem = can.create_image(x,y, image = pic)
  12.  
  13.  
  14. root = Tk()
  15. root.geometry('700x600+270+50')
  16.  
  17. lab = Label(root, text = 'Name of the picture', font=('Times', 12))
  18. can = Canvas(root, width ='500', height = '400', bg ='white')
  19. lis = Listbox(root, height=20)
  20.  
  21. pic = listdir('pictures/')
  22.  
  23. for item in pic:
  24.     lis.insert(0,item)
  25.  
  26. pic.bind("<Double-Button-1>",Press)
  27.  
  28.  
  29.  
  30. lis.focus()
  31. lab.pack()
  32. can.pack()
  33. lis.pack()
  34.  
  35. root.mainloop()
  36.  
Jul 10 '08 #6
tharden3
916 512MB
sorry guys, its my first time using this forum. here is my code properly displayed, thanks!

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from os import *
  3. from string import *
  4.  
  5. def Press():
  6.     selec=lis.curselection()
  7.     print selec
  8.     str = lis.get(selec[0])
  9.     print str
  10.  
  11.     tem = can.create_image(x,y, image = pic)
  12.  
  13.  
  14. root = Tk()
  15. root.geometry('700x600+270+50')
  16.  
  17. lab = Label(root, text = 'Name of the picture', font=('Times', 12))
  18. can = Canvas(root, width ='500', height = '400', bg ='white')
  19. lis = Listbox(root, height=20)
  20.  
  21. pic = listdir('pictures/')
  22.  
  23. for item in pic:
  24.     lis.insert(0,item)
  25.  
  26. pic.bind("<Double-Button-1>",Press)
  27.  
  28.  
  29.  
  30. lis.focus()
  31. lab.pack()
  32. can.pack()
  33. lis.pack()
  34.  
  35. root.mainloop()
  36.  
It's ok, I'm pretty new to this too, and actually I made the same mistake you made the first time I posted. Unfortunately, I can't help you with your problem... but be patient, there are many intelligent people in this community that can
help. Good Luck with your coding!
Jul 10 '08 #7
I was able to resolve the error that u mentioned but was not really not successful in getting the images displayed, try this and see if you are able to get going.

The problem was here
pic.bind("<Double-Button-1>",Press)
pic is a list u cannot bind an action to a list instead try this

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from os import *
  3. from string import *
  4.  
  5. def Press(event):
  6.     selec=lis.curselection()
  7.     selected = lis.get(selec[0])
  8.     photo1 = PhotoImage(file=selected)
  9.     can.create_image(200, 100, image=photo1)
  10.  
  11. root = Tk()
  12. root.geometry('700x600+270+50')
  13.  
  14. lab = Label(root, text = 'Name of the picture', font=('Times', 12))
  15. can = Canvas(width =300, height = 300, bg ='white')
  16. lis = Listbox(root, height=20,selectmode=EXTENDED)
  17.  
  18. pic = listdir('pictures/')
  19. for item in pic:
  20.     lis.insert(0,item)
  21.  
  22. lis.bind("<Double-Button-1>", Press)
  23.  
  24. lis.focus()
  25. lab.pack()
  26. can.pack()
  27. lis.pack()
  28. root.mainloop()
Jul 10 '08 #8
montana
10
ok so this time i have everything displaying correctly but the program still doesnt work. i have the new error message and the new code posted below

Expand|Select|Wrap|Line Numbers
  1.  
  2. from Tkinter import *
  3. from os import *
  4. from string import *
  5.  
  6. #def Press(self):
  7.  #   selec=lis.curselection()
  8.   #  print selec
  9.    # str = lis.get(selec[0])
  10.     #print str
  11.     #tem = can.create_image(image = pic)
  12.  
  13.  
  14. def Press(self):
  15.     selec=lis.curselection()
  16.     selected = lis.get(selec[0])
  17.     photo1 = PhotoImage(file=selected)
  18.     can.create_image(200, 100, image=photo1)
  19.  
  20.  
  21. root = Tk()
  22. root.geometry('700x600+270+50')
  23.  
  24. lab = Label(root, text = 'Name of the picture', font=('Times', 12))
  25. can = Canvas(root, width ='500', height = '400', bg ='white')
  26. lis = Listbox(root, height=20)
  27.  
  28. pic = listdir('pictures/')
  29.  
  30. for item in pic:
  31.     lis.insert(0,item)
  32.  
  33. lis.bind("<Double-Button-1>",Press)
  34.  
  35.  
  36.  
  37. lis.focus()
  38. lab.pack()
  39. can.pack()
  40. lis.pack()
  41.  
  42. root.mainloop()
  43.  
  44.  
(())__ this is the error message below:
IDLE 1.2.2 ==== No Subprocess ====
>>>
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "C:\Documents and Settings\Administrator\Desktop\first python lessons\lecture 15.pyw", line 16, in Press
photo1 = PhotoImage(file=selected)
File "C:\Python25\lib\lib-tk\Tkinter.py", line 3270, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python25\lib\lib-tk\Tkinter.py", line 3226, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: couldn't open "th_img322.gif": no such file or directory
Jul 10 '08 #9
jlm699
314 100+
This error is telling you that PYthon cannot locate the image file that you have specified. When you are working with opening text files, image files, etc. it is always a good idea to use absolute paths instead of relative to avoid things like this happening. So instead of Python looking for mypic.jpg it's looking for C:\Doc and Settings\Me\My Pictures\mypic.jpg....

So on this line:
selected = lis.get(selec[0])

You could say
selected = os.path.join( my_images_path, lis.get( selec[0] ) )
** NOTE: my_images_path is the path where your images are stored, so fill it in with the proper value

That way Python will know exactly where to go for the image
Jul 10 '08 #10
montana
10
here is the most recent version of the code. i dont get any error messages anymore but the pictures do not display on the canvas. tell me what you think, any advice? whats wrong with it?

Expand|Select|Wrap|Line Numbers
  1. #! usr/bin/python
  2.  
  3. from Tkinter import *
  4. from os import *
  5. from string import *
  6.  
  7.  
  8. def Press(event):
  9.     selec=lis.curselection()
  10.     selected = lis.get(selec[0])
  11.     photo1 = PhotoImage('file=selected')
  12.     can.create_image(200, 100, image=photo1)
  13.  
  14.  
  15. root = Tk()
  16. root.geometry('700x600+270+50')
  17.  
  18. lab = Label(root, text = 'Name of the picture', font=('Times', 12))
  19. can = Canvas(root, width ='500', height = '400', bg ='white')
  20. lis = Listbox(root, height=20)
  21.  
  22. pic = listdir('pictures/')
  23.  
  24. for item in pic:
  25.     lis.insert(0,"pictures/"+item)
  26.  
  27. lis.bind("<Double-Button-1>",Press)
  28.  
  29.  
  30.  
  31. lis.focus()
  32. lab.pack()
  33. can.pack()
  34. lis.pack()
  35.  
  36. root.mainloop()
  37.  
Jul 28 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: administrata | last post by:
I'm programming Car Salesman Program. It's been "3 days" learning python... But, i got problem Write a Car Salesman program where the user enters the base price of a car. The program should...
3
by: We need more power captain | last post by:
Hi, I know less than a noob, I've been asked to do some compiles in VC++ 6 without knowing too much at all. (I'm a COBOL program normally so this is all too much for me) I open VC++6, open...
4
by: Matt | last post by:
Ok, sorry for posting so much, but I just have alot of questions. Ok, so It says to make a program to count blanks, tabs, and newlines. So I made one: #include <stdio.h> /* Count tabs,...
23
by: Simon Hengel | last post by:
Hello, we are hosting a python coding contest an we even managed to provide a price for the winner... http://pycontest.net/ The contest is coincidentally held during the 22c3 and we will be...
22
by: Mike Polinske | last post by:
I am new to the C programming language but have been programming in Cobol for over 10 years. When I compile the following code, it compiles clean but I get an application error both under Windows...
11
by: calmar | last post by:
Hi all, since I'm just a 'handicraft'/beginner or so, could anybody provide me with some (rough) hints, about how to enhance the code here: http://calmar.ws/tmp/cal.html Cheers and...
9
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include...
5
by: Omar | last post by:
Hi all...this is a good group so I'm sure you'll tolerate some more noobish questions... 1) I'm also learning to program flash movies while I learn to do python. How can one implement flash...
3
by: coffeetime | last post by:
Hi, I just decided to learn java, so I'm quite a noob. I use the book learning java third edition. have JDK 1.6 program in notepad. I have made the simple HelloJava example, and now i wanted to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.