Good evening: I speak from Colombia, the problem is that I am doing a project in which I must have an object counter that passes a sensor. Well, I hope I can explain myself: code1 works perfectly, what a button connected to GPIO 24 does is simple and every time you press it it prints 1 in 1 in an ascending way, but this is done from the console. the question is how can I get this value printed by the interface or Gui from tkinter on a label,

Try to do it with code 2 but it does not work

codigo 1

import RPi.GPIO as GPIO #libreria to control the pins

import time #libreria for the use of time (sleep)

GPIO.setmode (GPIO.BCM)

GPIO.setup (24, GPIO.IN)

GPIO.setwarnings (False)

cont = 0

while True:

         inputValue = GPIO.input (24)

             if (inputValue == False):

                  cont = cont + 1

         print ("pressed button" + str (cont))

         time.sleep (.5)

             time.sleep (.01)

codigo 2

import RPi.GPIO as GPIO #libreria para controlar los pines
import time #libreria para el uso de tiempo (sleep)

from tkinter import*

GPIO.setup(23, GPIO.IN)


contador = StringVar()
contador.set("0"+" U/s")

label1=Label(raiz, background="#00dbde", textvariable=contador, font=("Helvetica", 40,"bold"))

label1.place(x=20, y=10)

raiz.attributes("-fullscreen", True)


def exit():

def leeboton():
    while True:
            inputValue = GPIO.input(23)
            if(inputValue == True):
                raiz.after(2000, leeboton)
                contador.set(str(int(cont))+" U/s")

raiz.after(2000, leeboton)

only the GUI comes out printing this variable "   contador.set ("0" + "U / s") "but does nothing else when pressing the button

  • 2
    three things .... one: you do not need to apologize for the English, because it is not your fault (your English is just fine) ..... it is enough to say that you are not an English speaker ... two: you did not say if the program works or if it has any errors .... three: you did not ask a question – jsotola Jan 25 '19 at 4:35
  • This site works best when you try to debug your program yourself and get stuck with a particular error or unexpected behavior, which you describe in details. That's the main issue with your question. – Dmitry Grigoryev Jan 25 '19 at 7:40
  • You will find this much easier using gpiozero and guizero gpiozero.readthedocs.io/en/stable lawsie.github.io/guizero – ben_nuttall Jan 25 '19 at 20:53

Tkinter programs need to process UI events continuously. You should not enter any kind of long-term loop or use time.sleep() on the main thread in Tkinter programs. Your program needs to setup the UI and inputs and then run the Tkinter mainloop and then response to events.

In this case you need to configure your GPIO buttons to raise an event in Tkinter when the button is pressed. GPIO.add_event_detect can be used to set a python function to be called when an event occurs on the pin (eg: a falling edge). In your handler for this you can use the tkinter after function or event generate to raise a Tkinter event to then perform some operation in response to the button press.


import tkinter as tk
from RPi import GPIO

def OnButtonPress(root):

def main():
    root = tk.Tk()
    GPIO.setup(22, GPIO.IN, pull_ip_down=GPIO.PUD_UP)
    GPIO.add_event_detect(22, GPIO.BOTH, callback=lambda: OnButtonPress(root), bouncetime=300)
    root.bind('<<GpioButtonPress>>', lambda e: root.configure(background='red'))

You really must ensure the Tk event loop is processed promptly. If you want to do something after a delay then use after to have the event loop call a function after a delay. Avoid sleep and while True unless these are used on a worker or background thread.

| improve this answer | |

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.