Were the tunes "official" military tunes or more like passed down sea shanties? The movies in question seem to show mostly British and American troops engaging in organized whistling, but did German and Russian troops also whistle?
This really is an amalgamation of several questions, and while I would love a detailed answer on the importance of music in morale, order, and comradeship among soldiers of various countries I wouldn't be able to give an answer that would meet AskHistorians guidelines. I can answer you specifically about whistling in The Bridge on the River Kwai since it's one of my favorite movies.
Quite simply, the reason why whistling is used is that the lyrics to the songs that are being whistled were far too vulgar for the time. These films were both made during the Production Code Association era prior to the first attempt at a classification system (the MPAA started the first film rating system in 1968). The Motion Picture Production Code (or Hayes Code) strictly regulated content in films to be released to the american public. While some notable films during this time had already started to defy the system and be released "without approval", the majority of films still fell in line. The famous song from The Bridge on the River Kwai is actually "Colonel Bogey's March", which was popular before WW2 but became hugely popular among enlisted men after lyrics from the song "Hitler Has Only Got One Ball" were combined with the marching tune, which basically went:
Hitler has only got one ball, Göring has two but very small, Himmler has something sim'lar, But poor old Goebbels has no balls at all.
The names from the verses were sometimes swapped around (it is originally a song about Göring in the first line as he had supposedly received a wound to his groin during the failed coup of 1923) and minor changes to the lyrics depending on country, etc. Singing about testicles would never have passed spec with the strict PCA guidelines so Producer Sam Spiegel insisted it be dropped to the soldiers simply whistling while marching in the movie.
Here's Steve Buscemi singing it in the movie John Rabe (2009) - https://www.youtube.com/watch?v=VPeM0BGGt80
Transcript of the MPPC "Don'ts and Be Carefuls" (warning - this document was developed originally in 1927 and is quite offensive in it's language and inferences by modern standards) - http://www.wabashcenter.wabash.edu/syllabi/w/weisenfeld/rel160/donts.html
A good read on Colonel Bogey's March and how it has been used and repurposed throughout history is available in a dedicated chapter in Warrior Ways: Explorations in Modern Military Folklore from Eliason and Tuleja, 2012 (or through a paywall at https://muse.jhu.edu/book/19553)