Hi;

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).

Thakns.

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"
     button_pressed()

def button_two():
     print "button two pressed"
     button_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.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()
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!

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.