Toggle LED Python 3.x tkinter -


i'm trying toggle led on , off using gui.

when execute code blank box saying "tk" title.

import tkinter 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 gave me following error:

nameerror: name 'label' not defined 

and sure, not. defined tk.label, let's change 2 lines:

label(frame, text='turn led on').grid(row=0, column=0) label(frame, text='turn led off').grid(row=1, column=0) 

into:

tk.label(frame, text='turn led on').grid(row=0, column=0) tk.label(frame, text='turn led off').grid(row=1, column=0) 

now, i'm raised following error:

nameerror: name 'frame' not defined 

and sure enough, isn't either. you're refering fact application class extends tk.frame class. well, that's true, doesn't tell frame is. assume frame means "the instance, considered frame instance". in case, self enough (and it's needed). here go, following 3 lines:

tk.label(frame, text='turn led on').grid(row=0, column=0) tk.label(frame, text='turn led off').grid(row=1, column=0)  self.button = button(frame, text='led 0 on', command=self.convert0)     

become:

tk.label(self, text='turn led on').grid(row=0, column=0) tk.label(self, text='turn led off').grid(row=1, column=0)  self.button = button(self, text='led 0 on', command=self.convert0)  

now, i'm told that

nameerror: name 'button' not defined 

i'm sure you're starting understand point. let's replace button tk.button:

self.button = tk.button(self, text='led 0 on', command=self.convert0) 

and here go, window displayed, 2 labels , 1 nice button, text changes when clicked.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -