倒计时在Tkinter中迟到

时间:2019-02-14 21:31:05

标签: python python-3.x tkinter raspberry-pi raspberry-pi3

使用Raspberry Pi和Python进行photobooth项目。当前正在对GUI进行编码,并且当我调用倒数计时时,尽管我在捕获图片之前调用了countdown(),但程序在运行倒数计时之前会拍摄三张图片。

#import statements
import tkinter
import picamera
import time
from picamera import PiCamera
from tkinter import *

#Setup proper rotation and dimensions for live camera preview
camera = PiCamera()
camera.rotation = 270
camera.start_preview()
camera.preview_fullscreen = False
camera.preview_window = (420, 120, 380, 300)
counter = 0

def countdown(count):
    # change text in label        
    textbox['text'] = count
    if count > 1:
        # call countdown again after 1000ms (1s)
        top.after(1000, countdown, count-1)

#configures the gui window
top = tkinter.Tk()
#sets background color to green
top.configure(background = 'green')
#configures dimensions for window
top.geometry('800x480+0+0')
textbox = tkinter.Label(top, bg = "green", fg = "white", font = ('Comic Sans MS', 15))
textbox.pack()
textbox.place(x=187, y=125)

#defines the command to take multiple pictures
def takePicture():
#sets up variables for loop
    counter = 0
    number = 0
#sets up loop to take 3 pictures
    while counter < 3:
        countdown(4)
#Sets up camera and saves a picture to the destination       
        camera.resolution = (1280, 720)
        camera.capture("/home/pi/Desktop/Image" + str(number) + ".jpg")
#Adds to the variable to keep loop running until 3 pictures are taken
        counter += 1
        number += 1

#configures the button text, command, and font
B = tkinter.Button(top, text ="Take Photo!", command = takePicture, font = ('Comic Sans MS',15), bg = 'white', fg = 'green')
#allows the button to work
B.pack()
#configures the positioning and dimensions of the button
B.place(x=150, y=185, width=125, height=75)

#loops the GUI window so it doesn't close
top.mainloop()
#stops camera preview
camera.stop_preview()

0 个答案:

没有答案