Python Forum
[Tkinter] Tkinter newly created button does not execute command
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Tkinter newly created button does not execute command
#2
You need to provide a quit method:
from tkinter import *
 
class Application(Frame):
    def __init__(self, master=None):
        self.master = master
        self.master.geometry('300x100+10+10')
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

    def new_button(self):
        print("enable_b")
        self.hi_there.config(state=ACTIVE)
        self.new_button.grid_remove()
         
    def say_hi(self):
        print("hi there, everyone!")
        self.new_button = Button(self)
        self.new_button["text"] = "New BTN"
        self.new_button.grid(row=1,column=0)
        self.hi_there.config(state=DISABLED, command=self.new_button)
 
    def createWidgets(self):
        self.QUIT = Button(self)
        self.QUIT.config(text="QUIT",fg="red",command=self.quit)
        self.QUIT.grid(row=0,column=1)
        self.hi_there = Button(self)
        self.hi_there["text"] = "Hello",
        self.hi_there["command"] = self.say_hi
        self.hi_there.grid(row=0,column=0)

    def quit(self):
        self.master.destroy()

def testit(): 
    root = Tk()
    app = Application(master=root)
    app.mainloop()

if __name__ == '__main__':
    testit()

I also moved the __init__ routine to top (will work elsewhere, but other programmers will always look for it at the top)
added startup code 'if __name__ ...'
and created class variable for delete
Reply


Messages In This Thread
RE: Tkinter newly created button does not execute command - by Larz60+ - Jul-20-2018, 09:03 AM
Help me!! - by JUANCARLOS - Jul-25-2018, 01:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 1,538 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 1,169 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 6,196 May-25-2023, 07:37 PM
Last Post: deanhystad
  tkinter.TclError: can't invoke "canvas" command cybertooth 8 6,659 Feb-23-2023, 06:58 PM
Last Post: deanhystad
  Can't get tkinter button to change color based on changes in data dford 4 3,648 Feb-13-2022, 01:57 PM
Last Post: dford
  [Tkinter] Button 'command' Argument Confusion gw1500se 11 6,373 Nov-11-2021, 08:45 PM
Last Post: menator01
  Creating a function interrupt button tkinter AnotherSam 2 5,820 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,354 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter showing image in button rwahdan 3 5,895 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  Continue command in python tkinter? MLGpotato 7 8,992 Mar-27-2021, 04:59 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020