Beginner - GUI toggle button Beginner - GUI toggle button tkinter tkinter

Beginner - GUI toggle button


You need to do two things:

  1. Define the button as self.button so that it becomes an instance attribute of App. That way, you can access it inside convert0 through self.

  2. Use Tkinter.Button.config to update the button's text.

Below is a fixed version of the script. I put the stuff I changed in comment boxes:

# Idle 07_02_LED ON using GUIfrom time import sleepfrom 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, 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()root.wm_title('LED on & off program')app = App(root)root.mainloop()