I'm trying to make my button change on and off whenever the user clicks on it. But I keep getting a name error. I've tried changing the order of the function and button creation. If I place the button creation above the function then it won't be able to call the function.
Here is my Code:
#import all of tkinter and also updated widgets
from tkinter import *
from tkinter import ttk
import main
#sets up main application window
root = Tk()
root.title("Winstreak Counter GUI")
#creating a content frame (frame widget, hold content of user interface)
#we do this because it'll allow us to control the background color and allow for more themed widgets
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=10, row=10, sticky=(N, W, E, S))
root.columnconfigure(10, weight=1)
root.rowconfigure(10, weight=1)
f = open('/Users/ripkoye/Desktop/WinstreakProject/winstreak.txt', 'r')
#store the information into a variable
winstreak = StringVar(mainframe)
winstreak.set(f.read())
#creates a winstreaklabel
winstreaklabel = ttk.Label(mainframe,width=20, padding='5 5 5 5')
winstreaklabel.grid()
winstreaklabel['textvariable'] = winstreak
#assigns the text to the variable named winstreak
f.close()
is_on = True
def switch():
global is_on
if is_on:
buttonOnOff.config(image = off)
is_on = False
else:
buttonOnOff.config(image = on)
is_on = True
on = PhotoImage(file='on.png')
off = PhotoImage(file='off.png')
buttonOnOff = ttk.Button(mainframe, image=on, width=0.5, padding='5 5 5 5', command=switch())
buttonOnOff.grid()
root.mainloop()
This is the error:
Traceback (most recent call last):
File "/Users/ripkoye/Desktop/WinstreakProject/gui.py", line 42, in <module>
buttonOnOff = ttk.Button(mainframe, image=on, width=0.5, padding='5 5 5 5', command=switch())
File "/Users/ripkoye/Desktop/WinstreakProject/gui.py", line 34, in switch
buttonOnOff.config(image = off)
NameError: name 'buttonOnOff' is not defined