0

I want to toggle a button from GUI tkinter and display a counts from GPIO.

Here I have a counter system code:

from gpiozero import *
from time import sleep

#####GPIO input##########

led = LED(26)
sensor = LineSensor(24)

#####Global variable#####
counts = 0

def number():
    global counts
    counts += 1
    print(counts)

while True:
    if sensor.is_active:
        sleep(1.5)
        number()
        print("detected" + str(counts))
        led.blink(on_time=.05, off_time=.05, n=2)

pause()

Then, from this code I want to display it on a tkinter after toggle a button. I've made some try:

from tkinter import *
from gpiozero import *
from time import sleep


root = Tk()
root.title('Counter')
root.geometry('250x250')

led = LED(26)
sensor = LineSensor(24)

counts = 0

def number():
    global counts
    counts += 1

while True:
    if sensor.is_active:
        sleep(1.5)
        number()
        led.blink(on_time=.05, off_time=.05, n=2)

StartCount = Button(root, text="Start count", font=('arial', 12), fg='black', command=number)
StartCount.pack()
CountsValue = Label(root, font=('arial', 16), text=counts, fg='black')
CountsValue.pack()

root.mainloop()

Then nothing happen. Can anyone help me with this, i'm still studying this.

  • You might find this easier using guizero (a simple wrapper around Tk): projects.raspberrypi.org/en/projects/… – ben_nuttall Apr 11 at 11:06
  • that would be good, but i wanted to create a tkinter ones, do u have any suggestion for that? – Dzulkarnain Shahidan Apr 11 at 11:26
  • Where is the problem with Raspberry Pi? Does it work on other platforms? – Ingo Apr 11 at 11:32
  • The problem is, nothing showing when i start running it. doesn't work also. I wanted to toggle a button from tkinter and then my counting will start and shows the value into tkinter also – Dzulkarnain Shahidan Apr 11 at 11:44

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.