Python实现点击图片计时功能

from tkinter import *
import tkinter as tk
from PIL import Image,ImageTk
import os
import tkinter.messagebox
import time

master = Tk()

Label(master, text="请输入场景数").grid(row=0, column=0,sticky=W,padx=5, pady=5)

e1 = Entry(master)
e1.grid(row=0, column=1)

timestr = tkinter.StringVar()   #创建可变数据类型
timestr.set('0:0')   #只能数值不能等于号

running = False    #计时器工作状态
starttime = 0      #开始计时时间
elapsedtime = 0.0    #计时器统计到的时间
timer = None

# 声明函数
def warning():
    # 弹出对话框
    result = tkinter.messagebox.showwarning(title = 'warning',message='Please input less than 9 or you can add more images')
    # 返回值为:ok
    print(result)

def update():
    global elapsedtime
    global timestr
    global timer
    elapsedtime = time.time() - starttime
    elapsedtime=round(elapsedtime,4)
    timestr.set(elapsedtime)
    timer = master.after(50, update)

def start(self):
    global running  #全局变量
    global starttime
    if not running:
        starttime = time.time() - elapsedtime
        running = True
        update()
        pass

def stop(self):
    global running
    global timer
    if running:
        master.after_cancel(timer)
        elapsedtime = time.time() - starttime
        timestr.set(elapsedtime)
        running = False
        pass

# 记录button按下的次数
labelArray=[]
buttonArray=[]

def cbClicked(label):
    # print(label)
    start()
    msg.showinfo('您点击了', label)

def getTextInput():
    text=e1.get() #获取文本框内容
    print(text)
    fileDir='./resource/'
    pathDir = os.listdir(fileDir)    #取图片的原始路径
    if text.isdigit():
        cnt=int(float(text))
        if(cnt>8):
            print('Please input less than 9 or you can add more images')
            # 添加按钮
            # btn1 = tkinter.Button(master,text = 'warning',command = warning)
            # btn1.grid(row=2, column=0, sticky=W, padx=5, pady=5)
            return
        
        row=2
        column=0
        for i in range(1,cnt+1):
            labeli=Label(textvariable=timestr, fg="black", font=("微软雅黑", 30, "bold")).grid(row=row, column=column, sticky=W, padx=50, pady=5)
            labelArray.append(labeli)
            column+=1
            if(column>3):
                column=0
                row+=1
        
        row+=1
        column=0
        for i in range(1,cnt+1):
            imgPath=fileDir+str(i)+'.gif'
            print(imgPath)
            photo = PhotoImage(file=imgPath)
            label = Label(image=photo)
            label.image = photo
            # # button=Button(master, image=photo, command=start).grid(row=row, column=column, sticky=W, padx=5, pady=5)
            # buttonArray.append(button)
            button=Button(master, image=photo)
            button.grid(row=row, column=column, sticky=W, padx=5, pady=5)
            # print(type(Button(button)))
            button.bind("", start)
            button.bind("", stop)
            # buttonArray.append(button)
            column+=1
            if(column>3):
                column=0
                row+=1
        
        # for btn in buttonArray2:
        #     print(type(btn))
        #     btn.grid(row=row, column=column, sticky=W, padx=5, pady=5)
        #     btn.bind("", start)
        

button = Button(master, text='选择背景',command=getTextInput)
button.grid(sticky=W,padx=5, pady=5)

master.title('Sample')
master.geometry("1080x800")  # 窗口大小
mainloop()


你可能感兴趣的:(Python,python,开发语言)