Mar-04-2020, 12:36 PM
Apologies.
While I stand by my statement that globals should be avoided and the comments of others that there is usually (?always) a better way, I do not own a Raspberry Pi and therefore cannot test the code.
Therefore must work from observations of the code. Your functions Increase_Time and Decrease_time both access the value of delay. I do get accused of overusing classes, but here is an idea that avoids globals by creating a class, instantiating that class, and the class holds delay and the two functions, isolating delay from the global arena.
No raspberry pi yet, so no testing, but see if this works for you.
While I stand by my statement that globals should be avoided and the comments of others that there is usually (?always) a better way, I do not own a Raspberry Pi and therefore cannot test the code.
Therefore must work from observations of the code. Your functions Increase_Time and Decrease_time both access the value of delay. I do get accused of overusing classes, but here is an idea that avoids globals by creating a class, instantiating that class, and the class holds delay and the two functions, isolating delay from the global arena.
No raspberry pi yet, so no testing, but see if this works for you.
from tkinter import * from tkinter import ttk import RPi.GPIO as GPIO import time root = Tk() class blinker : self.delay def __init__(self, dtime): self.delay = dtime def Decrease_Time(): delay -= .05 def Increase_Time(): delay += .05 bc = blinker(0.4) redLED = 26 GPIO.setmode(GPIO.BCM) GPIO.setup(redLED, GPIO.OUT) while True: GPIO.output(redLED, GPIO.HIGH) time.sleep(bc.delay) GPIO.output(redLED, GPIO.LOW) time.sleep(bc.delay) button_increse = ttk.Button(root, text = 'Increase', command = bc.Increase_Time).pack() button_decrease = ttk.Button(root, text = 'Decrease', command = bc.Decrease_Time).pack() root.mainloop()