Python Forum
trying to change variable value with a def
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying to change variable value with a def
#1
Hi, i am a newbie in python and am trying to make a gui to control how fast an LED blinks with a variable named "delay". i am using this code

from tkinter import *
from tkinter import ttk
import RPi.GPIO as GPIO
import time

root = Tk()

redLED = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(redLED, GPIO.OUT)


global delay
delay = .40
while True:
   GPIO.output(redLED, GPIO.HIGH)
   time.sleep(delay)
   GPIO.output(redLED, GPIO.LOW)
   time.sleep(delay)
   def Decrease_Time():
      delay -= .05

   def Increase_Time():
      delay += .05

   button_increse = ttk.Button(root, text = 'Increase', command = Increase_Time).pack()
   button_decrease = ttk.Button(root, text = 'Decrease', command = Decrease_Time).pack()

   root.mainloop()
but i get this error when i press the Increase or decrease buttons

Error:
UnboundLocalError: local variable 'delay' referenced before assignment
any help appreciated
Reply


Messages In This Thread
trying to change variable value with a def - by pythonbegginer - Mar-02-2020, 03:31 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 467 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  how can a variable change if I haven't changed it? niminim 5 3,242 Apr-07-2021, 06:57 PM
Last Post: niminim
  Change variable value during a while loop? penahuse 2 4,227 Nov-15-2020, 11:53 PM
Last Post: penahuse
  Change variable in an outside file ebolisa 5 2,809 Nov-11-2020, 04:41 AM
Last Post: ebolisa
  Change name of variable samuelbachorik 2 2,179 Aug-10-2020, 02:34 PM
Last Post: deanhystad
  Python - change variable type during program execution ple 1 2,486 Apr-12-2020, 08:43 AM
Last Post: buran
  change value of a global variable across all modules aster 5 5,291 Jan-01-2019, 06:42 PM
Last Post: aster
  How to change global variable in function? dan789 5 4,050 Nov-10-2018, 08:55 PM
Last Post: dan789
  Unwanted variable change in module dannyH 2 2,780 May-08-2018, 05:33 PM
Last Post: dannyH

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020