Python Forum
Get name of command button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get name of command button
#4
You should fix the syntax error. The error is not this line "m = Main(root)".

Here are a couple of methods that receive a widget as an argument and perform some action on the widget.
import tkinter as tk
 
class MyWindow:
    def __init__(self, root):
        frame = tk.Frame(root)
        frame.pack(expand=True, fill=tk.BOTH)
        
        label = tk.Label(frame, text="Press the Button")
        label.pack(expand=True, fill=tk.BOTH, padx=5, pady=5)
 
        button = tk.Button(frame, text="This is the Button")
        button.config(command=lambda:self.button_pressed(button, label))
        button.pack(expand=True, fill=tk.BOTH, padx=5, pady=5)

        quitbn = tk.Button(frame, text="Quit", command=lambda:self.quit(root))
        quitbn.pack(expand=True, fill=tk.BOTH, padx=5, pady=5)
 
    def button_pressed(self, button, label):
        label['text'] = 'The button was pressed'
        button['state'] = tk.DISABLED
 
    def quit(self, window):
        print('Quitting')
        window.destroy()
 
root = tk.Tk()
root.geometry('200x200')
MyWindow(root)
root.mainloop()
Reply


Messages In This Thread
Get name of command button - by Heyjoe - Dec-07-2020, 08:45 PM
RE: Get name of command button - by deanhystad - Dec-07-2020, 09:16 PM
RE: Get name of command button - by Heyjoe - Dec-09-2020, 10:53 PM
RE: Get name of command button - by deanhystad - Dec-10-2020, 04:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Button 'command' Argument Confusion gw1500se 11 6,243 Nov-11-2021, 08:45 PM
Last Post: menator01
  Class function does not create command button Heyjoe 2 2,376 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  [Tkinter] Command button, then more command buttons Heyjoe 4 3,027 Aug-08-2020, 11:28 AM
Last Post: Yoriz
  [Tkinter] button command tkinter Heyjoe 6 5,595 Jul-30-2020, 07:06 PM
Last Post: deanhystad
  Button Command Heyjoe 4 2,522 Jul-20-2020, 01:45 AM
Last Post: Heyjoe
  tkinter button not accessing the command when clicked jhf2 1 3,732 Nov-23-2019, 10:17 PM
Last Post: DT2000
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,145 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] command button automaticaly fired kferhat 2 2,604 Jun-16-2019, 08:21 PM
Last Post: kferhat
  [Tkinter] Button command getting TypeError: radsa() missing 3 required positional arguments nonzzo 2 3,459 Apr-22-2019, 12:36 PM
Last Post: Larz60+
  [Tkinter] how to assign a command to a button ChickenWithRice 2 4,553 Dec-08-2018, 09:35 PM
Last Post: ChickenWithRice

Forum Jump:

User Panel Messages

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