“If you start whining before the game’s even over,” Mr. Obama continued, “if whenever things are going badly for you and you lose, you start blaming somebody else, then you don’t have what it takes to be in this job, because there are times when things don’t go our way — or my way.”

Mr. Obama has been a happy warrior for Mrs. Clinton on the stump, not hesitating to tar Mr. Trump for his judgment, veracity and fitness for office. At first, though, he said he was reluctant to use the setting of the Rose Garden — where he stood next to Prime Minister Matteo Renzi of Italy — to deliver another broadside against Mr. Trump.

Asked about Mr. Trump’s statement that, if elected, he would meet with President Vladimir V. Putin of Russia before taking office, Mr. Obama replied, “I’m going to be a little more subdued in my discussions of the Republican nominee in this context than I might be on the campaign trail.”

“Mr. Trump rarely surprises me anymore,” the president said in a measured tone. He said he was more surprised that other Republican leaders, who had stridently opposed his efforts to reach out to Russia early in his presidency, would now endorse a candidate who he said was unprecedented in his “flattery of Mr. Putin.”

But when the subject returned to the Republican nominee — and the issue of voter fraud — Mr. Obama cast off his restraint.

“I’d invite Mr. Trump to stop whining and go try to make his case to get votes,” he said. If Mr. Trump won, the president added, Mrs. Clinton would deliver a concession speech and he would escort Mr. Trump to the Capitol for the time-honored ritual of a peaceful transfer of power.

“That’s what Americans do,” he said. “That’s why America is already great. One way of weakening America, making it less great, is if you start betraying those basic American traditions that have been bipartisan, and have helped hold together this democracy now for well over two centuries.”