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')
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
labelArray=[]
buttonArray=[]
def cbClicked(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')
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)
button.grid(row=row, column=column, sticky=W, padx=5, pady=5)
button.bind("", start)
button.bind("", stop)
column+=1
if(column>3):
column=0
row+=1
button = Button(master, text='选择背景',command=getTextInput)
button.grid(sticky=W,padx=5, pady=5)
master.title('Sample')
master.geometry("1080x800")
mainloop()