当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Get image name with help of click coordinate

问题描述:

I am making Memory Puzzle and I have problem with that I don't know how to compare if two clicked images are same. I thought of to get the image name with help of click coordinate to compare with second click, but I don't now how to go on with that. My code is:

from tkinter import Tk, W, E

from tkinter import ttk

from tkinter.ttk import*

from tkinter import*

from random import choice

from PIL import ImageTk

from PIL import Image

import pygame

class Application(Frame):

def __init__(self, master):

super (Application, self).__init__(master)

self.grid()

self.firstSelection=None

self.secoundSelection=None

self.create_widgets()

def create_widgets(self):

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.label1.grid(row=0, column=0,columnspan=3, rowspan=3, padx=20, pady=20)

self.label2 = Label(self, width=100, height=100)

self.label2.grid(row=0, column=4, columnspan=3, rowspan=3, padx=20, pady=20 )

self.label3 = Label(self, width=100, height=100)

self.label3.grid(row=0, column=8, columnspan=3, rowspan=3 , padx=20, pady=20)

self.label4 = Label(self, width=100, height=100)

self.label4.grid(row=4, column=0, columnspan=3, rowspan=3, padx=20, pady=20 )

self.label5 = Label(self, width=100, height=100)

self.label5.grid(row=4, column=4, columnspan=3, rowspan=3, padx=20, pady=20 )

self.label6 = Label(self, width=100, height=100)

self.label6.grid(row=4, column=8, columnspan=3, rowspan=3, padx=20, pady=20 )

self.randi=[None,None,None] # first list of randomly pictures

self.randi2=[None,None,None]

self.allLabel1=[self.label1 ,self.label2 , self.label3]

self.allLabel2=[self.label4, self.label5, self.label6 ]

self.allImage1=[None,None,None]

self.allImage2=[None,None,None]

# sort ther pictures randomly in two lists to use the latre to compare if them är same

for i in range(3):

self.rand=choice(self.im)

self.randi[i]=self.rand

self.randi2[i]=self.rand

self.im.remove(self.rand)

# put the first three pictures in labels (1,2,3)

for i in range (3):

self.rand=choice(self.randi)

self.img1 = ImageTk.PhotoImage( Image.open(self.rand).resize((300,300)))

self.allImage1[i]=self.img1 # save all opened images in a list

self.randi.remove(self.rand)

self.allLabel1[i]['image']= self.allImage1[i]

# put the first three pictures in labels (4,5,6)

for i in range (3):

self.ran=choice(self.randi2)

self.img = ImageTk.PhotoImage( Image.open(self.ran).resize((300,300)))

self.allImage2[i]=self.img # save all opened images in a list

self.randi2.remove(self.ran)

self.allLabel2[i]['image']= self.allImage2[i]

for i in range (3):

self.allLabel1[i].bind('<Button-1>',self.Event1) and self.allLabel2[i].bind('<Button-1>',self.Event1)

def Event1(self,event):

if self.firstSelection==None:

self.firstSelection=(event.x, event.y)

print (" firstSelection ", self.firstSelection)

else:

self.secoundSelection=(event.x, event.y)

print(" secoundSelection ", self.secoundSelection)

if self.allImage1== self.allImage2:

print ("Yes, They Matched ")

self.pack()

root = Tk()

root.title("Python snackes")

root.geometry("640x603")

app= Application(root)

root.mainloop()

If my code is right how can I get the name of image with help of its coordinate. But if my thought isn't right so how can I do that?

If you run the code so nothing happend!! Help me please!

分享给朋友:
您可能感兴趣的文章:
随机阅读: