self.button1 = Button(root, text = '1', \
command = self.buttonAction)
self.button1.grid(row = 1, column = 0)

self.button2 = Button(root, text = '2', \
command = self.buttonAction)

def buttonAction(self):
if self.button1 is clicked/pressed... :
value = 1

how do i express the word in red in python code?!!??
thanks :D

Recommended Answers

All 5 Replies

tryied to google it..but cant find anything helpful~~~..
hope sum1 would help me ..thanks so much~~~!!!

Edit: I'm sorry. I didn't realize that you wanted the button to set up a condition so that if another button is pressed something would be executed, or do you just want to check which button called the function?

yeah..thanks both of you
and vega's link too...its helpful :D

vegaseat wrote some good code. I did some experimentation with bindings. If you decide to use bindings, there is a very easy way to determine the widget that called the event. The event object has an attribute called "widget". This attribute contains the numerical name of the widget which called the event. I haven't experimented with using a widget's numerical name to perform actions on and with that widget, but I'm sure the functionality exists somewhere.

I suppose it would help if I taught you how I found out about the event's "widget" attribute. Every object in Python has a dictionary of attributes and methods. You can see the contents of this dictionary with:

print(dir(object))

Now any attributes and methods intended to be available to access from the outside will generally be in an object's "__dict__" attribute. If you wanted to find out the attributes and methods of an event for instance, you could use this code:

from tkinter import *

def myevent(event):
    for key in event.__dict__:
        print(str(key), ":", str(event.__dict__[key]))

root = Tk()

mybutton = Button(text = "My Button")
mybutton.bind("<Button-1>", myevent)
mybutton.pack()

root.mainloop()

Run this script, and then look in the Python shell to see the results. ("print" prints to stdout, not to the GUI.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.