0

I have a project that involves capturing an image using a push button and after I use image processing I can convert it into a string. How do I make the string display the tkinter text widget while still capturing an image this the code for the button and the tkinter.

class MainWindow(tk.Frame):
def __init__(self, *args, **kwargs):
    tk.Frame.__init__(self, *args, **kwargs)
    self.label = tk.Label(self,text="CHOOSE THE NUMBER OF PLAYERS THAT WILL BE PLAYING",
                         font="Helvetica 13 bold",
                         fg="white",
                         bg="#85C1E9").pack(fill=X,ipady=40)
    self.PlayerTwo=PhotoImage(file="2.png")
    self.PlayerThree=PhotoImage(file="3.png")
    self.PlayerFour=PhotoImage(file="4.png")


    self.button1 = tk.Button(self, text="CLICK HERE TO START THE GAME", 
                            command=self.create_window).pack(fill=X, ipady=20)
    self.button2 = tk.Button(self, image=self.PlayerTwo, command=self.pop_up1).pack(ipadx=15, side=LEFT, ipady=10)
    self.button3 = tk.Button(self, image=self.PlayerThree, command=self.pop_up2).pack(ipadx=15, side=LEFT, ipady=10)
    self.button4 = tk.Button(self, image=self.PlayerFour, command=self.pop_up3).pack(ipadx=15, side=LEFT, ipady=10)

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)

This is the image of my UI UI

The push button works now. But everytime I tried to capture an image it shoudl wait for me to press the button. How do I make isnide the def my_callback(channel): run only if I push the button.

  • I don't understand your last paragraph. Can you re-word that more clearly? Also it might help to include more of your code. – T. M. Jan 30 at 19:26
  • Is this a Raspberry Pi question? – MatsK Jan 31 at 14:57
  • Yes like how do I capture an image and run a class if I push a button – Xiao Sy Feb 1 at 5:41

Your Answer

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

Browse other questions tagged or ask your own question.