Barre d'icone

Bonjour,

Je suis débutant, j'utilise python, 2.7.6 tkinter.
Je voudrai savoir pourquoi losque je met ma barre d'icone dans une fonction, ça marche pas. impossible de voir les imaes des icone et impossible d'effectuer un clique.
Sans fonction ça marche !!!

Merci.

def barre_icones():
draw = Canvas(width=1400, height=800, scrollregion = (0,0, 230, 5800))
draw.sbar = Scrollbar(orient=VERTICAL)
frame = Frame(draw)
draw.create_window(0, 0, window=frame, width=1400, height=5800, anchor=N+W)
#Entry(frame, width=30).pack(side=TOP)

#toolbar = Frame(fp, bg="blue")
toolbar = Frame(fp)

fichier1 = "/home/laurent/python/m3u-free/exit_32x32.gif"
icon1 = PhotoImage(file=fichier1)
button1 = Button(toolbar, command=fp.destroy, image=icon1, justify = RIGHT)
button1.pack(side=LEFT)
#Label(toolbar, text="Quitter").pack()

icon2 = PhotoImage(file="/home/laurent/python/m3u-free/aide_32x32.gif")
button2 = Button(toolbar, command=aide, image=icon2, justify = RIGHT)
button2.pack(side=LEFT)
#Label(toolbar, text="Options").pack()

icon3 = PhotoImage(file="/home/laurent/python/m3u-free/information_32x32.gif")
button3 = Button(toolbar, command=a_propos, image=icon3, justify = RIGHT)
button3.pack(side=LEFT)
#Label(toolbar, text="Options").pack()

toolbar.pack(side=TOP, fill=X)

barre_icones()


8 réponses

  1. Pour ton premier code tu as redéfinie tes variables bouton1, bouton2, boution3 et donc c'est normal que l’icône ne s'affiche pas.

    Sinon faut voir si j'ai le temps de tester à un moment… pour trouver ton problème…

    Edit: Ça donne quoi le même principe que Label ?

    image1  = Image.open('/home/laurent/python/m3u-free/exit_32x32.gif')
    photo1  = ImageTk.PhotoImage(image1)
    button1 = Button(toolbar, image=photo1, justify=RIGHT, command=fp.destroy)
    button1.image = photo
    button1.pack(side=LEFT)
    0
    1. 1/ OK mais avec ceci :

      def config_fenetre_principale():
      global fp
      fp = Tk() # # Création de la fenêtre principale
      fp.title( "Playlist Free" ) # Titre de la fenetre
      fp['bg']='bisque' # couleur de fond de la fenetre
      #icone barre de titre (Fonctionne avec le format gif/png)
      img = Tkinter.Image("photo", file="/home/laurent/python/m3u-free/playlist-free_24x24.png")
      fp.tk.call('wm','iconphoto',fp._w,img)

      def barre_icones():
      print "barre icone ok"
      toolbar = Frame(fp)

      image1 = Image.open('/home/laurent/python/m3u-free/exit_32x32.gif')
      photo1 = ImageTk.PhotoImage(image1)
      button1 = Button(toolbar, image=photo1, justify = RIGHT)
      button1 = Button(toolbar, command=fp.destroy)
      button1.pack(side=LEFT)

      image2 = Image.open('/home/laurent/python/m3u-free/aide_32x32.gif')
      photo2 = ImageTk.PhotoImage(image2)
      button2 = Button(toolbar, image=photo2, justify = RIGHT)
      button2 = Button(toolbar, command=aide)
      button2.pack(side=LEFT)

      image3 = Image.open('/home/laurent/python/m3u-free/information_32x32.gif')
      photo3 = ImageTk.PhotoImage(image3)
      button3 = Button(toolbar, image=photo3, justify = RIGHT)
      button3 = Button(toolbar, command=aide)
      button3.pack(side=LEFT)

      toolbar.pack(side=TOP, fill=X)


      La barre d'icone s'affiche et le clique fonctionne sur chaque bouton mais l'image (icone) ne s'affiche pas dans les boutons ?

      2/ Et là

      def config_fenetre_principale():
      global fp
      fp = Tk() # # Création de la fenêtre principale
      fp.title( "Playlist Free" ) # Titre de la fenetre
      fp['bg']='bisque' # couleur de fond de la fenetre
      #icone barre de titre (Fonctionne avec le format gif/png)
      img = Tkinter.Image("photo", file="/home/laurent/python/m3u-free/playlist-free_24x24.png")
      fp.tk.call('wm','iconphoto',fp._w,img)

      def barre_icones():
      toolbar = Frame(fp)

      fichier1 = '/home/laurent/python/m3u-free/exit_32x32.gif'
      photo1 = PhotoImage(file=fichier1)
      button1 = Button(toolbar, command=fp.destroy, image=photo1, justify = RIGHT)
      button1.pack(side=LEFT)
      #Label(toolbar, text="Quitter").pack()

      photo2 = PhotoImage(file='/home/laurent/python/m3u-free/aide_32x32.gif')
      button2 = Button(toolbar, command=aide, image=photo2, justify = RIGHT)
      button2.pack(side=LEFT)
      #Label(toolbar, text="Options").pack()

      photo3 = PhotoImage(file='/home/laurent/python/m3u-free/information_32x32.gif')
      button3 = Button(toolbar, command=a_propos, image=photo3, justify = RIGHT)
      button3.pack(side=LEFT)
      #Label(toolbar, text="Options").pack()

      toolbar.pack(side=TOP, fill=X)


      n 'y le clique fonctionne, et n'y l'icone s'affiche dans les boutons

      Pourquoi ?
      Ou est l'erreur ?

      Merci.
      0
      1. Les labels sont destinés à l'affichage, ils n'ont pour vocation première d’interagir sur la partie événementielle, même s'il est possible de le faire via les events.

        Pour cela, il y a les buttons qui sont justement destinés à cet usage.
        0
        1. Bonsoir,

          De quel manière alors ?

          Quelle interer de faire une barre d'icones si au clique rien est possible ?????

          Merci.
          0
          1. Selon la doc :
            http://effbot.org/tkinterbook/label.htm

            Il n'existe pas de « command » comme option pour Label.

            Donc tu dois t'y prendre d'une autre manière…
            0
            1. Bon soir,

              ok ça marche
              def barre_icones():
              print "barre de menu icone ok"

              #toolbar = Frame(fp, bg="blue")
              toolbar = Frame(fp)

              image = Image.open('/home/laurent/python/m3u-free/exit_32x32.gif')
              photo = ImageTk.PhotoImage(image)
              label = Label(toolbar, image=photo, justify = RIGHT)
              label.image = photo # keep a reference!
              label.pack(side=LEFT) #(side=LEFT)

              image = Image.open('/home/laurent/python/m3u-free/aide_32x32.gif')
              photo = ImageTk.PhotoImage(image)
              label = Label(toolbar, image=photo, justify = RIGHT)
              label.image = photo # keep a reference!
              label.pack(side=LEFT) #(side=LEFT)

              image = Image.open('/home/laurent/python/m3u-free/information_32x32.gif')
              photo = ImageTk.PhotoImage(image)
              label = Label(toolbar, image=photo, justify = RIGHT)
              label.image = photo # keep a reference!
              label.pack(side=LEFT) #(side=LEFT)

              toolbar.pack(side=TOP, fill=X)


              mais dès que je rajoute :
              command=fp.destroy
              commme ceci
              label = Label(toolbar, command=fp.destroy, image=photo, justify = RIGHT)

              pour indiquer la commande a effectuer a un clique j'ai l'erreur suivante
              laurent@laurent-PC-Bureau:~$ python /home/laurent/python/m3u-free/m3u-free-3.py
              barre de menu icone ok
              Traceback (most recent call last):
              File "/home/laurent/python/m3u-free/m3u-free-3.py", line 442, in <module>
              executer()
              File "/home/laurent/python/m3u-free/m3u-free-3.py", line 434, in executer
              barre_icones2()
              File "/home/laurent/python/m3u-free/m3u-free-3.py", line 329, in barre_icones2
              label = Label(toolbar, command=fp.destroy, image=photo, justify = RIGHT)
              File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2559, in __init__
              Widget.__init__(self, master, 'label', cnf, kw)
              File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2058, in __init__
              (widgetName, self._w) + extra + self._options(cnf))
              _tkinter.TclError: unknown option "-command"
              laurent@laurent-PC-Bureau:~$


              Comment faire ?

              Merci.
              0
              1. La variable « fb », elle est définie où ? Parce que je ne la vois pas dans ton code…
                0
                1. Il faut référencer tes images dans tkinter.
                  Soit dans la fenêtre root, soit dans tes widgets.

                  Voir http://effbot.org/tkinterbook/photoimage.htm
                  Tout en bas de la page.
                  0