Reputation: 125
Can i ask for a little help please? I've created a GUI with a toggle button which toggles the button text LED ON, LED OFF. I've now added a label with an image and I would like to try to make the label image toggle when the button is pressed.
I've looked up some examples but I can't quite see how, or where, to add the code to make the label image toggle as well. I tried to add a piece of code into the first part of my IF statement, using label.config but I was trying things I had read on the forums but in the end it didn't work. So I've come to ask for advice.
Thanks for any help.
My code....
# Idle 07_02_LED ON using GUI
from time import sleep
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
Label(frame, text='Turn LED ON').grid(row=0, column=0)
Label(frame, text='Turn LED OFF').grid(row=1, column=0)
self.button = Button(frame, text='LED 0 ON', command=self.convert0)
self.button.grid(row=2, column=0)
LED = Label(frame, image=logo).grid(row=2, column=1)
def convert0(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
print('LED 0 OFF')
self.button.config(text='LED 0 OFF')
LED = Label.config(image = logo2)
else:
print('LED 0 ON')
self.button.config(text='LED 0 ON')
root = Tk()
logo = PhotoImage(file="C:\My Documents\MyPictures\Green LED.gif")
logo2 = PhotoImage(file="C:\My Documents\My Pictures\Red LED.gif")
root.wm_title('LED on & off program')
app = App(root)
root.mainloop()
Upvotes: 0
Views: 1218
Reputation: 2801
Replace LED
with self.LED
.
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
Label(frame, text='Turn LED ON').grid(row=0, column=0)
Label(frame, text='Turn LED OFF').grid(row=1, column=0)
self.button = Button(frame, text='LED 0 ON', command=self.convert0)
self.button.grid(row=2, column=0)
self.LED = Label(frame, image=logo)
self.LED.grid(row=2, column=1)
def convert0(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
print('LED 0 OFF')
self.button.config(text='LED 0 OFF')
self.LED.config(image = logo2)
else:
print('LED 0 ON')
self.button.config(text='LED 0 ON')
self.LED.config(image = logo)
Upvotes: 1