By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,955 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

Reading which button is pressed

P: 5
I am trying to write a python script to help some deaf people. They have a list of sign language songs which are in m4v format.
I have a raspberry pi and a monitor which I can give them.
My intention is to provide a gui with all the song numbers on buttons so they just click the mouse on one and the video plays then returns.
I know that I can launch omxplayer with the file as a parameter.
I am trying to develop the code on my linux laptop and then transfer it to the raspberry pi along with all the songs >>> There are 152 of them <<<
That is where my problem comes from.
I have the following code ( just the relevant snippets):
Expand|Select|Wrap|Line Numbers
  1. def play_song(p):
  2.     i=p; k=int(i)
  3.     if k>0:
  4.         if k<136:
  5.             starter="home/norman//Downloads/bsl/sn_"
  6.         else:
  7.             starter="snnw_"
  8.         if k<156:
  9.             stp=str(k)
  10.             if len(stp)<2:
  11.                 stp="0"+stp
  12.                 if len(stp)<3:
  13.                     stp=" " + stp
  14.     song2play=starter+ stp +"_r480P.m4v"
  15.     print song2play
  16.  
  17. # generate the buttons
  18. for x in range(1,9):
  19.     for y in range(1,20):
  20.         txt=str(q)
  21.         j=int(txt)
  22.         myname="button" + str(x*y)
  23.         myname = Button(frame2,text=txt, font=TF, width=3, command = lambda: play_song(txt))
  24.         myname.grid(row = x+2, column=y+1)
  25.         q+=1
  26.  
I have tried various ways of passing the correct song number to the function but whatever I try I get the same problem or lots of errors with other versions I have tried.
I get the same file name generated:
snnw_152_r480P.m4v
whichever button I click. Can it be done this way or do I have to create the buttons some other way
Sep 18 '16 #1
Share this Question
Share on Google+
3 Replies


P: 5
It's olay, thanks got it sorted. Must have my dumb head on tonight.Just altered a lit bit of the code:
Expand|Select|Wrap|Line Numbers
  1. def play_song(event):
  2.     p = event.widget['text']
  3.     k=int(p)
  4.  
and
Expand|Select|Wrap|Line Numbers
  1.         myname = Button(frame2,text=txt, font=TF, width=3)
  2.         myname.bind('<Button-1>', play_song)
  3.  
So now it does work.
Sep 18 '16 #2

Expert 100+
P: 613
What happens when k > 155? You can also use partial to pass the number to the function
Expand|Select|Wrap|Line Numbers
  1. from functools import partial
  2.  
  3.         myname = Button(frame2,text=txt, font=TF, width=3, command = partial(play_song, txt))  
Note that bind sends an event to the function, while "command" does not.
Sep 18 '16 #3

P: 5
Thanks dwblas, didn't know about partial.
I don't think k can be higher than 155 in this situation unless I am missing something?
Sep 19 '16 #4

Post your reply

Sign in to post your reply or Sign up for a free account.