0

I am making a simple program for my brother where when he pushes the 4th button on his mouse it plays a jingle he wants. I have PyAudio and used a simple function to get the jingle to play. I just need to know how to set a function to start when a button is pushed.
My first idea was to have a When loop and when the button is pushed it changes the var to fit and at the end of the loop but I don't know how to change a var on a button press either.

Thanks for the help in advance.

Jack Brand
  • 13
  • 2
  • 2
  • 7
  • what gui manager are you using? And you mean a "while" loop not a "when" loop, right? – en_Knight Nov 13 '15 at 03:08
  • I'm just running it with CMD and making it in Notepad++. And yes, I mean while, Sorry about that. – Jack Brand Nov 13 '15 at 03:11
  • @en_Knight from the context, I believe that Jack means “when”, but not “loop”. As a teacher I see pupils say loop to mean block. So read “When block” (or event handler). – ctrl-alt-delor Nov 17 '16 at 18:07

2 Answers2

0

It's easy to make simply GUI's with python. Pygame is popular if you love external decencies. Tkinter is built into python and very easy to use

Effbot is an indispensible resource for it; here's their page on buttons.

http://effbot.org/tkinterbook/button.htm

Someone on SO (actually, quite a few people) have wondered about this too

How to pass arguments to a Button command in Tkinter?

The gist is (python 2)

# import the GUI manager package
import Tkinter as tk
# make a window pop up
my_root_window = tk.Tk()
# add a button to it
button = tk.Button( my_root_window, text='Press Me!', command=foo)
# make the button appear
button.pack()
# keep the gui alive
my_root_window.mainloop()

That is as minimal as I can make an example. The only subteldly, as noted in the SO link, is that you pass the function object, i.e., do not call the function when you assign it.

Note that when searching for your problem, the query "python button command" pops up the pages I linked and over a million others; the red herring was that the audio stuff you're talking bout has nothing to do with the generic problem of calling a function from a button

I should also note that the while loop is not the right approach, as it will cause almost any GUI to freeze. The function "mainloop" called at the end there keeps the window open and also allows events to occur in the window, like button presses. Just make the function called when the button is pressed set of the correct chain reaction. If you need more control, look into more widgets!

Binding to a mouse button is easy too

my_root_window.bind('<Button-1>',lambda event : foobar)

will call foobar whenever the left button on the mouse is pressed. This enter link description here page gives some detail on what events are possible, in particular mouse events

Community
  • 1
  • 1
en_Knight
  • 5,301
  • 2
  • 26
  • 46
0
import Tkinter
import tkMessageBox

top = Tkinter.Tk()

def helloCallBack():
   tkMessageBox.showinfo( "Hello Python", "Hello World")

B = Tkinter.Button(top, text ="Hello", command = helloCallBack)

B.pack()
top.mainloop()
Gautham M
  • 4,816
  • 3
  • 15
  • 37
  • This dosent answers the question of the OP. – Thingamabobs Dec 25 '20 at 10:43
  • Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. [From Review](/review/low-quality-posts/27929497) – double-beep Dec 25 '20 at 13:06