Reading which button is pressed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • barrowman
    New Member
    • Feb 2016
    • 5

    Reading which button is pressed

    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):
    Code:
    def play_song(p):
        i=p; k=int(i)
        if k>0:
            if k<136:
                starter="home/norman//Downloads/bsl/sn_"
            else:
                starter="snnw_"
            if k<156:
                stp=str(k)
                if len(stp)<2:
                    stp="0"+stp
                    if len(stp)<3:
                        stp=" " + stp
        song2play=starter+ stp +"_r480P.m4v"
        print song2play
            
    # generate the buttons
    for x in range(1,9):
        for y in range(1,20):
            txt=str(q)
            j=int(txt)
            myname="button" + str(x*y)
            myname = Button(frame2,text=txt, font=TF, width=3, command = lambda: play_song(txt))
            myname.grid(row = x+2, column=y+1)
            q+=1
    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
  • barrowman
    New Member
    • Feb 2016
    • 5

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

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      What happens when k > 155? You can also use partial to pass the number to the function
      Code:
      from functools import partial
      
              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.

      Comment

      • barrowman
        New Member
        • Feb 2016
        • 5

        #4
        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?

        Comment

        Working...