affichage d'images avec Tkinter
Bonsoir,
je sais que ma question n'est pas nouvelle, mais je n'ai pas trouvé de réponse satisfaisante en navigant sur la toile. Depuis la fenêtre principale (fenetrage) j'ouvre une fenêtre Toplevel (affiche) dans laquelle je veux afficher une image. Le programme suivant affiche un Toplevel vide :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #!/usr/bin/python
# -*- coding: utf-8 -*-
from Tkinter import *
import tkMessageBox
import os, sys
import codecs
from PIL import ImageTk, Image
eol = "\n"
racine = "/home/jam/python/prog/histoire/fichiers/cartes/"
def fenetrage():
fenetre = Tk()
lancer = Button(fenetre, text="Afficher", command=affiche)
lancer.grid(column=0, row=0)
def affiche():
global images
fen = Toplevel()
img = ImageTk.PhotoImage(Image.open(racine + images[0]))
panel = Label(fen, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
if __name__ == "__main__": # création du main
# déterminer l'utilisateur
global images
images = os.listdir(racine)
fenetrage()
mainloop() |
Pour afficher l'image, je découpe la fonction "affiche" en deux comme indiqué ci-dessous, et l'image s'affiche. Y a-t-il une solution plus élégante pour un aussi simple affichage ?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #!/usr/bin/python
# -*- coding: utf-8 -*-
from Tkinter import *
import tkMessageBox
import os, sys
import codecs
from PIL import ImageTk, Image
eol = "\n"
racine = "/home/jam/python/prog/histoire/fichiers/cartes/"
def fenetrage():
fenetre = Tk()
lancer = Button(fenetre, text="Afficher", command=affiche)
lancer.grid(column=0, row=0)
def affiche():
global images, panel
fen = Toplevel()
img = ImageTk.PhotoImage(Image.open(racine + images[0]))
affiche1(fen, img)
def affiche1(fen, img):
global panel
panel = Label(fen, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
if __name__ == "__main__": # création du main
# déterminer l'utilisateur
global images
images = os.listdir(racine)
fenetrage()
mainloop() |
Merci pour votre aide