如何跟踪是否已单击按钮

时间:2019-05-06 14:42:39

标签: python image button tkinter

我正在做一个记忆游戏,我不知道如何跟踪是否已单击一个按钮。如果两个图像在单击时不匹配,我希望这些图块更改回默认图像,但如果它们匹配则保留为新图像。

import tkinter as tk
from tkinter import ttk
from tkinter import *
from PIL import Image, ImageTk
import random
import tkinter.font
from tkinter import messagebox
import time



class MemoryBricks:
    def __init__(self, master):     
        frame=Frame(master)
        frame.grid() 
        master.title("Let's play Kitten Memory!")
        master.iconbitmap(r"default.ico")


        self.button1=ttk.Button(root)
        self.button1.grid(row = 0, column = 0)
        self.photo1 = tk.PhotoImage(file="defaultPic.png")
        self.button1.config(image=self.photo1, command=lambda: showImage(1))

        self.button2=ttk.Button(root)
        self.button2.grid(row=0, column=1)
        self.photo2 = tk.PhotoImage(file="defaultPic.png")
        self.button2.config(image=self.photo2, command=lambda: showImage(2))

        self.button3=ttk.Button(root)
        self.button3.grid(row=0, column=2)
        self.photo3 = tk.PhotoImage(file="defaultPic.png")
        self.button3.config(image=self.photo3, command=lambda: showImage(3))

        self.button4=ttk.Button(root)
        self.button4.grid(row=1, column=0)
        self.photo4 = tk.PhotoImage(file="defaultPic.png")
        self.button4.config(image=self.photo4, command=lambda: showImage(4))

        self.button5=ttk.Button(root)
        self.button5.grid(row=1, column=1)
        self.photo5 = tk.PhotoImage(file="defaultPic.png")
        self.button5.config(image=self.photo5, command=lambda: showImage(5))

        self.button6=ttk.Button(root)
        self.button6.grid(row=1, column=2)
        self.photo6 = tk.PhotoImage(file="defaultPic.png")
        self.button6.config(image=self.photo6, command=lambda: showImage(6))


        class showImage(MemoryBricks):
            def __init__(self, master):

                if master == 1:
                    load1 = Image.open('Kittens1.GIF')
                    load1Size = load1.resize((145,145))
                    render1 = ImageTk.PhotoImage (load1Size)           
                    img1 = Label(image=render1)
                    img1.image = render1 
                    img1.grid(row = 0, column = 0)



                if master == 2:
                    load1 = Image.open('Kittens2.GIF')
                    load1Size = load1.resize((145,145))
                    render1 = ImageTk.PhotoImage (load1Size)           
                    img1 = Label(image=render1)
                    img1.image = render1                
                    img1.grid(row=0, column=1)   



                if master == 3:
                    load1 = Image.open('Kittens3.GIF')
                    load1Size = load1.resize((145,145))
                    render1 = ImageTk.PhotoImage (load1Size)           
                    img1 = Label(image=render1)
                    img1.image = render1                 
                    img1.grid(row=0, column=2)  



                if master == 4:
                    load1 = Image.open('Kittens1.GIF')
                    load1Size = load1.resize((145,145))
                    render1 = ImageTk.PhotoImage (load1Size)           
                    img1 = Label(image=render1)
                    img1.image = render1                 
                    img1.grid(row=1, column=0)  



                if master == 5:
                    load1 = Image.open('Kittens2.GIF')
                    load1Size = load1.resize((145,145))
                    render1 = ImageTk.PhotoImage (load1Size)           
                    img1 = Label(image=render1)
                    img1.image = render1                 
                    img1.grid(row=1, column=1)  


                if master == 6:
                    load1 = Image.open('Kittens3.GIF')
                    load1Size = load1.resize((145,145))
                    render1 = ImageTk.PhotoImage (load1Size)           
                    img1 = Label(image=render1)
                    img1.image = render1                 
                    img1.grid(row=1, column=2)

#skapar fönstret
root=Tk()  
start_time = time.time()

#root blir master 
mem=MemoryBricks(root)

root.mainloop()


elapsed_time = time.time() - start_time

print("Your time is", elapsed_time)

我认为我可能必须创建一个新类或一个函数。还是有人对如何轻松实现此操作有任何建议?

1 个答案:

答案 0 :(得分:1)

您可以有一个辅助变量,该变量将充当标志。

例如将其定义为self.flag = False。单击按钮时,以True(self.flag = True)更改变量的值,这是跟踪按钮是否被按下的方法

例如,您要跟踪是否单击了button1:

 class MemoryBricks:
      def __init__(self, master):
           # Define "flag variable" for button1
           self.flag_btn1 = False

在单击button1时将执行的功能(或代码的一部分)中,更改标志的状态:

 class showImage(MemoryBricks):
      def __init__(self, master):
           if master == 1:
                # Change state of the flag
                self.flag_btn1 = True
                load1 = Image.open('Kittens1.GIF')
                load1Size = load1.resize((145, 145))
                render1 = ImageTk.PhotoImage(load1Size)
                img1 = Label(image=render1)
                img1.image = render1
                img1.grid(row=0, column=0)

现在您可以检查是否单击了button1:

例如,在某些信号/动作上:

 if self.flag_btn1: # True
      print("button1 was clicked")
 else: # False
      print("button1 wasn't clicked")