What is the reason why King Claudius walked out while watching the play?
I like this question. Of course, Claudius knows that he has been discovered. Someone else in the court is not only aware that he murdered his brother King Hamlet but also how it was done--poison in the ear. When Claudius sees the play that reenacts the murder, he stands up, calling for more light, more light, and walks out. As Hamlet had predicted, he is clearly disturbed. So, two characters learn something from the play-within-a-play: Hamlet learns that Claudius is indeed guilty, and Claudius knows that someone (Hamlet)knows. Obviously, he is disturbed that he has been found out. But, he may also be reacting to an implied threat in the play. Notice that Hamlet constructed the play scene so that the Player King is murdered by his nephew, not his brother. Why would Hamlet do this? Don't you think he perhaps is threatening Claudius with revenge? Perhaps Claudius is also reacting to the threat. As Claudius heads to the confessional, he is overwrought with his guilt: "My offense is rank, and it smells to heaven." Yet, he may also feel guilty about the sins he will have to commit in the future to retain his queen and his crown. As Hamlet has become a true threat to his position and possibly his life, Claudius will be forced to take action against Hamlet. Claudius knows that he has not committed a perfect murder, and this fact weighs on him. How can he escape punishment in this life and the next? He must leave the theater to collect his thoughts.
The part you are talking about happens in Act III, Scene 2. Hamlet has arranged with the players to do a play (and have an extra speech) that will seem to be accusing Claudius of having killed Hamlet's father, the late king.
The ploy appears to work, and Claudius leaves because he does not like the way that the play seems to be accusing him. The king does not actually say why he's leaving, but he leaves right after the bad guy in the play poisons his rival. Hamlet comments that now the bad guy can have his rival's wife and Claudius leaves.