error coding noob program for a beginner

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • montana
    New Member
    • Jul 2008
    • 10

    error coding noob program for a beginner

    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_nam e,'w')
    fil.write(frien d_info)
    fil.close()

    li.delete(0,END )
    fil = listdir('friend s/')
    for item in fil:
    li.insert(END,i tem)



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


    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=("Helvatic a",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('friend s/')
    for item in fil:
    li.insert(END,i tem)


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



    root.mainloop()
  • montana
    New Member
    • Jul 2008
    • 10

    #2
    the input box screwed up my code when i submitted it. indents ARE in my program where i believe they are necessary.

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      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.

      Comment

      • jlm699
        Contributor
        • Jul 2007
        • 314

        #4
        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.

        Comment

        • tharden3
          Contributor
          • Jul 2008
          • 916

          #5
          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.

          Comment

          • montana
            New Member
            • Jul 2008
            • 10

            #6
            sorry guys, its my first time using this forum. here is my code properly displayed, thanks!

            Code:
            from Tkinter import *
            from os import *
            from string import *
            
            def Press():
                selec=lis.curselection()
                print selec
                str = lis.get(selec[0])
                print str
            
                tem = can.create_image(x,y, image = pic)
            
                
            root = Tk()
            root.geometry('700x600+270+50')
            
            lab = Label(root, text = 'Name of the picture', font=('Times', 12))
            can = Canvas(root, width ='500', height = '400', bg ='white')
            lis = Listbox(root, height=20)
            
            pic = listdir('pictures/')
            
            for item in pic:
                lis.insert(0,item)
            
            pic.bind("<Double-Button-1>",Press)
            
            
            
            lis.focus()
            lab.pack()
            can.pack()
            lis.pack()
            
            root.mainloop()

            Comment

            • tharden3
              Contributor
              • Jul 2008
              • 916

              #7
              Originally posted by montana
              sorry guys, its my first time using this forum. here is my code properly displayed, thanks!

              Code:
              from Tkinter import *
              from os import *
              from string import *
              
              def Press():
                  selec=lis.curselection()
                  print selec
                  str = lis.get(selec[0])
                  print str
              
                  tem = can.create_image(x,y, image = pic)
              
                  
              root = Tk()
              root.geometry('700x600+270+50')
              
              lab = Label(root, text = 'Name of the picture', font=('Times', 12))
              can = Canvas(root, width ='500', height = '400', bg ='white')
              lis = Listbox(root, height=20)
              
              pic = listdir('pictures/')
              
              for item in pic:
                  lis.insert(0,item)
              
              pic.bind("<Double-Button-1>",Press)
              
              
              
              lis.focus()
              lab.pack()
              can.pack()
              lis.pack()
              
              root.mainloop()
              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!

              Comment

              • kaarthikeyapreyan
                New Member
                • Apr 2007
                • 106

                #8
                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("<Doub le-Button-1>",Press)
                pic is a list u cannot bind an action to a list instead try this

                Code:
                from Tkinter import *
                from os import *
                from string import *
                
                def Press(event):
                    selec=lis.curselection()
                    selected = lis.get(selec[0])
                    photo1 = PhotoImage(file=selected)
                    can.create_image(200, 100, image=photo1)
                
                root = Tk()
                root.geometry('700x600+270+50')
                
                lab = Label(root, text = 'Name of the picture', font=('Times', 12))
                can = Canvas(width =300, height = 300, bg ='white')
                lis = Listbox(root, height=20,selectmode=EXTENDED)
                
                pic = listdir('pictures/')
                for item in pic:
                    lis.insert(0,item)
                
                lis.bind("<Double-Button-1>", Press)
                
                lis.focus()
                lab.pack()
                can.pack()
                lis.pack()
                root.mainloop()

                Comment

                • montana
                  New Member
                  • Jul 2008
                  • 10

                  #9
                  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

                  Code:
                  from Tkinter import *
                  from os import *
                  from string import *
                  
                  #def Press(self):
                   #   selec=lis.curselection()
                    #  print selec
                     # str = lis.get(selec[0])
                      #print str
                      #tem = can.create_image(image = pic)
                  
                  
                  def Press(self):
                      selec=lis.curselection()
                      selected = lis.get(selec[0])
                      photo1 = PhotoImage(file=selected)
                      can.create_image(200, 100, image=photo1)
                  
                      
                  root = Tk()
                  root.geometry('700x600+270+50')
                  
                  lab = Label(root, text = 'Name of the picture', font=('Times', 12))
                  can = Canvas(root, width ='500', height = '400', bg ='white')
                  lis = Listbox(root, height=20)
                  
                  pic = listdir('pictures/')
                  
                  for item in pic:
                      lis.insert(0,item)
                  
                  lis.bind("<Double-Button-1>",Press)
                  
                  
                  
                  lis.focus()
                  lab.pack()
                  can.pack()
                  lis.pack()
                  
                  root.mainloop()
                  (())__ this is the error message below:
                  IDLE 1.2.2 ==== No Subprocess ====
                  >>>
                  Exception in Tkinter callback
                  Traceback (most recent call last):
                  File "C:\Python25\li b\lib-tk\Tkinter.py", line 1403, in __call__
                  return self.func(*args )
                  File "C:\Documen ts and Settings\Admini strator\Desktop \first python lessons\lecture 15.pyw", line 16, in Press
                  photo1 = PhotoImage(file =selected)
                  File "C:\Python25\li b\lib-tk\Tkinter.py", line 3270, in __init__
                  Image.__init__( self, 'photo', name, cnf, master, **kw)
                  File "C:\Python25\li b\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

                  Comment

                  • jlm699
                    Contributor
                    • Jul 2007
                    • 314

                    #10
                    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

                    Comment

                    • montana
                      New Member
                      • Jul 2008
                      • 10

                      #11
                      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?

                      Code:
                      #! usr/bin/python
                      
                      from Tkinter import *
                      from os import *
                      from string import *
                      
                      
                      def Press(event):
                          selec=lis.curselection()
                          selected = lis.get(selec[0])
                          photo1 = PhotoImage('file=selected')
                          can.create_image(200, 100, image=photo1)
                      
                          
                      root = Tk()
                      root.geometry('700x600+270+50')
                      
                      lab = Label(root, text = 'Name of the picture', font=('Times', 12))
                      can = Canvas(root, width ='500', height = '400', bg ='white')
                      lis = Listbox(root, height=20)
                      
                      pic = listdir('pictures/')
                      
                      for item in pic:
                          lis.insert(0,"pictures/"+item)
                      
                      lis.bind("<Double-Button-1>",Press)
                      
                      
                      
                      lis.focus()
                      lab.pack()
                      can.pack()
                      lis.pack()
                      
                      root.mainloop()

                      Comment

                      Working...