Python: How to use a picture list to view in a label list
I made 4 labels and I have another list of 16 Pictures. I want to choice 4
pictures of the list and appear them in the labels. My problem is how can
I appear these pictures in labels which is in the list. I want when the
program starts again so another picture will appear in in every label. My
code is:
from tkinter.ttk import*
from tkinter import*
from random import choice
from PIL import ImageTk
from PIL import Image
import pygame
from collections import defaultdict
class Application(Frame):
def __init__(self, master):
super (Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.columnconfigure(0, pad=3)
self.columnconfigure(1, pad=3)
self.rowconfigure(0, pad=3)
self.rowconfigure(1, pad=3)
self.im=["a.png","b.png","c.png","d.png", "e.png", "f.png", "g.png",
"h.png","i.png","j.png","k.png","l.png","m.png","n.png","o.png","p.png"
]
self.label1 = Label(self, width=100, height=100)
self.label2 = Label(self, width=100, height=100)
self.label3 = Label(self, width=100, height=100)
self.label4 = Label(self, width=100, height=100)
self.allLabel=[ self.label1 ,self.label2 , self.label3 , self.label4]
for i in range(4):
self.rand=choice(self.im)
self.img1 = ImageTk.PhotoImage(
Image.open(self.rand).resize((300,300)))
self.allLabel[i]['image']= self.img1
root = Tk()
root.title("Python snackes")
root.geometry("640x603")
app= Application(root)
root.mainloop()
when I run these code nothing appear in the frame. I don't now where is
the problem, can somebody help me with this :)
No comments:
Post a Comment