Roshan Hemrajani
Roshan Hemrajani

Reputation: 21

How to detect button in self running loop in python + tkinter

I have one little program with 2 buttons. I have also one self running loop in every 60 ms. My problem is when click start_camera button , it will set one bool(ok) = true when I click stop_camera button , it will set bool(ok) = false & I want to test this bool(ok) in self running loop.

import tkinter
class App():
    def __init__(self, window, window_title):
        self.window = window
        self.window.title = window_title

        self.opencamera = tkinter.Button(window, text="open camera", command=self.open_camera)
        self.opencamera.pack()
        self.closecamera = tkinter.Button(window, text="close camera", command=self.close_camera)
        self.closecamera.pack()
        self.delay = 60
        self.update()
        # After it is called once, the update method will be automatically called every delay milliseconds
        self.window.mainloop()

    def update(self):
        print("60 ms found")
        self.window.after(self.delay, self.update)


    def open_camera(event):
        print("camera opened")
        # save the file

    def close_camera(event):
        print("camera closed")



App(tkinter.Tk(), "mywindow")

Upvotes: 0

Views: 81

Answers (1)

Hein Wessels
Hein Wessels

Reputation: 947

import tkinter
class App():
    def __init__(self, window, window_title):
        self.window = window
        self.window.title = window_title

        self.opencamera = tkinter.Button(window, text="open camera", command=self.open_camera)
        self.opencamera.pack()
        self.closecamera = tkinter.Button(window, text="close camera", command=self.close_camera)
        self.closecamera.pack()
        self.delay = 60
        self.update()
        # After it is called once, the update method will be automatically called every delay milliseconds

        self.okay = False ##################################################

        self.window.mainloop()

    def update(self):
        print("60 ms found")
        self.window.after(self.delay, self.update)
        print(self.okay) ##################################################


    def open_camera(self): ##################################################
        print("camera opened") 
        self.okay = True ##################################################
        # save the file

    def close_camera(self): ##################################################
        self.okay = False ##################################################
        print("camera closed")   


App(tkinter.Tk(), "mywindow")

Edit: Updated little mistake.

Upvotes: 1

Related Questions