-1

I am making a project using a raspberry pi. In this project, I have to capture an image and go in a process of computer vision. I created a UI and in this UI, for after I click a button widget I use the push button hardware to call a particular class. This is the code I have done so far.

camera = PiCamera()
camera.resolution = (320,320)

GPIO.setmode(GPIO.BCM)

GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
time_stamp = time.time() 

def create_window(self):
        t = tk.Toplevel(self)
        t.geometry("480x320")
        t.wm_title("Game")
        text_file = open("players.txt", "r")
        message = text_file.read()
        self.players = message.split()

        if len(self.players) == 2:
            text_file = open("players.txt", "r")
            message = text_file.read()
            players = message.split()
            Player1 = self.players[0]  
            Player2 = self.players[1]
            player1 = tk.Label(t,
                                 text=Player1,
                                 font="Times 45",
                                 fg="white",
                                 bg="#0000FF")
            #player1.pack(side='left', padx=10, pady=10, anchor= N)
            player1.grid(row=0, column=0)
            player1.config(width=8)


            player2 = tk.Label(t,
                             text=Player2,
                             font="Times 45",
                             fg="white",
                             bg="#FF0000")

            player2.grid(row=0, column=1)
            player2.config(width=8)


            text1 = tk.Text(t, height=30, width=30)
            text1.grid(column=0, row=1, sticky='N')

            text2 = tk.Text(t, height=30, width=30)
            text2.grid(column=1, row=1, sticky='N')

            proc = ImageProcess()

            global globvar

            globvar = 0

            def my_callback(channel):  
                global time_stamp       # put in to debounce
                global globvar
                globvar += 1
                time_now = time.time()  
                if (time_now - time_stamp) >= 0.3:  
                    camera.capture('/home/pi/Thesis/Test/lets.jpg')
                    img = cv2.imread("/home/pi/Thesis/Test/lets.jpg")

                    print(proc.frame_table(img))

                    player_number = globvar%len(self.players)

                    print(player_number)

                    pickle_in = open("game_file.pickle","rb")
                    example_dict = pickle.load(pickle_in)

                    if player_number == 1:
                        text1.insert(tk.INSERT,example_dict[0])
                    if player_number == 0:
                        text2.insert(tk.INSERT,example_dict[1])

                time_stamp = time_now 


            GPIO.wait_for_edge(21, GPIO.FALLING)


            GPIO.add_event_detect(21, GPIO.FALLING, callback=my_callback)

The expected output that I want is for every click the push button it must capture an image and process to a class. How do I make the push button wait so that after it run the class it will wait for me to press the button and capture the image.

  • You don't call a class (you may call a function in a class instance) but I don't see any classes. In fact there does seem to be any executable code. Embedded function definitions are not a good idea. As far as I can see this would define a function and exit. – Milliways Feb 3 at 7:41
  • @Milliways Can you show how to make a push button capture an image only if I click the button. Otherwise, if the button is not click nothing will happen. – Xiao Sy Feb 3 at 9:18
  • This is a general programming question and not specific to Raspberry Pi. You should better ask at stackoverflow.com – Ingo Feb 3 at 13:17
  • does anything happen when you press the button? ..... it seems that you should be asking how do i detect a button press? ...... worry about calling a function after the button code works – jsotola Feb 3 at 19:34

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.