0

I have a project in which involves capturing of images. I have to display a UI while capturing an image if a button is pressed. This is the code.

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')

        global globvar

        globvar = -1

        proc = ImageProcess()

        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.50:

                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 == 0:
                    text1.insert(tk.INSERT,example_dict[globvar])
                if player_number == 1:
                    text2.insert(tk.INSERT,example_dict[globvar])

            time_stamp = time_now

       GPIO.add_event_detect(21, GPIO.RISING, callback=my_callback, bouncetime=500)
        time.sleep(1)

The code works but for some reason after I click the button it always captures the image without pressing the button. Can someone help me?

0

Are you sure it's pin 21 in the mode you're using? (BCM/BOARD) See pinout.xyz

Are you sure your button is wired in a pull-down circuit (3V3->GPIO)?

A pull-up circuit (GND->GPIO) means a press triggers a falling edge and release triggers a rising edge.

You should test your button press on its own (no GUI) to see if it behaves as you expect with a simple print as your callback.

Aside: You might find it a lot easier with gpiozero and guizero.

  • It works fine when I try with the button itself it only print the word inside it but when I tried capturing an image after I click it always captures the image without the need of pressing the button. – James Wu Feb 10 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.