I'm trying to toggle an LED on and off using a GUI.
When I execute the code I get a blank box just saying "tk" for the title.
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.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, columnspan=2)
def convert0(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
self.button.config(text='LED 0 OFF')
else:
self.button.config(text='LED 0 ON')
root = tk.Tk()
app = Application(master=root)
app.mainloop()
Your code needs several fixes.
First off, running it as is gave me the following error:
And for sure, it is not. What is defined is
tk.Label
, so let's change those two lines:into:
Now, I'm raised the following error:
And sure enough, it isn't either. You're probably refering to the fact that the
Application
class extends thetk.Frame
class. Well, that's true, but that doesn't tell us whatframe
is. I assume thatframe
means "the instance, but considered as aFrame
instance". In this case,self
is enough (and it's actually what is needed). So here we go, the following three lines:become:
Now, I'm told that
I'm sure you're starting to understand the point. So let's replace
Button
bytk.Button
:And here you go, the window is displayed, with two labels and one nice button, whose text changes when clicked.