0

I've created a simple program to change the LED blink rate (using a Raspberry Pi 2B running Raspbian) based on a GUI button input.

The code works, but the problem I'm having is that there is a noticeable lag between when I click the button and updating the counter (label4) as well as executing the code itself. I think the problem is that I'm updating the GUI much more than I need to, but I don't know how to fix this.

# This program will create an interactive GUI which can change an LED's blink rate

# Import necessary modules
from tkinter import *
from time import sleep
from gpiozero import LED

# Set up GUI Window
mygui = Tk()
myfont = ('Times', 24, 'bold')
myfont2 = ('Times', 36, 'bold')
mygui.title('LED Controller')
mygui.geometry('640x480+120+120')
counter = DoubleVar()
counter.set(1.0)
red_LED = LED(17)
loop = True

# Define usable functions
def increase_speed():
    counter.set(counter.get() + 0.25)
    label4['text']=str(counter.get())+'     '
    mygui.update_idletasks()
    mygui.update()
    print(counter.get())


def decrease_speed():
    counter.set(counter.get() - 0.25)
    label4['text']=str(counter.get())+'     '
    mygui.update_idletasks()
    mygui.update()
    print(counter.get())


def end_program():
    mygui.destroy()
    loop = False


while loop == True:

    # Create GUI labels
    label1 = Label(mygui, text='Increase Blink Speed', padx=15, pady=15, font=myfont)
    label1.grid(row=1, column=1, sticky=W)
    label2 = Label(mygui, text='Decrease Blink Speed', padx=15, pady=15, font=myfont)
    label2.grid(row=2, column=1, sticky=W)
    label3 = Label(mygui, text='Blink Delay =', padx=15, pady=15, font=myfont)
    label3.grid(row=3, column=1, sticky=W)
    label4 = Label(mygui, text=counter.get(), padx=0, pady=15, font=myfont)
    label4.grid(row=3, column=2, sticky=W)

    # Create GUI buttons
    button1 = Button(mygui, text='+', font=myfont2, command=increase_speed, fg='green', width=5)
    button1.grid(row=1, column=2, padx=10, pady=10, sticky=W)
    button2 = Button(mygui, text='-', font=myfont2, command=decrease_speed, fg='red', width=5)
    button2.grid(row=2, column=2, padx=10, pady=10, sticky=W)
    button3 = Button(mygui, text='Close Window', font=myfont, command=end_program, fg='black', bg='yellow', width=15)
    button3.grid(row=4, column=1, padx=10, pady=10, sticky=W)

    # Blink LED
    red_LED.on()
    sleep(counter.get())
    red_LED.off()
    sleep(counter.get())

    # Update GUI Window
    mygui.update_idletasks()
    mygui.update()

put on hold as off-topic by Steve Robillard, Milliways, Dmitry Grigoryev, Darth Vader Jan 12 at 22:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center." – Steve Robillard, Milliways, Dmitry Grigoryev, Darth Vader
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    This isn't on topic, but this is the same issue as your last question - you have startup code in your loop. – Milliways Jan 4 at 23:30
  • Hi Milliways, thanks for responding again- so you're saying I should take out the label and button creation from the loop if I understand you correctly? That makes sense and is good practice. Thank you – Clueless_User Jan 6 at 2:09