So I'm writing a code that imports pygame to play files of music notes. I set each note to a button, 24 notes for 24 buttons. However, the program will also have four more buttons: "Record", "Stop", "Playback", and "Quit". when the record button is clicked, it will append the file name to a songlist. Then the stop button will be pressed to terminate the record function. the songlist will then be played using the playback by having an index = 0 and in the function it will have:

while index < len(songList):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL            PROJECT/notes/"+songList[index])
        mixer.music.play(0,0.0)
        index+=1

The function will go through each element of the list and play each note.


So my problem here, is creating the record function. I need to have it so that it knows when a note button is clicked so that it can append a name to the songlist. So can I write a code that tells whether or not a button is clicked or not.

for example in pseuodocode...

if "C# Button is clicked":
    note = 'Csharp.mp3'
    songList.append(note)
if "D Button is clicked":
    note = 'D.mp3'
    songList.append(note)

how would I write in actual coding ( if "D Button is clicked")

when each note button is pressed, it runs this function:

def playC(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[0])
        mixer.music.play(0,0)
        note = '2C.mp3'

Each note button will have this format but with the note and the filename different of course. a friend of mine gave me the idea to set maybe a dummy statement such as
self.C = 'Go' after the mixer.music.play function. So that I would write in the Record function....
if self.C = 'Go':
songList.append(note)

Any ideas? this is the actual code that I have so far... it's not quite finished yet and I've been commenting stuff out as well.

from tkinter import *
from pygame import *

NOTELIST = ['2C.mp3', '2C#.mp3', '2D.mp3', '2D#.mp3', '2E.mp3', '2F.mp3', '2F#.mp3', '2G.mp3', '2G#.mp3', '2A.mp3', '2A#.mp3', '2B.mp3', \
            '3C.mp3', '3C#.mp3', '3D.mp3', '3D#.mp3', '3E.mp3', '3F.mp3', '3F#.mp3', '3G.mp3', '3G#.mp3', '3A.mp3', '3A#.mp3', '3B.mp3']

KEY = ['z', 's', 'x', 'd', 'c', 'v', 'g', 'b', 'h', 'n', 'j', 'm',\
       'q', '2', 'w', '3', 'e', 'r', '5', 't', '6', 'y', '7', 'u']


class Editor:
    def __init__(self):
        #create the main window
        self.main_window = Tk()
        self.main_window.title("Music")

        #create frames
        self.top_frame = Frame(self.main_window)
        self.middle_frame = Frame(self.main_window)
        self.octave1 = Frame(self.main_window)
        self.octave2 = Frame(self.main_window)
        self.bottom_frame = Frame(self.main_window)

        

        #Create widgest

        #create label for top frame
        self.Label = Label(self.top_frame, text = "Hit Record and record a tune")

        #pack label
        self.Label.pack(side = "top")

        

        #create buttons for middle frame

##        self.Record = Button(self.middle_frame, text = "Record", command = self.Record)
##        self.Stop = Button(self.middle_frame, text = "Stop", command = self.Stop)
##        self.Play = Button(self.middle_frame, text = "Play", command = self.Playback)
##        self.Quit = Button(self.middle_frame, text = "Quit", command = self.main_window.destroy)

        #Pack buttons
##        self.Record.pack(side = "left")
##        self.Stop.pack(side = "left")
##        self.Play.pack(side = "left")
##        self.Quit.pack(side = "left")

        #create buttons for keyboard octave 1
        self.C = Button(self.octave1, text = "C", command = self.playC)
        self.Csharp = Button(self.octave1, text = "C#", command = self.playCsharp)
        self.D = Button(self.octave1, text = "D", command = self.playD)
        self.Dsharp = Button(self.octave1, text = "D#", command = self.playDsharp)
        self.E = Button(self.octave1, text = "E", command = self.playE)
        self.F = Button(self.octave1, text = "F", command = self.playF)
        self.Fsharp = Button(self.octave1, text = "F#", command = self.playFsharp)
        self.G = Button(self.octave1, text = "G", command = self.playG)
        self.Gsharp = Button(self.octave1, text = "G#", command = self.playGsharp)
        self.A = Button(self.octave1, text = "A", command = self.playA)
        self.Asharp = Button(self.octave1, text = "A#", command = self.playAsharp)
        self.B = Button(self.octave1, text = "B", command = self.playB)

        self.C.pack(side='left')
        self.Csharp.pack(side='left')
        self.D.pack(side='left')
        self.Dsharp.pack(side='left')
        self.E.pack(side='left')
        self.F.pack(side='left')
        self.Fsharp.pack(side='left')
        self.G.pack(side='left')
        self.Gsharp.pack(side='left')
        self.A.pack(side='left')
        self.Asharp.pack(side='left')
        self.B.pack(side='left')
        

        #create buttons for keyboard octave 2
        self.octC = Button(self.octave2, text = "C", command = self.play2C)
        self.octCsharp = Button(self.octave2, text = "C#", command = self.play2Csharp)
        self.octD = Button(self.octave2, text = "D", command = self.play2D)
        self.octDsharp = Button(self.octave2, text = "D#", command = self.play2Dsharp)
        self.octE = Button(self.octave2, text = "E", command = self.play2E)
        self.octF = Button(self.octave2, text = "F", command = self.play2F)
        self.octFsharp = Button(self.octave2, text = "F#", command = self.play2Fsharp)
        self.octG = Button(self.octave2, text = "G", command = self.play2G)
        self.octGsharp = Button(self.octave2, text = "G#", command = self.play2Gsharp)
        self.octA = Button(self.octave2, text = "A", command = self.play2A)
        self.octAsharp = Button(self.octave2, text = "A#", command = self.play2Asharp)
        self.octB = Button(self.octave2, text = "B", command = self.play2B)

        self.octC.pack(side='left')
        self.octCsharp.pack(side='left')
        self.octD.pack(side='left')
        self.octDsharp.pack(side='left')
        self.octE.pack(side='left')
        self.octF.pack(side='left')
        self.octFsharp.pack(side='left')
        self.octG.pack(side='left')
        self.octGsharp.pack(side='left')
        self.octA.pack(side='left')
        self.octAsharp.pack(side='left')
        self.octB.pack(side='left')
        
        

        #create legend in bottom frame
##        self.canvas = Canvas(width = 1000, height = 500)
##        image = PhotoImage(file = "keyboard.gif")
##        self.canvas.create_image(500,250, image = image)
##        self.canvas.pack()
        

        #pack Frames
        self.top_frame.pack()
        self.middle_frame.pack()
        self.bottom_frame.pack()

        self.octave1.pack()
        self.octave2.pack()
        


        mainloop()
##
##
##        
    def Record(self):

    def Stop(self):

    def Playback(self):

    def playC(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[0])
        mixer.music.play(0,0)
        note = '2C.mp3'
    def playCsharp(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[1])
        mixer.music.play(0,0)
        note = '2C#.mp3'
    def playD(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[2])
        mixer.music.play(0,0)
        note = '2D.mp3'
    def playDsharp(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[3])
        mixer.music.play(0,0)
        note = '2D#.mp3'
    def playE(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[4])
        mixer.music.play(0,0)
        note = '2E.mp3'
    def playF(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[5])
        mixer.music.play(0,0)
        note = '2F.mp3'
    def playFsharp(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[6])
        mixer.music.play(0,0)
        note = '2G.mp3'
    def playG(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[7])
        mixer.music.play(0,0)
        note = '2G.mp3'
    def playGsharp(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[8])
        mixer.music.play(0,0)
        note = '2G#.mp3'
    def playA(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[9])
        mixer.music.play(0,0)
        note = '2A.mp3'
    def playAsharp(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[10])
        mixer.music.play(0,0)
        note = '2A#.mp3'
    def playB(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[11])
        mixer.music.play(0,0)
        note = '2B.mp3'
        
        
    def play2C(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[12])
        mixer.music.play(0,0)
    def play2Csharp(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[13])
        mixer.music.play(0,0)
    def play2D(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[14])
        mixer.music.play(0,0)
    def play2Dsharp(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[15])
        mixer.music.play(0,0)
    def play2E(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[16])
        mixer.music.play(0,0)
    def play2F(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[17])
        mixer.music.play(0,0)
    def play2Fsharp(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[18])
        mixer.music.play(0,0)
    def play2G(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[19])
        mixer.music.play(0,0)
    def play2Gsharp(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[20])
        mixer.music.play(0,0)
    def play2A(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[21])
        mixer.music.play(0,0)
    def play2Asharp(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[22])
        mixer.music.play(0,0)
    def play2B(self):
        mixer.init()
        mixer.music.load("C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[23])
        mixer.music.play(0,0)

        
        


mygui = Editor()

Do not repeat code but use loop and argument with value from notelist by functools.partial.

This code wont run as you have place holder functions without any statements, add pass statement. Record/Stop should be toggle switch changing the self.recording between True and False. Add self.recording check into play function. You should probably use key up even to record the length of note. Also you should assoociate keyboard key so you possibly can push chords, even keyboard keys are not so reliable with multiple keys pressed (in same loop you associate the graphical button)

Here textual way worked on code (as I have not mp3 files).

import functools

from Tkinter import *
from pygame import *

NOTEPAYH = "G:/notes/"
NOTELIST = ['2C.mp3', '2C#.mp3', '2D.mp3', '2D#.mp3', '2E.mp3', '2F.mp3', '2F#.mp3', '2G.mp3', '2G#.mp3', '2A.mp3', '2A#.mp3', '2B.mp3',
            '3C.mp3', '3C#.mp3', '3D.mp3', '3D#.mp3', '3E.mp3', '3F.mp3', '3F#.mp3', '3G.mp3', '3G#.mp3', '3A.mp3', '3A#.mp3', '3B.mp3']

KEY = ['z', 's', 'x', 'd', 'c', 'v', 'g', 'b', 'h', 'n', 'j', 'm',
       'q', '2', 'w', '3', 'e', 'r', '5', 't', '6', 'y', '7', 'u']

from time import clock

class Editor:
    def __init__(self):

        self.notes = []
        self.keyboard = []
        self.recording = False
        
        #create the main window
        self.main_window = Tk()
        self.main_window.title("Music")

        #create frames
        self.middle_frame = Frame(self.main_window)
        self.octave1 = Frame(self.main_window)
        self.octave2 = Frame(self.main_window)
        self.bottom_frame = Frame(self.main_window)       

        #Create widgest
        
        #create buttons for middle frame

        self.record = Button(self.middle_frame, text = "Start Recording", command = self.record_toggle)
        self.play = Button(self.middle_frame, text = "Play", command = self.playback)
        self.quit = Button(self.middle_frame, text = "Quit", command = self.main_window.destroy)
        for widget in self.record, self.play, self.quit:
            widget.pack(side=LEFT, expand=TRUE, fill=BOTH)

        #create buttons for keyboard octaves
        octave = self.octave1
        self.key_note = {}
        for note, key in zip(NOTELIST, KEY):
            if note.startswith('3C'):
                octave = self.octave2
            self.keyboard.append(Button(octave, text = note.split('.')[0], command= functools.partial(self.play_note, note=note)))
            self.keyboard[-1].pack(side='left')
            self.main_window.bind_all(key, functools.partial(self.play_note, key=key))
            self.key_note[key] = note
            #octave.bind('ButtonRelease-1', self.stop_note) # does not function            
        
        #pack Frames
        self.middle_frame.pack()
        self.bottom_frame.pack()

        self.octave1.pack()
        self.octave2.pack()
        #self.main_window.get_focus()
        
        mainloop()

    def record_toggle(self):
        self.recording = not self.recording
        self.record['text'] =  ('Stop' if self.recording else 'Start') + ' Recording' 
        self.main_window.title('  '.join('Recording') if self.recording else 'Music')
        

    def playback(self):
        self.main_window.title('   '.join('Playback'))
        #to be done

    # stop_note should note the release time of button, to be done
    def stop_note(self, event):
        print event
        self.notes[-1].extend(clock())
        print(self.notes[-1])
            
    def play_note(self, note=0, key=''):
        if key:
            note = self.key_note[key]
        if self.recording:
            self.notes.append([note, clock()])
            print self.notes[-1]
        else:
            print note, clock()
##        mixer.init()
##        mixer.music.load(NOTEPATH + NOTELIST[note])
##        mixer.music.play(0,0)
    
        
mygui = Editor()

I need to have it so that it knows when a note button is clicked

The standard way is to set some variable. In Tony's code, a button click calls play_note() and sends it the note and key. You could use the key to tell which button is clicked and possibly link it to a dictionary that contains the function you want to execute.

I am saving clock time with note information and checking it whe key goes up. My newer version has multiple keypresses OK by keeping set of currently playing keys/mouse presses. Note and key are of course redundant, but now I finally needed the key_note dictionary also. The redundancy can be cleaned at end but it is usefull until code is finalized. Even nowadays it is not so big sin to waste memory for dictionary of all of 24 values... Sometimes my 64 kByte memory days...

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.