I have two buttons and one function.When a user click button1 or button2 program should call the function and the function will write to the screen whic button clicked.I mean if i click button1 there will write "Button1 clicked" on the screen.All in al i want to learn how can learn that, which button called my function(i dont want more than 1 function).


Which GUI toolkit are you using?

With most GUI's you can pass parameters with the function call, so button 1 would use button_press("1") and button 2 would use button_press("2"). If this does not work with the graphics you are using, use a pass-through function

def button_pressed():
     ##do common stuff here
def button_one():
     print "button one pressed"

def button_two():
     print "button two pressed"

Here is an example using the common Tkinter GUI toolkit ...

# Tkinter, show the button that has been clicked

import Tkinter as tk

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

root = tk.Tk()

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

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

commented: Good knowledge +2

Thank you very much! This was just what I was looking for in order to call a function from a button!