-2

I want to break a loop with the same button I started it. The code below doesn't work because it ends the loop after "sleep(2)".

I know that "camera.capture_continuous" is specific for the PiCamera but maybe someone can still help me to find a solution. ;)

import tkinter as tk
from picamera import PiCamera
root = tk.Tk()
camera = PiCamera()




def start_tl():
    if rec_btn.config('text')[-1] == 'START':
        rec_btn.config(text='STOP')
        for filename in camera.capture_continuous('/tl_img{counter:03d}.jpg', use_video_port=True):
            sleep(2)
            if rec_btn.config('text')[-1] == 'START':
                break

    
             
rec_btn = tk.Button(root,text="START", command=start_tl)
rec_btn.pack()



root.mainloop()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • 1
    What do you mean by "break a loop with a button"? – zr0gravity7 Jul 06 '20 at 20:34
  • I want to break the "for filename in camera...." loop (edit: I´m not sure if it´s called "a loop") – Neldela Jul 06 '20 at 20:35
  • (it is a loop). I understand that, I'm confused about the "... with a button part". – zr0gravity7 Jul 06 '20 at 20:39
  • I start the loop by pressing the "rec_btn" and after some time I wan´t to "break" it by pressing the same "rec_btn" again. – Neldela Jul 06 '20 at 20:43
  • Why would you do that? Is there a reason for? – Thingamabobs Jul 06 '20 at 20:44
  • @Atlas435 It´s just because I wan´t to have a simple clean GUI. – Neldela Jul 06 '20 at 20:45
  • You need to look into asynchronous code. Essentially you want to attach a callback to a Button, so that when it is clicked the callback is executed. In that callback, you want to attach another callback to the button, so that if the button is again clicked while handling the first callback, the second callback is executed. Before the first callback returns, you want to unbind the second callback (in case it was not clicked while the first callback was executing). I am not sure how you would do asynchronous callbacks using tkinter. If possible I would suggest using JS, especially Node.js – zr0gravity7 Jul 06 '20 at 20:53
  • 1
    @zr0gravity7 thanks for your efforts! :) But I don´t know JavaScript and I´ve already written the rest of the program using tkinter. – Neldela Jul 06 '20 at 21:11

1 Answers1

-1

I have not worked with the picamera, so that line is commented out, but try the following, you will have to use threading otherwise your main UI thread will be locked up. I added another button and a label just to help show that the process is running on a separate thread.

import tkinter as tk
from time import sleep
import threading

root = tk.Tk()

def start_tl():
    if rec_btn['text'] == 'STOP':
        t.join(1)
        rec_btn.config(text='START')
        return

    if rec_btn['text'] == 'START':
        t.start()
        rec_btn.config(text='STOP')
        return
def exit_tl():
    root.destroy()

def process_file():
    i = 1
    while rec_btn['text'] == 'STOP':
        i = i+1
        rec_btn.update()
        sleep(5)
        rec_lbl["text"] = "{}".format(i)
        # for filename in camera.capture_continuous('/tl_img{counter:03d}.jpg', use_video_port=True):
            #do something with the file??


rec_btn = tk.Button(root, text="START", command=lambda: start_tl())
rec_btn_exit = tk.Button(root, text="Exit", command=lambda: exit_tl())
rec_lbl = tk.Label(root,text="")
rec_btn.pack()
rec_btn_exit.pack()
rec_lbl.pack()

t = threading.Thread(target=process_file)

root.mainloop()

if you need your thread to be able to start, stop, start, stop have a look at this question and answers.

CobyC
  • 2,058
  • 1
  • 18
  • 24
  • thank you! :) That perfectly solved my problem that the GUI freezes when the loop is active. It didn´t work to stop the timelapse with the same button, but I added "camera.close()" to the "if rec_btn..." and this will do the job for me. – Neldela Jul 07 '20 at 08:00