Python:How to arrange pictures randomly in a table?
I have to make a memory puzzle in a cours. I have a list with all pictures
in it. My problem is how can I arrange these pictures in a table randomly.
from tkinter import*
from random import choice
from PIL import ImageTk
from PIL import Image
class Application(Frame):
def __init__(self, master):
super (Application, self).__init__(master)
self.initUI()
def initUI(self):
self.columnconfigure(0, pad=3)
self.columnconfigure(1, pad=3)
self.columnconfigure(2, pad=3)
self.rowconfigure(0, pad=3)
self.rowconfigure(1, pad=3)
self.rowconfigure(2, pad=3)
self.im=["1.png","2.png","3.png","4.png", "5.png", "6.png", "7.png",
"8.png"]
self.label1 = Label(self)
self.label1.grid(row=0, column=0)
self.label2 = Label(self)
self.label2.grid(row=0, column=1)
self.label3 = Label(self)
self.label3.grid(row=1, column=0)
self.label4 = Label(self)
self.label4.grid(row=1, column=1)
self.label5 = Label(self)
self.label5.grid(row=2, column=0)
self.label6 = Label(self)
self.label6.grid(row=2, column=1)
for element in im:
self.img = ImageTk.PhotoImage( Image.open(choice(self.im)))
self.label1['image']= self.img
self.img = ImageTk.PhotoImage( Image.open(choice(self.im)))
self.label2['image']= self.img
self.img = ImageTk.PhotoImage( Image.open(choice(self.im)))
self.label3['image']= self.img
self.img = ImageTk.PhotoImage( Image.open(choice(self.im)))
self.label4['image']= self.img
self.img = ImageTk.PhotoImage( Image.open(choice(self.im)))
self.label5['image']= self.img
self.img = ImageTk.PhotoImage( Image.open(choice(self.im)))
self.label6['image']= self.img
root = Tk()
app = Application(root)
root.mainloop()
Please help me with this, how can I arrange pictures in a table randomly.
No comments:
Post a Comment