2
Contributors
2
Replies
18
Views
3 Years
Discussion Span
Last Post by vegaseat
2

You could bind the event to pass certain arguments ...

# Tkinter, show the text of the button that has been clicked

# for Python3 use tkinter
import Tkinter as tk

def click(event):
    s = "clicked: " + event.widget.cget("text")
    root.title(s)

root = tk.Tk()
root.geometry("300x50+30+30")

b1 = tk.Button(root, text="button1")
b1.bind("<Button-1>", click)
b1.pack()

b2 = tk.Button(root, text="button2")
b2.bind("<Button-1>", click)
b2.pack()

root.mainloop()

Edited by vegaseat

1

You can also use lambda or a partial function to pass arguments ...

''' tk_button_text102.py
show the text of the Tkinter button that has been clicked
'''

# for Python3 use tkinter
import Tkinter as tk

def click(val):
    sf = "clicked: {}".format(val)
    root.title(sf)

root = tk.Tk()
root.geometry("300x80+80+50")

text1 = "button1"
cmd1 = lambda: click(text1)
b1 = tk.Button(root, text=text1, command=cmd1)
b1.pack(pady=10)

text2 = "button2"
cmd2 = lambda: click(text2)
b2 = tk.Button(root, text=text2, command=cmd2)
b2.pack()

root.mainloop()
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.